Вы находитесь на странице: 1из 3

Curtin University of Technology Department of Electrical and Computer Engineering Engineering Programming 210

Laboratory Session 9 1 Exercises

Exercise 1. Put a list of student names, identication numbers, and grades into a le called data. For example, the beginning of the le might look like Casanova Smith Jones 910017 934422 878766 A C B

Write a program called reorder that can be used to read the data in the le and put it into the array of type struct student. This can be done with redirection reorder < data The program should print out an ordered list of students and grades. Students with A grands should be listed rst, students with B grades next, and so forth. Among all students having the same grade, the students should be listed alphabetically by name. Exercise 2. Modify the program that you wrote in Exercise 1 by adding the function class average() to compute the class average and print it. Assume that an A grade has value 4, a B grade has value 3, and so forth. Exercise 3. Consider the code char *a = "xyx", *b = "xyz";

if ( a==b ) printf("The two strings have the same address!\n"); else printf("As I expected, the addresses are different.\n"); The expression a==b compares pointer values, not the contents of the strings. Also, the expression a=b is an assignment of a pointer value, not an assignment of a string. In this exercise we want to consider = and == with respect to their use with structures. Suppose now that a and b are two structure variables of the

same type. The expression a=b is valid, but the expression a==b is not. The operands of the == operator cannot be structures. Write a small test program to see what your compiler does if you try to use the expression a==b, where a and b are structures of the same type. Exercise 4. A tsunami is a large destructive wave, which is generated by sudden changes in the seaoorcaused by earthquakes, underwater volcanic eruptions, and underwater landslides. The following table lists large tsunamis from the 1990s: Date September 2, 1992 December 2, 1992 July 12, 1993 June 3, 1994 November 14, 1994 October 9, 1995 January 1, 1996 February 17, 1996 February 21, 1996 July 17, 1998 Location Nicaragua Flores Island Okushiri, Japan Eastern Java Mindoro Island Jalisco, Mexico Sulawesi Island Irain Java Peru Papua, New Guinea Maximum Wave (m) Fatalities 10 26 31 14 7 11 3.4 7.7 5 15 170 1000 239 238 49 1 9 161 12 2200

In this exercise, the following structure is used to represent information for tsunamis: struct tsunami { int mo, da, yr, fatalities; double max_height; char location[20]; } (a) Create a data le called waves.txt that contains the information in the above table. (b) Write a program to read the information in waves.txt. Use the preceding structure and print a report giving the maximum wave height for the tsunamis in the le. Include the average wave height (in feet) and the location of all tsunamis with a wave height higher than the average. (c) Write a program to read the information in waves.txt. Use the preceding structure and print a report with the total number of fatalities per year in the le. Us an output format similar to the following: Information for Large Tsunamis from the 1990s Year Number of Fatalities xxxx xx

Lab Report

A brief lab report for the above exercises is required for assessment purposes in Week 11 and should contain the following: 1. a description of working steps including assumptions, formulas, and algorithms wherever applicable. 2. source code with appropriate documentation in terms of comments. 3. sample outputs. 4. observations and conclusions wherever applicable.

Вам также может понравиться