Upload
jemima-walsh
View
216
Download
0
Embed Size (px)
DESCRIPTION
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
Citation preview
CS 1430: Programming in C++
Function Design
1
Good Functions
Focusing on one thingFunction name tells what it does
sqrt(val)pow(base, exp)
cin.eof()cin.get(ch)
2
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
Bad Functionsfloat 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
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
Using Functions for Program 2
6
7
Find Max, Min, Average of m Sections
Max, Min and Average of each section
Max, Min and Average of all sections together
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
Pseudo CodeInitialize 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 LCVProcess and display class result
10
Pseudo CodeInitialize 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 LCVProcess and display class result
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
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
. . .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
Pseudo Code
While not end of class Process one section While not end of class While not end of section Process one score
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