1 / 12

Program 2

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

troy-baird
Download Presentation

Program 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

  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) …

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

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

  12. Quiz 4-4

More Related