120 likes | 198 Views
Program 2. Pseudo Code Read NumOfSongs of 1 st CD (Prime Read!) While not end of file Process CD Read NumOfSongs for next CD. Program 2. Pseudo Code Read NumOfSongs of 1 st CD While not end of file loopCount = 0 While loopCount < NumOfSongs
E N D
Program 2 Pseudo Code Read NumOfSongs of 1st CD (Prime Read!) While not end of file Process CD Read NumOfSongs for next CD
Program 2 Pseudo Code Read NumOfSongs of 1st CD While not end of file loopCount = 0 While loopCount < NumOfSongs Process one song loopCount ++ Read NumOfSongs for next CD
Using Functions for Program 2 int main() { ... cin >> numSongs; while (!cin.eof()) { loopCount = 0; while (loopcount < numSongs) { ProcessOneSong(); loopCount ++; } cin >> numSongs; } DisplayResult(); return 0; } Pseudo Code Read NumOfSongs of 1st CD While not end of file loopCount = 0 While loopCount < NumOfSongs Process one song loopCount ++ Read NumOfSongs for next CD
int main() { . . . countCD = 0; cin >> numSongs; while (!cin.eof()) { countCD ++; totalSeconds = 0; CDHeader(countCD); loopCount = 0; while (loopcount < numSongs) { ProcessOneSong(totalSeconds); loopCount ++; } DisplayCDTime(countCD, totalSeconds); grandTotal += totalSeconds; cin >> numSongs; } DisplayResult(countCD, grandTotal); return 0; }
int main() { int countCD = 0, grandTotal = 0; int numSongs; cin >> numSongs; while (!cin.eof()) { ProcessOneCD(numSongs, countCD, grandTotal); cin >> numSongs; } DisplayResult(countCD, grandTotal); return 0; }
void ProcessOneCD(int numSongs, int& countCD, int& grandTotal) { int totalSeconds = 0, loopCount = 0; countCD ++; DisplayCDHeader(countCD); while (loopCount < numSongs) ProcessOneSong(loopCount, totalSeconds); DisplayCDResult(countCD, totalSeconds); grandTotal += totalSeconds; return; }
Program 3 Must Use Functions! Required functions Other functions of your own Each function has at most 30 lines! Function description! In, Out and InOut Parameters!
Program 3 Required Functions int GetIntegerValue(string inputPrompt, string errMessage, int lowLimit, int highLimit); int NumberOfBoxes(int numTiles); void DisplayResults(int tiles, int boxes, int extra); You must call function GetIntegerValue to get all input values, one at a time. Otherwise, you will lose five points.
Function Design Good Functions Focusing on one thing Function name tells what it does sqrt(val) pow(base, exp) cin.eof() cin.get(ch) …
Function Design Good Functions int NumberOfBoxes(int numTiles); float GrossPay(float payRate, float hours); void DisplayResult(float avg, float max, float min); void GetInput(float& payRate, float& hoursWorked); void DisplayMenu(); void UpdateMaxMin(float value, float& max, float& min);
Bad Functions float GetRate(float rate) { cout << “Enter the rate: ”; cin >> rate; return rate; } // Why use parameter? // Use local variable! float GetRate() { float rate; cout << “Enter the rate: ”; cin >> rate; return rate; }