1 / 11

Quiz 5–1: Arrays

Quiz 5–1: Arrays. float myArray[50];. 0 1 2 3 48 49. float myArray[50]; int size; // Read in the value of size.

ria-english
Download Presentation

Quiz 5–1: Arrays

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. Quiz 5–1: Arrays float myArray[50]; 0 1 2 3 48 49

  2. float myArray[50]; int size; // Read in the value of size. cin >> size; // Using a for loop to input values into the array. for (int i = 0; i < size; i ++) cin >> myArray[i]; size elements (0 to size -1) 0 1 2 3 size-1 48 49

  3. Using a function to read data to an array The size (number of elements) is known Function Prototype Function Name InputToArray Function Type void How to return an array? Parameters s[] : array of float In, Out, InOut OUT size: int, number of elements of s[] In, Out, InOut IN // Parameters: (Out, In) void InputToArray(float s[], int size); // No &!

  4. Passing Parameters • Basic Data Types (char, int, float, string, bool) Pass by Value (without &) Pass by Reference (with &) • Arrays (of any data type) Always Pass by Reference (Never &) • Why? Save Space and Time for copying values

  5. //-------------------------------------------------- // The function reads size float values into // array s[], assuming size is positive // and in the range. // Parameters: (out, in) //-------------------------------------------------- void InputToArray(float s[], int size) { for (int i = 0; i < size; i++) cin >> s[i]; return; } // What’s the value of size?

  6. const int MAX_SIZE = 100; void InputToArray(float s[], int size); int main() { int count; float Scores[MAX_SIZE], average; cin >> count; // Call function to input data to array Scores[] InputToArray(Scores, count); // No [] for array as actual parameter // No type for any actual parameters // Compute the average return 0; }

  7. Using a function to compute the average Function Prototype Function name ArrayAvg Function type float Parameters s[] : array of float In, Out, InOut size: int, number of elements of s[] In, Out, InOut // Parameters: (In, In) float ArrayAvg(const float s[], int size); // Array is always passed by reference // Use const for In array parameter // No const for size

  8. Using a function to compute the average //------------------------------------------------------ // The function computes and returns the average of all // array elements of s[], assuming size is positive // and in the range. // Parameters: (In, In) //------------------------------------------------------ float ArrayAvg(const float s[], int size) { float total; total = 0; for (int i = 0; i < size; i++) total += s[i]; return total / size; } // Compute average after the for loop. // Not inside the loop!

  9. const int MAX_SIZE = 100; void InputToArray(float s[], int size); float ArrayAvg(const float s[], int size); int main() { int count; float Scores[MAX_SIZE], average; cin >> count; InputToArray(Scores, count); // Function call: No type! No []! average = ArrayAvg(Scores, count); // Function call: No type! No []! cout << “The average score: “ << average; return 0; }

  10. Function Parameters • Basic Data Types (char, int, float, string, bool) Pass by Value (without &) In Parameters Pass by Reference (with &) Out and InOut Parameters • Arrays (of any data type) Always Pass by Reference (Never &) In Parameters: const

  11. Schedule Quiz5-1 Due 5 PM Today Quiz5-2 Due 5 PM Friday Thursday: Lab6 Lab5: 3 point by 5 pm today Program 3 Due 9:30 PM Today, March 23

More Related