> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; }"> > grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.

Similar presentations


Presentation on theme: "Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the."— Presentation transcript:

1 Computer Science 1620 Arrays

2 Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the grades 2. calculate the average A 3. add (70 – A) to each grade 4. re-output the new grades to one decimal place

3

4 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; }

5 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades 1. Read in the grades 2. calculate the average A 3. add (70 – A) to each grade

6 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades 1. Read in the grades 2. calculate the average A 3. add (70 – A) to each grade

7 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades 2. calculate the average A 3. add (70 – A) to each grade

8 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades 2. calculate the average A 3. add (70 – A) to each grade

9 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades 3. add (70 – A) to each grade

10 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades 3. add (70 – A) to each grade

11 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades

12 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades

13 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; }

14 Does the previous code work? Yes! Problems: 1) Repeated code! adjusting each mark, and re-outputting each mark is basically the same for each mark

15 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; }

16 Does the previous code work? Yes! Problems: 1) Repeated code! 2) Scalability what happens if the number of students increases to X, where X is large?

17 #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; double grade6, grade7, grade8, grade9, grade10; double grade11, grade12, grade13, grade14, grade15; double grade16, grade17, grade18, grade19, grade20; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5 >> grade6 >> grade7 >> grade8 >> grade9 >> grade10 >> grade11 >> grade12 >> grade13 >> grade14 >> grade15 >> grade16 >> grade17 >> grade18 >> grade19 >> grade20; double A = (grade1 + grade2 + grade3 + grade4 + grade5 +grade6 + grade7 + grade8 + grade9 + grade10 +grade11 + grade12 + grade13 + grade14 + grade15 +grade16 + grade17 + grade18 + grade19 + grade20)/20; grade1 += (70 – A); grade2 += (70 – A); grade3 += (70 – A); grade4 += (70 – A); grade5 += (70 – A); grade6 += (70 – A); grade7 += (70 – A); grade8 += (70 – A); grade9 += (70 – A); grade10 += (70 – A); grade11 += (70 – A); grade12 += (70 – A); grade13 += (70 – A); grade14 += (70 – A); grade15 += (70 – A); grade16 += (70 – A); grade17 += (70 - A); grade18 += (70 - A); grade19 += (70 - A); grade20 += (70 - A);...

18 Arrays an aggregate data structure vs. atomic data types (int, float, double, char, …) a group of variables of the same type

19 Declare 10 variables of type int: int x1, x2, x3, x4, x5, x6, x7, x8, x9, x10; int x[10]; Without Arrays: With Arrays: Array syntax: type name[size]; type can be any of the int or floating point types* name can be any valid variable name size can be any positive integral value, but MUST BE CONSTANT*

20 How to access the elements in an array use the subscript operator [ ] indices START AT 0, NOT 1 value in the subscripts need not be a constant int x[3]; x[0] = 5; // sets the first element in the array to value 5 x[1] = 6; // sets the second element in the array to value 6 int i = 2; x[i] = 7; // sets the third element in the array to value 7

21 The elements of an array are used exactly like a single variable can assign a value to them: x[i] = 5; can use them in an expression: int sum = x[0] + x[1] + x[2]; cin >> x[0]; cout << "Second value: " << x[1];

22 Arrays and Loops the indexing feature of arrays makes it very easy to use them in a loop very handy if you want to perform some operation on each element in the array General template (using a for loop) given an array x of size N begin integer index (call it i) at 0 loop up to (but not including) N increment one at a time perform operation on x[i] inside loop int x[10]; for (int i = 0; i < 10; i++) { // do some operation on x[i] }

23 Example 1: given an array x of 10 ints, set each element in the array to equal 5 Example 2: given an array x of 10 ints, output the value of each element to standard output int x[10]; for (int i = 0; i < 10; i++) { x[i] = 5; } int x[10]; for (int i = 0; i < 10; i++) { cout << x[i] << endl; }

24 Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Solution: read in the grades calculate the average A add (70 – A) to each grade re-output the new grades

25 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Reprogram this example using arrays!

26 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Convert these to an array

27 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Convert these to an array

28 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } cin a value to each element in the array 1. begin integer index (call it i) at 0 2. loop up to (but not including) N 3. increment one at a time 4. perform operation on x[i] inside loop

29 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } cin a value to each element in the array 1. begin integer index (call it i) at 0 2. loop up to (but not including) N 3. increment one at a time 4. perform operation on x[i] inside loop

30 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } cin a value to each element in the array 1. begin integer index (call it i) at 0 2. loop up to (but not including) N 3. increment one at a time 4. perform operation on x[i] inside loop

31 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } cin a value to each element in the array 1. begin integer index (call it i) at 0 2. loop up to (but not including) N 3. increment one at a time 4. perform operation on x[i] inside loop

32 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } cin a value to each element in the array 1. begin integer index (call it i) at 0 2. loop up to (but not including) N 3. increment one at a time 4. perform operation on x[i] inside loop

33 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Add the value of each element in the array to A.

34 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Add the value of each element in the array to A.

35 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Divide the total by 5 to obtain the average.

36 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Subtract 70-A from each grade.

37 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; for (int i = 0; i < 5; i++) grade[i] += (70 – A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Subtract 70-A from each grade.

38 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; for (int i = 0; i < 5; i++) grade[i] += (70 – A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Re-output each grade.

39 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; for (int i = 0; i < 5; i++) grade[i] += (70 – A); cout << fixed << setprecision(1); cout << "New grades:" << endl; for (int i = 0; i < 5; i++) cout << grade[i] << "%" << endl; return 0; } Re-output each grade.

40 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; for (int i = 0; i < 5; i++) grade[i] += (70 – A); cout << fixed << setprecision(1); cout << "New grades:" << endl; for (int i = 0; i < 5; i++) cout << grade[i] << "%" << endl; return 0; } We can save some space by combining these two loops!

41 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; cout << fixed << setprecision(1); cout << "New grades:" << endl; for (int i = 0; i < 5; i++) { grade[i] += (70 – A); cout << grade[i] << "%" << endl; } return 0; } We can save some space by combining these two loops!

42 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; cout << fixed << setprecision(1); cout << "New grades:" << endl; for (int i = 0; i < 5; i++) { grade[i] += (70 – A); cout << grade[i] << "%" << endl; } return 0; } Each of these statements is only used once in the code!

43 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[10]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 10; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 10; i++) A += grade[i]; A /= 10.0; cout << fixed << setprecision(1); cout << "New grades:" << endl; for (int i = 0; i < 10; i++) { grade[i] += (70 – A); cout << grade[i] << "%" << endl; } return 0; } Adjusting the code to accommodate 10 students requires changing the array size!

44 Good programming practice: try to avoid magic numbers in your code numbers whose significance may not be obvious use a const parameter instead this applies to the size of the arrays this is exemplified by the last slide use a const integral value for your array sizes

45 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { const int NUM_STUDENTS = 10; double grade[NUM_STUDENTS]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < NUM_STUDENTS; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < NUM_STUDENTS; i++) A += grade[i]; A /= NUM_STUDENTS; cout << fixed << setprecision(1); cout << "New grades:" << endl; for (int i = 0; i < NUM_STUDENTS; i++) { grade[i] += (70 – A); cout << grade[i] << "%" << endl; } return 0; }

46 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { const int NUM_STUDENTS = 1000; double grade[NUM_STUDENTS]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < NUM_STUDENTS; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < NUM_STUDENTS; i++) A += grade[i]; A /= NUM_STUDENTS; cout << fixed << setprecision(1); cout << "New grades:" << endl; for (int i = 0; i < NUM_STUDENTS; i++) { grade[i] += (70 – A); cout << grade[i] << "%" << endl; } return 0; } Adjusting the code to accommodate 10000 students requires changing one variable value minimal effect on code length

47 Arrays: Common Programming Errors off by 1 (run-time error) using indices beginning at 1 int x[10]; for (int i = 1; i <= 10; i++) { cout << x[i]; }

48 Arrays: Common Programming Errors using a non const value for size (compiler error) int size = 6; int x[size]; for (int i = 0; i < size; i++) { cout << x[i]; }

49 Arrays: Common Programming Errors going out of bounds (runtime error) C++ does not do bounds checking int x[10]; for (int i = 0; i <= 10; i++) { cout << x[i]; }

50 Array Initialization when declaring an atomic variable, we had the option to initialize to a value: int x = 10; we can do the same with arrays, using braces int x[5] = {1, 2, 3, 4, 5}; x[0]x[1]x[2]x[3] x[4]

51 Array Initializers if you do not include enough initializers, fills the rest with 0 int x[5] = {1,2,3}; // x[3] and x[4] get value 0 if you include too many initializers, compiler error int x[5] = {1,2,3,4,5,6}; // won't compile when using initializers, can exclude the size of the array altogether C++ uses size of initializer list int x[ ] = {1,2,3,4,5}; // same as x[5] = {1,2,3,4,5};

52 Arrays and Operators as mentioned, the elements of an array can be used wherever a variable can int i; int a[10]; cout << i << " " << a[4] << endl; cout << 3 + i << " " << 3 + a[4] << endl;

53 Arrays and Operators the array however, cannot be used just like a variable some common errors: 1) You cannot output all the elements of an array with one redirect* int a[3]; a[0] = a[1] = a[2] = 4; cout << a << endl;

54 Arrays and Operators the array however, cannot be used just like a variable some common errors: 2) You cannot input all the elements of an array with one redirect* int a[3]; cin >> a; cout << a[0] << endl;

55 Arrays and Operators the array however, cannot be used just like a variable some common errors: 3) You cannot assign the elements of one array to another array with one assignment int a[3]; a[0] = a[1] = a[2] = 4; int b[3]; b = a; cout << b[0] << endl;

56 Arrays and Operators the array however, cannot be used just like a variable some common errors: 4) You cannot compare two arrays with one == operator int a[3] = {4, 4, 4}; int b[3] = {4, 4, 4}; if (a == b) cout << "True" << endl; else cout << "False" << endl;

57 Arrays and Operators as a rule, you typically have to manually perform operations on each element of an array* 1) To output all values: int a[3]; a[0] = a[1] = a[2] = 4; cout << a[0] << endl; cout << a[1] << endl; cout << a[2] << endl; or int a[3]; a[0] = a[1] = a[2] = 4; for (int i = 0; i < 3; i++) cout << a[i] << endl;

58 Arrays and Operators as a rule, you typically have to manually perform operations on each element of an array* 2) To input all values: int a[3]; cin >> a[0] >> a[1] >> a[2]; or int a[3]; for (int i = 0; i < 3; i++) cin >> a[i];

59 Arrays and Sizes once you declare the size of an array, you cannot resize it this can be inconvenient, especially if you don't know how much data you need when the amount of incoming data is not known, an array is typically declared to be larger than necessary this typically requires a second integer, for storing the number of values in the array example: write a program to read in a list of numbers from a user, and print out these numbers in reverse. The user will indicate the end of the numbers by entering a 0. int a[3]; // a has three integers in it // for the rest of its lifetime

60

61 1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { const int MAX_SIZE = 512; int numbers[MAX_SIZE]; int length = 0; int input; cout << "Please enter up to 512 numbers, 0 to stop: "; cin >> input; while ( (length < 512) && (input != 0)) { numbers[length] = input; length++; cin >> input; } for (int i = length-1; i >= 0; i--) cout << numbers[i] << endl; return 0; }

62 Arrays as Function Arguments Array elements can be passed by value only a copy is sent void swap(int a, int b) { int temp = a; a = b; b = temp; } int main() { int x[3] = {0, 1, 2}; swap(x[0], x[1]); cout << x[0] << ","; cout << x[1] << ","; cout << x[2] << endl; } Output: 0,1,2

63 Arrays as Function Arguments Array elements can be passed to reference parameters void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main() { int x[3] = {0, 1, 2}; swap(x[0], x[1]); cout << x[0] << ","; cout << x[1] << ","; cout << x[2] << endl; } Output: 1,0,2

64 Passing Arrays as Parameters an entire array can be passed as a parameter Example: Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers

65 #include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[5] = {1,2,3,4,5}; cout << "Sum = " << sum(x) << endl; int y[5] = {5, 10, 15, 20, 25}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers

66 #include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[5] = {1,2,3,4,5}; cout << "Sum = " << sum(x) << endl; int y[5] = {5, 10, 15, 20, 25}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers

67 #include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[5] = {1,2,3,4,5}; cout << "Sum = " << sum(x) << endl; int y[5] = {5, 10, 15, 20, 25}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers

68 #include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[5] = {1,2,3,4,5}; cout << "Sum = " << sum(x) << endl; int y[5] = {5, 10, 15, 20, 25}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers.

69 #include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[5] = {1,2,3,4,5}; cout << "Sum = " << sum(x) << endl; int y[5] = {5, 10, 15, 20, 25}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers. Our summation code goes here: Add each element in the array to result.

70 #include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[5] = {1,2,3,4,5}; cout << "Sum = " << sum(x) << endl; int y[5] = {5, 10, 15, 20, 25}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers.

71 #include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[5] = {1,2,3,4,5}; cout << "Sum = " << sum(x) << endl; int y[5] = {5, 10, 15, 20, 25}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers. Note: When sending the array variable as an argument, no parentheses are needed!

72

73 Size element in array parameter the size element in the array parameter has no consequence on the code

74 #include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[6] = {1,2,3,4,5,6}; cout << "Sum = " << sum(x) << endl; int y[7] = {5, 10, 15, 20, 25, 30, 35}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers. Note: The size of these arrays are not 5, but 6 and 7.

75

76 Size element in array parameter the size element in the array parameter has no consequence on the code in fact, the size of the array can be omitted completely

77 #include using namespace std; int sum(int a[]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[6] = {1,2,3,4,5,6}; cout << "Sum = " << sum(x) << endl; int y[7] = {5, 10, 15, 20, 25, 30, 35}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers. Note: Size of array not specified!

78 The last example works fine, but is slightly limited suppose we want to add the first n numbers in our array, where n != 5 Solution: build more functions? inefficient send a second parameter!

79 #include using namespace std; int sum(int a[], int size) { int result = 0; for (int i = 0; i < size; i++) { result += a[i]; } return result; } int main() { int x[6] = {1,2,3,4,5,6}; cout << "Sum = " << sum(x, 6) << endl; int y[7] = {5, 10, 15, 20, 25, 30, 35}; cout << "Sum = " << sum(y, 7) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers. Note: Size of array specified as second parameter.

80

81 In C/C++, when a function takes an array as input, it very often takes in the size of that array as well exception: strings Hence, the format of your functions with array parameters should typically look like previous example: int sum(int a[], int size) { int result = 0; for (int i = 0; i < size; i++) { result += a[i]; } return result; }

82 Arrays and Memory arrays are passed BY REFERENCE no copy of the array is made only the address of the array is passed no reference indicator (&) is required on the parameter consequently, all changes to the array made in the function are permanent, not local Example: write a function that adds 5 to each value of an array

83 #include using namespace std; void add5(int a[], int size) { for (int i = 0; i < size; i++) { a[i] += 5; } int main() { int x[3] = {0, 3, 6}; cout << x[0] << "," << x[1] << "," << x[2] << endl; add5(x, 3); cout << x[0] << "," << x[1] << "," << x[2] << endl; return 0; } Example: write a function that adds 5 to each value of an array

84 #include using namespace std; void add5(int a[], int size) { for (int i = 0; i < size; i++) { a[i] += 5; } int main() { int x[3] = {0, 3, 6}; cout << x[0] << "," << x[1] << "," << x[2] << endl; add5(x, 3); cout << x[0] << "," << x[1] << "," << x[2] << endl; return 0; } Example: write a function that adds 5 to each value of an array

85

86 Const Parameters suppose I have an array of important information e.g. a list of student grades suppose that I want to calculate the sum of all of those grades e.g. for computing an average what if I use the sum function?

87 #include using namespace std; int sum(int a[], int size) { int result = 0; for (int i = 0; i < size; i++) { result += a[i]; } return result; } int main() { int marks = {87, 62, 49, 71, 55}; cout << "Average = " << sum(marks, 5) / 5.0 << endl; return 0; } Example: write a function that adds 5 to each value of an array

88

89 Previous example works fine but what if you couldn't see the code for sum?

90 #include using namespace std; /* this function computes the sum of the first size elements in the array a */ int sum(int a[], int size); int main() { int marks = {87, 62, 49, 71, 55}; cout << "Average = " << sum(marks, 5) / 5.0 << endl; return 0; } // the code for sum is somewhere down here Example: write a function that adds 5 to each value of an array

91 Problem with the previous code remember that arrays are passed by reference this means that the function can change the values in the array is this safe? for information that you do not want changed (such as a list of grades), not really how do we safeguard against this? Solution 1: check the source code of the called function

92 #include using namespace std; /* this function computes the sum of the first size elements in the array a */ int sum(int a[], int size); int main() { int marks = {87, 62, 49, 71, 55}; cout << "Average = " << sum(marks, 5) / 5.0 << endl; return 0; } // the code for sum is somewhere down here int sum(int a[], int size) { int result = 0; for (int i = 0; i < size; i++) { result += a[i]; } return result; } Example: write a function that adds 5 to each value of an array Programmer has to manually check the sum code to make sure no changes to a occur.

93 Problems with this approach time-consuming what if the function had 500 lines of code error-prone what if programmer overlooked a line difficult sometimes, code is stored in a totally separate header file

94 Solution: const parameters prepend an array parameter declaration with the keyword const when an array parameter is declared const, then the function is forbidden from changing the values in that array int sum(const int a[], int size) { int result = 0; for (int i = 0; i < size; i++) { result += a[i]; } return result; }

95 What happens if the function tries to change a value in a const array? int sum(const int a[], int size) { int result = 0; for (int i = 0; i < size; i++) { result += a[i]++; } return result; }

96 #include using namespace std; /* this function computes the sum of the first size elements in the array a */ int sum(const int a[], int size); int main() { int marks = {87, 62, 49, 71, 55}; cout << "Average = " << sum(marks, 5) / 5.0 << endl; return 0; } // the code for sum is somewhere down here Example: write a function that adds 5 to each value of an array Programmer can call sum with total confidence that the array will not be altered, without even knowing what the source code of sum looks like.

97 When should you declare an array parameter const? whenever the function is not going to change any values of the array two purposes: 1) guarantees other programmers using your function that no changes will occur 2) good error check for yourself if you accidentally write code that changes the array, compiler will alert you

98 Example: write a function that returns the maximum value of a set of integers int sum(int a[], int size) { int biggest = a[0]; for (int i = 1; i < size; i++) if (a[i] > biggest) biggest = a[i]; return biggest; } does this function change any values in a? no therefore, we should declare it const

99 Example: write a function that returns the maximum value of a set of integers int maximum(const int a[], int size) { int biggest = a[0]; for (int i = 1; i < size; i++) if (a[i] > biggest) biggest = a[i]; return biggest; } does this function change any values in a? no therefore, we should declare it const

100 Example: write a function that sets all negative values to positive in a function void sum(int a[], int size) { for (int i = 0; i < size; i++) a[i] = abs(a[i]); } does this function change any values in a? yes therefore, we should leave it as non-const

101 Const Reference Parameters we can also declare const reference parameters this guarantees that we will not make any changes to the parameters in the calling function this is useful when we want to send data to the function, but do not want to make copies e.g. for strings

102 #include using namespace std; void bio(string &name, string &city, string &province); int main() { string name = "Kevin"; string city = "Lethbridge"; string province = "Alberta"; bio(name, city, province); return 0; } void bio(string &name, string &city, string &province) { cout << name << " currently lives in " << city << ", " << province << endl; } write a function that takes a name, city, and province as inputs, and writes a short bio of the person. No guarantee function will not alter strings, unless function code is checked.

103 #include using namespace std; void bio(const string &name, const string &city, const string &province); int main() { string name = "Kevin"; string city = "Lethbridge"; string province = "Alberta"; bio(name, city, province); return 0; } void bio(const string &name, const string &city, const string &province) { cout << name << " currently lives in " << city << ", " << province << endl; } write a function that takes a name, city, and province as inputs, and writes a short bio of the person. Function is not allowed to change the const values.

104 When should you declare reference parameters const? whenever the function is not going to change their values const reference parameters are often used for large data types strings structures/classes (stay tuned!!)

105 void swap(int &a, int &b) { int temp = b; b = a; a = temp; } does this function change its reference parameters? yes therefore, they cannot be const


Download ppt "Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the."

Similar presentations


Ads by Google