110 likes | 184 Views
Quiz 5–1: Arrays. float myArray[50];. 0 1 2 3 48 49. float myArray[50]; int size; // Read in the value of size.
E N D
Quiz 5–1: Arrays float myArray[50]; 0 1 2 3 48 49
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
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 &!
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
//-------------------------------------------------- // 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?
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; }
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
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!
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; }
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
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