Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that...
-
date post
21-Dec-2015 -
Category
Documents
-
view
219 -
download
0
Transcript of Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that...
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 grades
2. calculate the average A
3. add (70 – A) to each grade
4. re-output the new grades to one decimal place
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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;}
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 4. Re-output the grades#include <iostream>#include <iomanip> 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 4. Re-output the grades#include <iostream>#include <iomanip> 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;}
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
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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;}
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?
#include <iostream>#include <iomanip> 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);
...
Arraysan aggregate data structure
vs. atomic data types (int, float, double, char, …)a group of variables of the same type
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*
How to access the elements in an arrayuse the subscript operator [ ] indices START AT 0, NOT 1value in the subscripts need not be a
constant
int x[3];
x[0] = 5; // sets the first element in the array to value 5x[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
The elements of an array are used exactly like a single variablecan 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];
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]}
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;}
Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%.
Solution: read in the gradescalculate the average Aadd (70 – A) to each grade re-output the new grades
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 thisexample usingarrays!
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 theseto an array
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 theseto an array
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 eachelement 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
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 eachelement 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
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 eachelement 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
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 eachelement 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
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 eachelement 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
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 eachelement in the array toA.
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 eachelement in the array toA.
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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.
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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.
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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.
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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.
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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.
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 spaceby combining these twoloops!
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 spaceby combining these twoloops!
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 statementsis only used once in the code!
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 codeto accommodate 10students requires changingthe array size!
Good programming practice: try to avoid magic numbers in your code
numbers whose significance may not be obvioususe a const parameter instead this applies to the size of the arrays
this is exemplified by the last slideuse a const integral value for your array
sizes
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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;}
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream>#include <iomanip> 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 codeto accommodate 10000students requires changingone variable value• minimal effect on code length
Arrays: Common Programming Errorsoff by 1 (run-time error)
using indices beginning at 1
int x[10];
for (int i = 1; i <= 10; i++) { cout << x[i];}
Arrays: Common Programming Errorsusing a non const value for size (compiler
error)
int size = 6;
int x[size];
for (int i = 0; i < size; i++) { cout << x[i];}
Arrays: Common Programming Errorsgoing out of bounds (runtime error)
C++ does not do bounds checking
int x[10];
for (int i = 0; i <= 10; i++) { cout << x[i];}
Array Initializationwhen 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]
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};
Arrays and Operatorsas mentioned, the elements of an array can
be used wherever a variable canint i;
int a[10];
cout << i << " " << a[4] << endl;
cout << 3 + i << " " << 3 + a[4] << endl;
Arrays and Operators the array however, cannot be used just like
a variablesome 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;
Arrays and Operators the array however, cannot be used just like
a variablesome common errors:
2) You cannot input all the elements of an array with one redirect*
int a[3];cin >> a;cout << a[0] << endl;
Arrays and Operators the array however, cannot be used just like
a variablesome 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;
Arrays and Operators the array however, cannot be used just like
a variablesome 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;
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;
Arrays and Operatorsas 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];
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
1. Read in the grades 2. Calculate the average A
3. Add (70 – A) to each grade 4. Re-output the grades#include <iostream> 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;
}
Arrays as Function Arguments
Array elements can be passed by valueonly 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
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
Passing Arrays as Parametersan entire array can be passed as a
parameterExample: Write a program with a function
called sum that takes an array of 5 integers, and returns the sum of those integers
#include <iostream> 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
#include <iostream> 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
#include <iostream> 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
#include <iostream> 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.
#include <iostream> 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.
#include <iostream> 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.
#include <iostream> 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!
Size element in array parameter the size element in the array parameter has
no consequence on the code
#include <iostream> 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.
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
#include <iostream> 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!
The last example works fine, but is slightly limitedsuppose we want to add the first n numbers
in our array, where n != 5Solution:
build more functions? inefficient
send a second parameter!
#include <iostream> 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.
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;
}
Arrays and Memoryarrays are passed BY REFERENCE
no copy of the array is madeonly the address of the array is passed no reference indicator (&) is required on the
parameterconsequently, all changes to the array made
in the function are permanent, not localExample: write a function that adds 5 to
each value of an array
#include <iostream> 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
#include <iostream> 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
Const Parameterssuppose I have an array of important
informatione.g. a list of student grades
suppose that I want to calculate the sum of all of those grades
e.g. for computing an averagewhat if I use the sum function?
#include <iostream> 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
Previous example works finebut what if you couldn't see the code for
sum?
#include <iostream> 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
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
#include <iostream> 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 hereint 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.
Problems with this approach time-consuming
what if the function had 500 lines of codeerror-prone
what if programmer overlooked a linedifficult
sometimes, code is stored in a totally separate header file
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;
}
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;
}
#include <iostream> 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.
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
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
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
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
Const Reference Parameterswe 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
#include <iostream>#include <string> 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.
#include <iostream>#include <string> 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.
When should you declare reference parameters const?whenever the function is not going to
change their valuesconst reference parameters are often used
for large data typesstringsstructures/classes (stay tuned!!)
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