DOC PREVIEW
UVa-Wise COSC 181 - Foundations of Computer Programming

This preview shows page 1-2-3-20-21-22-41-42-43 out of 43 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 431COSC 181 – Foundations of Computer ProgrammingClass 352Final ExamSection 0001•Wednesday, Dec. 10th•11-1:30Section 0002•Thursday, Dec. 11th•8-10:3037.10 Case Study: Class GradeBook Using a Two-Dimensional ArrayClass GradeBook•One-dimensional array•Store student grades on a single exam•Two-dimensional array•Store multiple grades for a single student and multiple students for the class as a whole•Each row represents a student’s grades•Each column represents all the grades the students earned for one particular exam41 // Fig. 7.23: GradeBook.h2 / / Def in it io n of class GradeBook that uses a 3 / / two- dimens iona l array to store test grades.4 / / Member funct ions are def ined in GradeBook.cpp5 #inc lude <string> // program uses C++ Standard Library string class6 us ing std::string;78 // GradeBook class definition9 class GradeBook10 {11 public:12 // constants13 const static int students = 10; // number of students14 const static int tests = 3; // number of tests15 16 // constructor initializes course name and array of grades17 GradeBook( string, const int [][ tests ] );Outlinefig07_23.cpp (1 of 2)GradeBook constructor accepts a string and a two-dimensional array51819 void setCourseName( string ); // function to set the course name20 string getCourseName(); // function to retrieve the course name21 void displayMessage(); // display a welcome message22 void processGrades(); // perform various operations on the grade data23 int getMinimum(); // find the minimum grade in the grade book24 int getMaximum(); // find the maximum grade in the grade book25 double getAverage( const int [], const int ); // find average of grades26 void outputBarChart(); // output bar chart of grade distribution27 void outputGrades(); // output the contents of the grades array28 private:29 string courseName; // course name for this grade book30 int grades[ students ][ tests ]; // two-dimensional array of grades31 }; // end class GradeBook Outlinefig07_23.cpp (2 of 2)Declare two-dimensional array grades61 // Fig. 7.24: GradeBook.cpp2 / / Member- funct ion def in it io ns for class GradeBook that3 / / uses a two- dimens iona l array to store grades.4 #include <iostream>5 using std::cout;6 using std::cin;7 using std::endl;8 using std::fixed; 910 #include <iomanip> // parameterized stream manipulators11 using std::setprecision; // sets numeric output precision12 using std::setw; // sets field width1314 // include definition of class GradeBook from GradeBook.h15 #include "GradeBook.h"1617 // two-argument constructor initializes courseName and grades array18 GradeBook::GradeBook( string name, const int gradesArray[][ tests ] )19 {20 setCourseName( name ); // initialize courseName2122 // copy grades from gradeArray to grades 23 for ( int student = 0; student < students; student++ ) 24 25 for ( int test = 0; test < tests; test++ ) 26 grades[ student ][ test ] = gradesArray[ student ][ test ];27 } // end two-argument GradeBook constructor28Outlinefig07_24.cpp (1 of 7)Use nested for loops to copy elements from gradesArray to grades729 / / funct ion to set the course name30 void GradeBook::setCourseName( string name )31 {32 courseName = name; // store the course name33 } // end function setCourseName3435 / / funct ion to ret r ieve the course name36 string GradeBook::getCourseName()37 {38 return courseName;39 } / / end funct ion getCourseName4041 / / disp lay a welcome message to the GradeBook user42 void GradeBook::displayMessage()43 {44 // th is statement calls getCourseName to get the 45 // name of the course this GradeBook represents46 cout << "Welcome to the grade book for \n " << getCourseName() << "!" 47 << endl;48 } / / end funct ion disp layMessage4950 / / perform var ious operat ions on the data51 void GradeBook::processGrades()52 {53 / / output grades array54 outputGrades();5556 // call funct ions getMinimum and getMax imum57 cout << "\nLowest grade in the grade book is " << getMinimum() 58 << "\nHighest grade in the grade book is " << getMaximum() << endl;Outlinefig07_24.cpp (2 of 7)85960 // output grade distribution chart of all grades on all tests61 outputBarChart();62 } // end function processGrades6364 // find minimum grade65 int GradeBook::getMinimum()66 {67 int lowGrade = 100; // assume lowest grade is 1006869 // loop through rows of grades array 70 for ( int student = 0; student < students; student++ ) 71 { 72 // loop through columns of current row 73 for ( int test = 0; test < tests; test++ ) 74 { 75 // if current grade less than lowGrade, assign it to lowGrade76 if ( grades[ student ][ test ] < lowGrade ) 77 lowGrade = grades[ student ][ test ]; // new lowest grade 78 } // end inner for 79 } // end outer for 8081 return lowGrade; // return lowest grade82 } // end function getMinimum83Outlinefig07_24.cpp (3 of 7)Loop through rows and columns of grades to find the lowest grade of any student9Outlinefig07_24.cpp (4 of 7)84 // f ind max imum grade85 int GradeBook::getMaximum()86 {87 int highGrade = 0; // assume highest grade is 08889 // loop through rows of grades array 90 for ( int student = 0; student < students; student++ ) 91 { 92 // loop through columns of current row 93 for ( int test = 0; test < tests; test++ ) 94 { 95 // if current grade greater than lowGrade, assign it to highGrade96 if ( grades[ student ][ test ] > highGrade ) 97 highGrade = grades[ student ][ test ]; // new highest grade 98


View Full Document

UVa-Wise COSC 181 - Foundations of Computer Programming

Download Foundations of Computer Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Foundations of Computer Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Foundations of Computer Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?