Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1430: Programming in C++ Function Design 1. Good Functions Focusing on one thing Function name tells what it does sqrt(val) pow(base, exp) cin.eof()

Similar presentations


Presentation on theme: "CS 1430: Programming in C++ Function Design 1. Good Functions Focusing on one thing Function name tells what it does sqrt(val) pow(base, exp) cin.eof()"— Presentation transcript:

1 CS 1430: Programming in C++ Function Design 1

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

3 Good Functions 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); 3

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

5 Program 3 Must Use Functions! Each function has at most 30 lines! Function description! In, Out and InOut! Five Required Functions. You can have other functions. 5

6 Using Functions for Program 2 6

7 7 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together

8 8 Pseudo Code While not end of class Process one section While not end of class While not end of section Process one score

9 9 Pseudo Code Initialize class variables (LCV) While not end of class Initialize section variables (LCV) While not end of section Process one score Update section LCV Process and display section result Process class variables Update class LCV Process and display class result

10 10 Pseudo Code Initialize class variables (LCV) While not end of class Initialize section variables (LCV) While not end of section Process one score Update section LCV Process and display section result Process class variables Update class LCV Process and display class result

11 int main() { int score, sectionSum, hiScore, loScore, scoresInSection, count; int aCount, bCount, cCount, dCount, fCount; int countAll = 0, sumAll = 0, sectionNum = 1; cin >> scoresInSection; while ( ! cin.eof() ) { InitializeSectionValues(aCount, bCount, cCount, dCount, fCount, sectionSum, hiScore, loScore); count = 0; while ( count < scoresInSection ) { cin >> score; ProceeOneScore (score, hiScore, loScore, sectionSum, aCount, bCount, cCount, dCount, fCount); count ++; } DisplaySectionResult(aCount, bCount, cCount, dCount, fCount, scoresInSection, sectionSum, hiScore, loScore, sectionNum); UpdateClassValues(countAll, scoresInSection, sumAll, sectionSum, sectionNum); cin >> scoresInSection; cout << endl; } DisplayClassResult(sectionNum, countAll, sumAll); return 0; } 11

12 Function ProcessOneSection() //-------------------------------------------------------------- // Processes all scores of one section and returns section sum. // Parameters: (in, in) //-------------------------------------------------------------- int ProcessOneSection(int scoresInSection, int sectionNum) { int score, hiScore, loScore, sectionSum; int aCount, bCount, cCount, dCount, fCount; InitializeSectionValues(aCount, bCount, cCount, dCount, fCount, sectionSum, hiScore, loScore); int count = 0; while ( count < scoresInSection ) { cin >> score; ProceeOneScore (score, hiScore, loScore, sectionSum, aCount, bCount, cCount, dCount, fCount); count ++; } DisplaySectionResult(aCount, bCount, cCount, dCount, fCount, scoresInSection, sectionSum, hiScore, loScore, sectionNum); return sectionSum; } 12

13 ... int ProcessOneSection(int scoresInSection, int sectionNum); int main() { int countAll = 0, sumAll = 0, sectionNum = 1; int scoresInSection, sectionSum; cin >> scoresInSection; while ( ! cin.eof() ) { sectionSum = ProcessOneSection(scoresInSection, sectionNum); UpdateClassValues(countAll, scoresInSection, sumAll, sectionSum); sectionNum ++; cin >> scoresInSection; cout << endl; } DisplayClassResult(sectionNum, countAll, sumAll); return 0; } 13

14 14 Pseudo Code While not end of class Process one section While not end of class While not end of section Process one score

15 Schedule Quiz4-2: Due 10 PM Friday Quiz4-3: Due 10 PM Monday Lab5: Thursday by 5 PM Prog2 Grace time: 10 PM Friday Prog3 Due Tuesday, Oct 20 Optional groups 15


Download ppt "CS 1430: Programming in C++ Function Design 1. Good Functions Focusing on one thing Function name tells what it does sqrt(val) pow(base, exp) cin.eof()"

Similar presentations


Ads by Google