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

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

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

Page 1: 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()

CS 1430: Programming in C++

Function Design

1

Page 2: 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()

Good Functions

Focusing on one thingFunction name tells what it does

sqrt(val)pow(base, exp)

cin.eof()cin.get(ch)

2

Page 3: 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()

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

Page 4: 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()

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

Page 5: 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()

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

Page 6: 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()

Using Functions for Program 2

6

Page 7: 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()

7

Find Max, Min, Average of m Sections

Max, Min and Average of each section

Max, Min and Average of all sections together

Page 8: 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()

8

Pseudo Code

While not end of class Process one section While not end of class While not end of section Process one score

Page 9: 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()

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

Page 10: 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()

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

Page 11: 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()

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

Page 12: 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()

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

Page 13: 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()

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

Page 14: 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()

14

Pseudo Code

While not end of class Process one section While not end of class While not end of section Process one score

Page 15: 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()

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