Script started on Wed 01 Apr 2009 05:29:18 PM EDT [?1034h[17:29] m695340m@cslab100:~/Documents/cs130/class22 $ cat mccabe_exam2.cpp // Name: Micheal H. McCabe // April 1, 2009 // Exam #2 Source Code // // // Includes and namespace definitions #include #include using namespace std; // Function Prototypes double Addemup(double Data[]); int AddInt(double Data[]); void listem(double Data[]); double roundhalf(double num); // Main Routine int main() { double Data [ ] = {8, 50.5, 60.6, 70.7, -25.5, -30.3, -35.3, 10, 20, 0, 0, 0}; double sum; int total; char c1 = '\0'; while ( c1 != 'q' ) { cout << "Choose Letter: t, s, q, l:"; cin >> c1; switch (c1) { case 't': { sum=Addemup(Data); break; } case 's': { total=AddInt(Data); break; } case 'l': { listem(Data); break; } case 'q': { // Since the while loop depends on the value of c1 // anyway, we really don't need to do anything here. break; } default: { cout << "Not accepted\n"; break; } } } cout << fixed << setw(10) << setprecision(2) << showpoint << "Dbl Sum=" << sum << setprecision(0) << "; Int Total=" << total << "; All Done!\n"; return 0; } double Addemup(double Data[]) { double total=0; int i; for (i=0; i<=(int)Data[0]; i++) total=total+Data[i]; return total; } int AddInt(double Data[]) { int total=0; int i; float rounded; for (i=1; i<=(int)Data[0]; i++) total=total+(int)roundhalf(Data[i]); return total; } void listem(double Data[]) { int i; for (i=1; i<=(int)Data[0]; i++) { cout << "Element: " << i << " Data: " << Data[i] << endl; } return; } double roundhalf (double num) { return (int)num+((num*2-(int)(num*2)>0.5)?0.5:0.0); } [17:29] m695340m@cslab100:~/Documents/cs130/class22 $ make mccabe_exam2. make: `mccabe_exam2' is up to date. [17:29] m695340m@cslab100:~/Documents/cs130/class22 $ ./mccabe_exam2 Choose Letter: t, s, q, l:l Element: 1 Data: 50.5 Element: 2 Data: 60.6 Element: 3 Data: 70.7 Element: 4 Data: -25.5 Element: 5 Data: -30.3 Element: 6 Data: -35.3 Element: 7 Data: 10 Element: 8 Data: 20 Choose Letter: t, s, q, l:t Choose Letter: t, s, q, l:s Choose Letter: t, s, q, l:q Dbl Sum=128.70; Int Total=120; All Done! [17:30] m695340m@cslab100:~/Documents/cs130/class22 $ exit exit Script done on Wed 01 Apr 2009 05:30:18 PM EDT