Presentation is loading. Please wait.

Presentation is loading. Please wait.

Quiz 10 1. 2 // ------------------------------------------------------------------- // The function exchanges the two parameters. // Param: ( ) // Param:

Similar presentations


Presentation on theme: "Quiz 10 1. 2 // ------------------------------------------------------------------- // The function exchanges the two parameters. // Param: ( ) // Param:"— Presentation transcript:

1 Quiz 10 1

2 2 // ------------------------------------------------------------------- // The function exchanges the two parameters. // Param: ( ) // Param: ( InOut, InOut ) // ------------------------------------------------------------------- void Swap( int& x, int& y ) { int z = x; x = y; y = z; return; }

3 3 main() Swap() x y z x y z 17 15 20 ? ? ? address of x address of y _____________________________in main_______in main____________?____ ____________________________________________________________17___ _15______________________________________________________________ _________17______________________________________________________ address of y address of z _____________________________in main_______in main____________?____ ____________________________________________________________17___ _________20______________________________________________________ __________________17_____________________________________________

4 // DO: Complete struct Section. The struct has three fields: struct SectionType// title: string up to 25 chars {// count: number of students (int) // students: array of StudentType of // at most 30 students char title[26]; // Not 25! int count; StudentType students[30]; // Not 31 or 29 }; 4

5 5 // ------------------------------------------------------------------- // The function inputs data to a StudentType struct and returns // the struct to the calling function. // Param: ( None ) // ------------------------------------------------------------------- StudentType GetStudent() { // Don’t write anything here // But you should call this function later. }

6 6 // ------------------------------------------------------------------- // The function has one parameter sec of SectionType // It inputs all data to sec. // DO: Specify the parameter as in, out, or in/out. // Param: ( ) // Param: ( OUT ) // ------------------------------------------------------------------- // DO: Complete the function header GetSection( ) void GetSection( SectionType & sec ) { // DO: Write the function body. // You MUST call function GetStudent.... }

7 7 // ------------------------------------------------------------------- // The function has one parameter sec of SectionType // It inputs all data to sec. // DO: Specify the parameter as in, out, or in/out. // Param: ( OUT ) // ------------------------------------------------------------------- // DO: Complete the function header void GetSection( SectionType & sec ) { // DO: Write the function body. // You MUST call function GetStudent. cin >> sec.title >> sec.count; for (int i = 0; i < sec.count; i ++) sec.students[i] = GetStudent(); return; }

8 8 // -------------------------------------------------------------- // The function has one parameter sec of SectionType. // It computes and returns the average GPA of all students // in the section. // DO: Specify the parameter as in, out, or in/out. // Param: ( ) // Param: ( IN ) // -------------------------------------------------------------- // DO: Complete the function header. AvgGPA( ) float AvgGPA( const SectionType& sec ) { // DO: Write the function body.... }

9 9 // -------------------------------------------------------------- // The function has one parameter sec of SectionType. // It computes and returns the average GPA of all students // in the section. // DO: Specify the parameter as in, out, or in/out. // Param: ( IN ) // -------------------------------------------------------------- // DO: Complete the function header. float AvgGPA( const SectionType& sec ) { // DO: Write the function body. float total = 0; for (int i = 0; i < sec.count; i ++) total += sec.students[i].gpa; return (total / sec.count); }

10 10 // -------------------------------------------------------------- // The function has one parameter sec of SectionType. // The function computes and returns the number of students of sec // whose gpa is above avg. // DO: Specify the parameter as in, out, or in/out. // Param: ( ) // Param: ( IN ) // -------------------------------------------------------------- // DO: Complete the function header. AboveAvg( ) int AboveAvg( const SectionType& sec ) { // DO: Write the function body.... }

11 11 // -------------------------------------------------------------- // The function has one parameter sec of SectionType. // The function computes and returns the number of students of sec // whose gpa is above avg. // DO: Specify the parameter as in, out, or in/out. // Param: ( IN ) // -------------------------------------------------------------- // DO: Complete the function header. int AboveAvg( const SectionType& sec ) { // DO: Write the function body. // The function MUST call function AvgGPA // to find the average GPA. int above = 0; float average = AvgGPA(sec); for (int i = 0; i < sec.count; i ++) if (sec.students[i].gpa > average) above ++; return above; }

12 12 int main() { SectionType CS143; int count; // DO: Call GetSection to input data to CS143. GetSection( CS143 ); // DO: Call AboveAvg to find the number of students who gpa is higher than the section average gpa. count = AboveAvg( CS143 ); cout << endl << “There are “ << count << “ students in the section whose gpa is above the ” << “average gpa.”; return 0; }

13 // DO: Include the header file for file Input/Output. #include int main() { int number, total = 0; char filename[31]; // DO: Declare a variable file1 as an input file stream. ifstream file1; // DO: Declare a variable file2 as an output file stream. ofstream file2; // DO: Open a file J:\CS143\Q10.dat for input using file1. file1.open(“J:\\CS143\\Q10.dat”); 13

14 // DO: Fill in the condition that file1 cannot be open for input. if ( !file1.good() )// file1.fail() { // DO: Display an error message to the standard output stream. cout << “Cannot open file.”; return 1; } // DO: Read an integer from file1 to number. file1 >> number; // Function get() reads one char at a time. // DO: Fill in the condition that it’s NOT the end of the input file. while ( !file1.eof() ) { total += number; // DO: Read in the next integer from file1 to number. file1 >> number; } 14

15 // DO: Output the value of total to the standard output stream. cout << total; cout << “Enter the output file name: ”; cin >> filename; // DO: Open the entered file for output using file2. file2.open(filename); // Not “filename”! // Let’s assume the file can be open for output. // DO: Output the value of total to file2 file2 << total; // DO: Close both input file stream file1 and output file stream file2. file1.close(); file2.close(); // No parameters! return 0; } 15


Download ppt "Quiz 10 1. 2 // ------------------------------------------------------------------- // The function exchanges the two parameters. // Param: ( ) // Param:"

Similar presentations


Ads by Google