Download ppt - Quiz 5–1: Arrays

Transcript
Page 1: Quiz 5–1: Arrays

Quiz 5–1: Arrays

? ? ? ? . . . . ? ?

0 1 2 3 48 49

float myArray[50];

1

Page 2: Quiz 5–1: Arrays

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];

? ? ? ? . . . . ? ?

0 1 2 3 size-1 48 49

size elements (0 to size -1)

2

Page 3: Quiz 5–1: Arrays

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 &!

3

Page 4: Quiz 5–1: Arrays

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

4

Page 5: Quiz 5–1: Arrays

//--------------------------------------------------

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

5

Page 6: Quiz 5–1: Arrays

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;}

6

Page 7: Quiz 5–1: Arrays

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

7

Page 8: Quiz 5–1: Arrays

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!

8

Page 9: Quiz 5–1: Arrays

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;}

9

Page 10: Quiz 5–1: Arrays

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: const10

Page 11: Quiz 5–1: Arrays

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 2311


Recommended