CS 1430 Programming in C Function Design 1

  • Slides: 15
Download presentation
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,

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

Good Functions float Gross. Pay(float pay. Rate, float hours); void Display. Result(float avg, float

Good Functions float Gross. Pay(float pay. Rate, float hours); void Display. Result(float avg, float max, float min); void Get. Input(float& pay. Rate, float& hours. Worked); void Display. Menu(); void Update. Max. Min(float value, float& max, float& min); 3

Bad Functions float Get. Rate(float rate) { cout << “Enter the rate: ”; cin

Bad Functions float Get. Rate(float rate) { cout << “Enter the rate: ”; cin >> rate; return rate; } // Why use parameter? // Use local variable! float Get. Rate() { 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!

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

Using Functions for Program 2 6

Using Functions for Program 2 6

Find Max, Min, Average of m Sections Max, Min and Average of each section

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

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

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

Pseudo Code Initialize class variables (LCV) While not end of class Initialize section variables

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 9

Pseudo Code Initialize class variables (LCV) While not end of class Initialize section variables

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

int main() { int score, section. Sum, hi. Score, lo. Score, scores. In. Section,

int main() { int score, section. Sum, hi. Score, lo. Score, scores. In. Section, count; int a. Count, b. Count, c. Count, d. Count, f. Count; int count. All = 0, sum. All = 0, section. Num = 1; cin >> scores. In. Section; while ( ! cin. eof() ) { Initialize. Section. Values(a. Count, b. Count, c. Count, d. Count, f. Count, section. Sum, hi. Score, lo. Score); count = 0; while ( count < scores. In. Section ) { cin >> score; Procee. One. Score (score, hi. Score, lo. Score, section. Sum, a. Count, b. Count, c. Count, d. Count, f. Count); count ++; } Display. Section. Result(a. Count, b. Count, c. Count, d. Count, f. Count, scores. In. Section, section. Sum, hi. Score, lo. Score, section. Num); Update. Class. Values(count. All, scores. In. Section, sum. All, section. Sum, section. Num); cin >> scores. In. Section; cout << endl; } Display. Class. Result(section. Num, count. All, sum. All); return 0; } 11

Function Process. One. Section() //-------------------------------// Processes all scores of one section and returns section

Function Process. One. Section() //-------------------------------// Processes all scores of one section and returns section sum. // Parameters: (in, in) //-------------------------------int Process. One. Section(int scores. In. Section, int section. Num) { int score, hi. Score, lo. Score, section. Sum; int a. Count, b. Count, c. Count, d. Count, f. Count; Initialize. Section. Values(a. Count, b. Count, c. Count, d. Count, f. Count, section. Sum, hi. Score, lo. Score); int count = 0; while ( count < scores. In. Section ) { cin >> score; Procee. One. Score (score, hi. Score, lo. Score, section. Sum, a. Count, b. Count, c. Count, d. Count, f. Count); count ++; } Display. Section. Result(a. Count, b. Count, c. Count, d. Count, f. Count, scores. In. Section, section. Sum, hi. Score, lo. Score, section. Num); return section. Sum; } 12

. . . int Process. One. Section(int scores. In. Section, int section. Num); int

. . . int Process. One. Section(int scores. In. Section, int section. Num); int main() { int count. All = 0, sum. All = 0, section. Num = 1; int scores. In. Section, section. Sum; cin >> scores. In. Section; while ( ! cin. eof() ) { section. Sum = Process. One. Section(scores. In. Section, section. Num); Update. Class. Values(count. All, scores. In. Section, sum. All, section. Sum); section. Num ++; cin >> scores. In. Section; cout << endl; } Display. Class. Result(section. Num, count. All, sum. All); return 0; } 13

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

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

Schedule • Quiz 4 -2: Due 10 PM Friday • Quiz 4 -3: Due

Schedule • Quiz 4 -2: Due 10 PM Friday • Quiz 4 -3: Due 10 PM Monday • Lab 5: Thursday by 5 PM • Prog 2 Grace time: 10 PM Friday • Prog 3 Due Tuesday, Oct 20 Optional groups 15