1 / 17

Test 2

Test 2. Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules to Different Problems. C++ Functions. Why? Modularity!. C++ Functions. Function Prototype int IndexOfMax(float s[], int size); Function Definition

Download Presentation

Test 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. Test 2 Friday Functions Arrays For Loops • Understand Concepts and Rules • Memorize Concepts and Rules • Apply Concepts and Rules to Different Problems

  2. C++ Functions Why? Modularity!

  3. C++ Functions • Function Prototype int IndexOfMax(float s[], int size); • Function Definition int IndexOfMax(float s[], int size) { int index = 0; for (int i = 1; i < size; i ++) if (s[i] > s[index]) index = i; return index; } • Function Call index = IndexOfMax(Test2Scores, numStudents);

  4. Function Type The type of the value returned by a function float sqrt(float x); int IndexOfMax(float s[], int size); bool eof(); char RomanChar(int romanDigitValue); StudentType GetStudent(); void ReadAnswers(char answers[], int numQuestions); RETURN Statement! Not Parameters!

  5. Function Parameters • Need to pass data to the function • May need more than one value out of the function Not Return Statement! • Parameters are local variables • Parameters get values or addresses at function calls

  6. Formal Parameters and Actual Parameters • Formal Parameters In function prototype and definition Must specify type, &, and [] for arrays • Actual Parameters In function call No type, &, and no [] for arrays

  7. Passing Parameters • Basic Data Types (char, int, float, string, bool) Pass by Value (without & in prototype) Pass by Reference (with & in prototype) • Arrays (of any data type) Always Pass by Reference (Never &) • (Structure and Class)

  8. Value Parameters and Reference Parameters GetInput() main() void GetInput(float& payRate, float& hoursWorked) { cout << "Enter pay rate: "; cin >> payRate; cout << "Enter hours: "; cin >> hoursWorked; return; } const float REG_HOURS = 40.0; const float OVER_TIME = 1.5; int main() { float hours, rate, gross; GetInput(rate, hours); gross = GrossPay(rate, hours); // display result return 0; } Passing parameters Addresses of rate and hours Return control GrossPay() Passing parameters 12.5 50 float GrossPay(float payRate, float hours) { if (hours > REG_HOURS) payRate = (hours - REG_HOURS) * OVER_TIME * payRate + REG_HOURS * payRate; else payRate = hours * payRate; return payRate; } Return control With value

  9. In, Out, InOut Parameters The original value of the actual parameter The value of the actual parameter before the function call In The function uses the original value of the actual parameter, but does not change its value. Out The function does not use the original value of the actual parameter, but changes its value. InOut The function uses the original value of the actual parameter, and also changes its value.

  10. Tracing Functions Input: 10.5 53 float GrossPay(float payRate, float hours) void GetInput(float& payRate, float& hoursWorked); main() GetInput() GrossPay() hours rate gross payRate& hoursWorked& hours payRate ? ? ? ? ? ? ? Addr of Addr of rate hours 10.5 53.0 53.0 10.5 556.5 556.5

  11. Arrays • Array Declaration const int MAX_SIZE = 30; int size = 30; int nums[30]; // Valid? float ArrayA[MAX_SIZE]; // Valid? float ArrayB[size]; // Valid? • Memory allocation • Array Element and Index • Index starts with 0 • An array element is the same as a single variable • For Loop on array for (int i = 0; i < size; i ++) cin >> ArrayA[i];

  12. Functions on Arrays • Always passing by reference • No & • Size is passed separately • Const for IN parameter void ReadAnswers(char answers[], int numQuestions); void ProcessAnswer(const char keys[], const char answers[], int numQuestion, int& correct, bool& pass); • Function calls No [] • Passing arrays and passing array elements average = ArrayAvg(Scores, count); max = Largest(Scores[0], Scores[1], Scores[2]); • Linear search return value or index

  13. For Loops • Same as while loops • Better on arrays • Better on Count-Controlled loops

  14. Nested For Loops int result; for (int i = 4; i < 6; i ++) { result = 1; for (int j = i; j > 1; j --) result *= j; cout << “The result: “ << result; }

  15. Tracing i j result ? ? ? 4 1 4 4 3 12 2 24 1 5 1 5 5 4 20 3 60 2 120 1 6 int i, j, result; for (i = 4; i < 6; i ++) { result = 1; for (j = i; j > 1; j --) result *= j; cout << “The result: ” << result; } Do it yourself now!

  16. Schedule • Wednesday Quiz7-1 due Prog4 Due • Thursday Lab 8 • Friday Test 2 Prog4 Grace time

  17. Test 1 Test 2 Test 3 Final

More Related