Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Program 2 Pseudo Code Read NumOfSongs of 1 st CD (Prime Read!) While not end of file Process CD Read NumOfSongs for next CD.

Similar presentations


Presentation on theme: "1 Program 2 Pseudo Code Read NumOfSongs of 1 st CD (Prime Read!) While not end of file Process CD Read NumOfSongs for next CD."— Presentation transcript:

1 1 Program 2 Pseudo Code Read NumOfSongs of 1 st CD (Prime Read!) While not end of file Process CD Read NumOfSongs for next CD

2 2 Program 2 Pseudo Code Read NumOfSongs of 1 st CD While not end of file loopCount = 0 While loopCount < NumOfSongs Process one song loopCount ++ Read NumOfSongs for next CD

3 3 Using Functions for Program 2 Pseudo Code Read NumOfSongs of 1 st CD While not end of file loopCount = 0 While loopCount < NumOfSongs Process one song loopCount ++ Read NumOfSongs for next CD int main() {... cin >> numSongs; while (!cin.eof()) { loopCount = 0; while (loopcount < numSongs) { ProcessOneSong(); loopCount ++; } cin >> numSongs; } DisplayResult(); return 0; }

4 4 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; }

5 5 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; }

6 6 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; }

7 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! 7

8 8 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.

9 Function Design Good Functions Focusing on one thing Function name tells what it does sqrt(val) pow(base, exp) cin.eof() cin.get(ch) … 9

10 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); 10

11 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; } 11

12 Quiz 4-4 12


Download ppt "1 Program 2 Pseudo Code Read NumOfSongs of 1 st CD (Prime Read!) While not end of file Process CD Read NumOfSongs for next CD."

Similar presentations


Ads by Google