/*********************************************************************** * CS 141, Spring '01, Exam 2: Sample Answers * * These are possible answers to the questions on the exam. They are * not the only possible right answers, nor are they necessarily the * most concise answers. * If you find an error, please notify me at tuinstra@clarkson.edu * right away. For any error that may be found, the first three * persons to report it to me will receive an extra 2 points per * error (up to a maximum exam score of 100, and up to a maximum * of 5 extra points). * The `include' statements below are not required in your answers. * They are included here so that this file can actually be compiled. * *********************************************************************** */ #include #include #include /*====================================================================== * Problem 1 A ======================================================================= */ int sum_part( int arr[], /* array of integers */ int item_count, /* how many items in the array */ int start_pos, /* add up scores starting from here... */ int end_pos ) /* ... and ending here */ { int sum; int i; sum = 0; for( i = start_pos; i <= end_pos; ++i ) sum += arr[i]; return sum; } /*====================================================================== * Problem 1 B ======================================================================= */ int main( ) { int scores[101]; /* indexes from 0 to 100 require 101 entries */ int B_count; /* holds how many got a `B' */ int i; /* array index and loop control */ /* Instruct and prompt the user. Your program was not required * to do this. */ cout << "Please enter how many students got a score of 0," << endl; cout << " followed by how many got a score of 1," << endl; cout << " followed by how many got a score of 2, and so" << endl; cout << " on up to how many got a score of 100. If no" << endl; cout << " students earned a given score, enter 0 (zero)" << endl; cout << " for that score's number. You will need to" << endl; cout << " enter 101 numbers. Please enter them now." << endl; /* The user will now enter 101 numbers. We read them in and put * them into their respective array positions. */ for( i = 0; i <= 100; ++i ) cin >> scores[i]; /* Now calculate how many students got scores from 80 to 89 */ B_count = sum_part(scores, 101, 80, 89); /* Print out the result */ cout << "There were " << B_count << " students who got a B\n"; return (EXIT_SUCCESS); } /*====================================================================== * Problem 2 ======================================================================= */ bool more( char str[] ) { int i; /* index into string */ int count; /* how many letters are the same as the first letter */ count = 0; /* The loop below will always count the first character, since * it's equal to itself! */ for ( i = 0; str[i] != '\0'; ++i ) { if ( str[i] == str[0] ) ++count; } /* A count of one means the only character matching str[0] * was itself. Other positive counts mean there were other * characters which matched str[0]. A count of 0 occurs if * (and only if) the string is empty; in this case we'll * return `false' (your function is not required to handle the * empty string this way). Negative counts are not possible. */ if ( count > 1 ) return true; else return false; } /*====================================================================== * Problem 3 ======================================================================= */ /* Here, we consider `positive' to mean integers strictly greater * than zero. We will also give credit if your answer treats 0 as * a positive number. */ void copypos( ifstream & in, ofstream & out ) { int item; /* holds numbers read from the input stream */ while ( in >> item ) { if ( item > 0 ) /* write positives, ignore others */ out << item << endl; } } /*====================================================================== * Problem 4 A ======================================================================= */ class university { private: char name[201]; /* name of the university, 200 chars max */ int women; /* female enrollment */ int men; /* male enrollment */ public: int num_students( ); /* Returns total enrollment. * NOTE: no parameters needed!! */ }; /*====================================================================== * Problem 4 B ======================================================================= */ int university::num_students( ) { return (women + men); /* Since this is a member function, it * has direct acess to the data fields. */ } /*====================================================================== * Problem 4 C ======================================================================= */ bool more_students( university & u1, university & u2 ) { if ( u1.num_students() > u2.num_students() ) return true; else return false; }