Lab 9
-
Upload
samuel-tan -
Category
Documents
-
view
89 -
download
4
Transcript of Lab 9
Curtin University of TechnologyDepartment of Electrical and Computer Engineering
Engineering Programming 210
Laboratory Session 9
1 Exercises
Exercise 1. Put a list of student names, identification numbers, and grades into a filecalled data. For example, the beginning of the file might look like
Casanova 910017 ASmith 934422 CJones 878766 B· · · · · · · · ·
Write a program called reorder that can be used to read the data in the file andput 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 withA grands should be listed first, students with B grades next, and so forth. Amongall students having the same grade, the students should be listed alphabeticallyby name.
Exercise 2. Modify the program that you wrote in Exercise 1 by adding the functionclass average() to compute the class average and print it. Assume that an A gradehas 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 assignmentof a string. In this exercise we want to consider = and == with respect to theiruse 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. Theoperands of the == operator cannot be structures. Write a small test program tosee what your compiler does if you try to use the expression a==b, where a andb are structures of the same type.
Exercise 4. A tsunami is a large destructive wave, which is generated by suddenchanges in the seafloor—caused by earthquakes, underwater volcanic eruptions,and underwater landslides. The following table lists large tsunamis from the 1990s:
Date Location Maximum Wave (m) Fatalities
September 2, 1992 Nicaragua 10 170December 2, 1992 Flores Island 26 1000July 12, 1993 Okushiri, Japan 31 239June 3, 1994 Eastern Java 14 238November 14, 1994 Mindoro Island 7 49October 9, 1995 Jalisco, Mexico 11 1January 1, 1996 Sulawesi Island 3.4 9February 17, 1996 Irain Java 7.7 161February 21, 1996 Peru 5 12July 17, 1998 Papua, New Guinea 15 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 file called waves.txt that contains the information in the abovetable.
(b) Write a program to read the information in waves.txt. Use the precedingstructure and print a report giving the maximum wave height for the tsunamisin the file. Include the average wave height (in feet) and the location of alltsunamis with a wave height higher than the average.
(c) Write a program to read the information in waves.txt. Use the precedingstructure and print a report with the total number of fatalities per year inthe file. Us an output format similar to the following:
Information for Large Tsunamis from the 1990s
Year Number of Fatalities
xxxx xx
2 Lab Report
A brief lab report for the above exercises is required for assessment purposes in Week 11and should contain the following:
1. a description of working steps including assumptions, formulas, and algorithmswherever applicable.
2. source code with appropriate documentation in terms of comments.
3. sample outputs.
4. observations and conclusions wherever applicable.