INTRO TO ARRAYS
A LECTURE FOR THE C++ COURSE
Each slide may have its own narration in an audio file. �For the explanation of any slide, click on the audio icon to start the narration.
The Professor‘s C++Course by Linda W. Friedman is licensed under a �Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
WHAT IS AN ARRAY?
An array is a finite set of ordered, homogeneous elements.
RECURSION
2
WHEN DO WE NEED ONE?
Arrays are used when it is necessary to
and to
RECURSION
3
WHAT DO WE DO WITH IT?
Some basic array operations:
RECURSION
4
TO DECLARE AN ARRAY IN C++
int X [100];
Just like any other declaration:
RECURSION
5
ACCESSING ELEMENTS IN AN ARRAY
Array elements are stored sequentially in memory. They all have the same variable name, and are referred to individually by means of subscripts in brackets, e.g.:
X[3] = 24.6;
X[2] = 4 * A + 13;
In this case, X is a one-dimensional array.
RECURSION
6
EXERCISE:
RECURSION
7
What is the value of each of the following?
Y[0] _______ Y[1] _______ X[0] _______
A _______ Y[B] _______ Z[A] _______
B _______ Y[C] _______ Z[B] _______
Y[A] ______ X[A] _______ Y[A+2] ________
GIVEN
DO
TBD
RECURSION
8
TO ACCESS ARRAY ELEMENTS
To access array elements we use the for loop.
RECURSION
9
PROBLEM: ARRAY I/O
RECURSION
10
How to do this?
Program Output:
PROBLEM: ARRAY I/O
//array1.cp
#include <iostream>
using namespace std;
int main(){
double a[4];
cout << "Enter 4 real numbers: \n";
for (int i=0; i<4; i++){
cout << i+1 << ": ";
cin >> a [i];
}//end for
cout<< "\nEnd of data entry.\n \n";
cout << "Here's what the array looks like: \n";
for (int i=0; i<4; i++){
cout << "a["<<i<<"]= " << a[i] << endl;}
return 0;
}//end main
RECURSION
11
HERE’S HOW!!
PROBLEM: ARRAY I/O
//array2.cp
#include <iostream>
using namespace std;
int main(){
const int SIZE = 4;
double a[SIZE];
cout << "Enter 4 real numbers: \n";
for (int i=0; i<SIZE; i++){
cout << i+1 << ": ";
cin >> a [i];
}//end for
cout<< "\nEnd of data entry.\n \n";
cout << "Here's what the array looks like: \n";
for (int i=0; i<SIZE; i++){
cout << "a["<<i<<"]= " << a[i] << endl;}
return 0;
}//end main
RECURSION
12
THIS WORKS TOO
PROBLEM: ARRAY I/O
//array3.cp
#include <iostream>
using namespace std;
int main(){
const int SIZE = 4;
double a[SIZE];
cout << "Enter 4 real numbers: \n";
for (int i=0; i<SIZE; i++){
cout << i+1 << ": ";
cin >> a [i];
}//end for
cout<< "\nEnd of data entry.\n \n";
cout << "Display the array elements in reverse: \n";
for (int i=SIZE-1; i>=0; i--){
cout << "a["<<i<<"]= " << a[i] << endl;}
return 0;
}//end main
RECURSION
13
REVERSING ARRAY PROCESSING
PROBLEM: MINIMUM VALUE IN ARRAY
RECURSION
14
PROBLEM: MINIMUM VALUE IN ARRAY
min = 9999; // or initialized to some other large number
for (j=0; j<SIZE; j++)
if (x[j] < min) min = x[j];
RECURSION
15
PROBLEM: MINIMUM VALUE IN ARRAY
min = x[0]; // min initialized to first array element
for (j=1; j<SIZE; j++)
if (x[j] < min) min = x[j];
RECURSION
16
PROBLEM: MINIMUM VALUE IN ARRAY
min = x[0]; // min initialized to first array element
for (j=1; j<SIZE; j++) //start comparing at x[1]
if (x[j] < min) min = x[j];
RECURSION
17
PROBLEM: MINIMUM VALUE IN ARRAY
min = 1; // what is min?
for (j=1; j<SIZE; j++)
if (x[j] < x[min]) min = j;
RECURSION
18
min tells WHERE, not WHAT the minimum is
THE VERY GRADING PROBLEM
RECURSION
19
PROBLEM: COUNTING THE VOTES
Write a program to count the votes in a local community election.
The candidates: Sam Smoothe and Gaby Gruff
We will use an integer array of 2 cells to accumulate the votes.
RECURSION
20
while (v>=0)
if (v==1) votes[1]++
else if (v==2) … … …
How about this?
PROBLEM: COUNTING THE VOTES
//votecounter.cpp
#include <iostream>
using namespace std;
int main(){
short v;
int votes[2] = {0};
cout << "Enter a 0 for Sam Smoothe OR a 1 for Gabby Gruff.\n"
<< "When you have no more votes to enter, "
<< "type a negative number.\n\n"
<< "Enter 0 or 1: ";
cin >> v;
while (v >= 0) {
votes[v]++;
cout << "Enter 0 or 1: ";
cin >> v;
}//end while
cout << "\nSam Smoothe got " << votes[0] << " votes\n";
cout << "Gabby Gruff got " << votes[1] << " votes\n";
return 0;
}//end main
RECURSION
21
Does this work?
Try it!
PROBLEM: FREQUENCIES
Can we use the vote counting idea to count random integers?
We could use an integer array with 10 cells, or frequencies for the integers 0-9.
Here’s the assignment:
RECURSION
22
ARRAYS AND FUNCTIONS
Example on next slide 🡪
RECURSION
23
ARRAYS AND FUNCTIONS
//morearray.cpp
//Reads an array of data values, prints it, and sums the values.
#include <iostream>
using namespace std;
const int SIZE = 20;
void readarray (float[], int&);
void printarray (const float[], int);
float sumarray (const float[], int);
//**************************************
int main(){
int n;
float data [SIZE] = {0.0};
readarray(data, n);
printarray(data, n);
cout << "The sum is "
<< sumarray(data, n);
return 0;
}
//**************************************
RECURSION
24
void readarray(float x[], int &n){
n = 0; // "number of numbers" in the array
char more='y';
while (more != 'n' && more != 'N'){
cout << "\nEnter a number: "; cin >> x[n];
n++;
cout << "Another number? (y or n) ";
cin >> more;
}
}
//*********************************************
float sumarray (const float x[], int n){
float sum = 0;
for (int i=0; i<n; i++) sum += x[i];
return sum;
}
//*********************************************
void printarray (const float x[], int n){
cout << "The data values are:\n";
for (int i=0; i<n; i++) cout << '\t' << x[i];
cout << endl;
}
REVIEW
RECURSION
25
What did we learn in this lecture? Plenty. Some terms to jog your memory: