// John Bent's CS 110 Hand-out 4 // Parrallel arrays. // // Limitations: MINIMAL error checking. #include #include // make the maxsize easily accessible, facilitating easy change const int MAXSIZE = 100; // function prototypes // for a description, see their implementations void initialize (int&, int&, int&, int&, int&); int getAve_Method1 (int, int, int); void getAve_Method2 (int, int, int, int&); void show (int[], int[], int); // main() starts here int main() { // parrellel arrays to hold student data int ID[MAXSIZE], Quiz1[MAXSIZE], Quiz2[MAXSIZE], Quiz3[MAXSIZE], FinalAve[MAXSIZE]; // other non-array variables int students, i; // students is number of students in teacher's class // i is for loops char answer; // does teacher want to enter by hand or batch? for (i = 0; i < MAXSIZE; i++) initialize (ID[i], Quiz1[i], Quiz2[i], Quiz3[i], FinalAve[i]); do { cout << "\nHow many students do you have? "; cin >> students; } while (students < 1 || students > 100); do { cout << "\nWould you like to enter the info by [H]and or " << "is it in a [B]atch file? "; cin >> answer; } while (answer != 'B' && answer != 'b' && answer != 'H' && answer != 'h'); if (answer == 'B' || answer == 'b') { char filename[100]; cout << "\nPlease enter the file name: "; cin >> filename; ifstream infile (filename, ios::in); if (!infile) { // file not found cout << "\nI'm sorry. I could not find " << filename << ".\n"; return 1; // kick out the user for entering a bad filename } // end if // file is there, get the data for (i = 0; i < students && !infile.eof(); i++) infile >> ID[i] >> Quiz1[i] >> Quiz2[i] >> Quiz3[i]; infile.close(); // clean up after yourself } // end if else { // user wants to enter by hand for (i = 0; i < students; i++) { cout << "Enter ID followed by three quiz scores: "; cin >> ID[i] >> Quiz1[i] >> Quiz2[i] >> Quiz3[i]; } } // end else, arrays have been filled // do the averages - two ways for (i = 0; i < students; i++) FinalAve[i] = getAve_Method1(Quiz1[i], Quiz2[i], Quiz3[i]); // OR this way for (i = 0; i < students; i++) getAve_Method2(Quiz1[i], Quiz2[i], Quiz3[i], FinalAve[i]); // show them show (ID, FinalAve, students); return 0; } // this function is passed the addresses of five integers and set their // values to zero // void initialize (int &id, int &Q1, int &Q2, int &Q3, int &Ave) { id = Q1 = Q2 = Q3 = Ave = 0; } // this function makes copies of three integer values, and returns // their average // int getAve_Method1 (int One, int Two, int Three) { return ( (One + Two + Three) / 3 ); } // this function is identical to the previous except that it does // not need to return the average because it is passed the address // where the average needs to go void getAve_Method2 (int One, int Two, int Three, int &ave) { ave = (One + Two + Three) / 3; } // this function takes the addresses of two arrays and makes a copy // of one integer value which is the working size of the arrays // it then traverses the arrays, printing the values // void show (int identityNo[], int ave[], int number_students) { for (int i = 0; i < number_students; i++) cout << identityNo[i] << " " << ave[i] << endl; }