Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1430: Programming in C++.

Similar presentations


Presentation on theme: "CS 1430: Programming in C++."— Presentation transcript:

1 CS 1430: Programming in C++

2 Quiz 5–1: Arrays . . . . // DO_02: Declare an array myArray of
// type float of 50 elements. float myArray[50]; ?

3 size elements (index: 0 to size -1)
float myArray[50]; int size; // Read in the value of size. cin >> size; // Using a for loop to input values into the array. // Assuming size is between 1 and 50 (30 in Quiz5-1). for (int i = 0; i < size; i ++) cin >> myArray[i]; size elements (index: 0 to size -1) ? size

4 Using a Function to Read Data to an Array
The size (number of values to input) 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 values to read into s[] IN // Parameters: (Out, In) void InputToArray(float s[], int size); // No &!

5 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

6 //--------------------------------------------------
// 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?

7 const int MAX_SIZE = 100; void InputToArray(float s[], int size); int main() { int count; float scores[MAX_SIZE], average; cin >> count; // Assuming in the range // 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; }

8 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[] // 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

9 Using a Function to Compute the Average
Function Definition // // 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!

10 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); cout << “The average score: ” << average; return 0; }

11 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

12 Lab5: 3 point by 5 pm Tuesday
Schedule Quiz5-1 Due 10 PM Today Quiz5-2 Due 10 PM Wednesday Thursday: Lab6 Lab5: 3 point by 5 pm Tuesday Program 3 Due 10 PM Tuesday

13 Quiz5-1 // DO_04: Input a number into the first element of myArray. // DO_05: Input a number into the last element of myArray. // DO_12: Assign the average of the 1st, 2nd and last array // elements of myArray to the second to last array element // of myArray. // DO_13: Complete the following statement to output // the second to last array element of myArray. // DO_14: Use a for loop to compute the largest value of the // first 15 elements of myArray and store the result in Max. What’s the index? 49 to 48 48 0 to 14

14 Data Type double Any Questions?
Prog3 Data Type double Any Questions?


Download ppt "CS 1430: Programming in C++."

Similar presentations


Ads by Google