9/1/2024
Examples With Arrays
Review
• An array makes it possible to store and manipulate a collection of related data as
if it were one unit.
• Each elements of an array has the same data type.
1
9/1/2024
Parts of an Array
Array name
Array locations
0 1 2 3 4 5 6 7 8 9
marks
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]
Array elements
Declaring Arrays
Indicate that an array is being declared
Name of the array
int marks [ 10 ] ;
Data type of elements
being stored in the array Number of
elements in the
array
2
9/1/2024
Assigning Values
• Assignment Operator
marks [0] = 85; 0 1 2 3 4 5 6 7 8 9
85
82.5
marks
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]
int bonus = 10;
marks [0] = 75 + (75 * (bonus/100.0));
• A for loop can be used to generate all the locations from 0 to 9 in increments of 1:
for (i=0; i<10; i=i+1) { 0 1 2 3 4 5 6 7 8 9
marks [i] = 0; marks 0 0 0 0 0 0 0 0 0 0
} marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]
Assigning Values
• From the keyboard using cin
cin >> marks [3];
for (i=0; i<10; i=i+1) {
cout << "Please enter mark for student " << i << " ";
cin >> marks[i];
}
3
9/1/2024
Displaying Values in an Array
• To display the value of a particular element in an array
cout << marks [5] << endl;
• To display the values of all the elements in the array, a for loop can be used to
generate the array subscripts from 0 to 9.
for (i=0; i<10; i=i+1) {
cout << marks[i] << endl;
}
• To display the elements in reverse order
for (i=9; i>=0; i=i-1) {
cout << marks[i] << endl;
}
Arrays and Arithmetic Expressions
highest = marks [0]; 0 1 2 3 4 5 6 7 8 9
12 24 30 56 89 74 44 63 98 1
marks
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]
marks [2] = marks [2] + 5;
for (i = 0; i < 10; i = i + 1) {
marks [i] = marks [i] + 5;
}
4
9/1/2024
Assigning Values: From a File
• Suppose the text file, [Link], contains the marks of the 10 students enrolled in
the course.
• An ifstream object must be declared and connected to the file.
ifstream inputFile;
[Link]("[Link]");
• A value can be read from the file and assigned to an array element such as
marks [3] as follows:
inputFile >> marks[3];
Assigning Values: Using File Input
• To assign values to all the array elements, the values can be read from the file and
assigned to the array elements using a for loop:
for (i=0; i<10; i=i+1) {
inputFile >> marks[i];
}
[Link]();
5
9/1/2024
Writing the Value of an Array Element to a
File
• Suppose that we would like to store the value of element 3 of the marks array in
a text file, [Link].
• An ofstream object must be declared and connected to the file as follows:
ofstream outputFile;
[Link]("[Link]");
• Once the file is opened, the values of marks [3] can be written to the file as
follows:
outputFile << marks [3] << endl;
Writing the Values of All the Array Elements
to a File
• To write the values of all the array elements to the file, a for loop can be used to
generate the subscripts from 0 to 9. In each pass of the for loop, the array
element identified by the subscript is written to the file:
for (i=0; i<10; i=i+1) {
outputFile << marks[i] << endl;
}
[Link]();
6
9/1/2024
Traversing an Array
• The act of generating the array subscripts from 0 to 9 and then performing some
operation on the array element with that subscript is referred to as traversing the
array.
• For example, the code to display all the elements of the marks array
for (i=0; i<10; i=i+1) {
cout << marks [i] << endl;
}
Searching an Array for a Value
• When searching for a value, the term key is often used to refer to the value being
searched for.
• The array can be searched by checking each element one by one to see if it is
equal to the key.
• If we come to the last element and the key has not been found, then it can be
concluded that the array does not contain the key.
0 1 2 3 4 5 6 7 8 9
marks 74 91 87 65 79 95 84 81 56 75
7
9/1/2024
Code Snippet: Searching for a Value
int key;
bool found;
key = 95;
found = false; //found must be set to false, before entering the loop.
for (i=0; i<10; i=i+1) {
if (marks[i] == key) {
/* If an array element is equal to the key, the Boolean variable, found, is
found = true;
set to true. */
}
}
if (found == true)
cout << "The value " << key << " was found in the array. " << endl;
else
cout << "The value " << key << " was not found in the array. " << endl;
Searching for a Value
• Instead of using the Boolean variable found, it is possible to place a cout
statement within the inner if statement to indicate that the key was found:
for (i=0; i<10; i=i+1) {
if (marks[i] == key) {
cout << "The value " << key << " was found in the array. " << endl;
}
}
• If we do this, it is difficult to indicate that the key was not found.
8
9/1/2024
Searching for a Value
• If we put the cout statement that the key was not found after the for loop, it will
always display that the key was not found, regardless of what happens inside the
loop.
for (i=0; i<10; i=i+1) {
if (marks[i] == key) {
cout << "The value " << key << " was found in the array. " << endl;
}
}
cout << "The value " << key << " was not found in the array. " << endl;
Will always be
displayed.
Searching for a Value
• If we put the cout statement as part of the if statement within the loop, it will
display found or not found for every element in the array.
for (i=0; i<10; i=i+1) {
if (marks[i] == key) {
cout << "The value " << key << " was found in the array. " << endl;
}
else{
cout << "The value " << key << " was not found in the array. " << endl;
}
}
• These problems are avoided by using the Boolean variable.
9
9/1/2024
Searching for a Value
• Suppose the array contains many elements.
• Even if the key is found early in the array, the entire array will be traversed, since
the for loop goes from the first element to the last.
• An improvement on this approach is to exit the loop as soon as the key is found
(i.e., as soon as found is set to true).
• One way to do this is to use a while loop
Code Snippet: while loop
key = 95;
found = false;
i = 0;
while ((i < 10) && (!found)) {
if (marks [i] == key) {
found = true;
}
i = i + 1;
}
if (found)
cout << "The value " << key << " was found in the array. " << endl;
else
cout << "The value " << key << " was not found in the array. " << endl;
10
9/1/2024
Searching for a Value
The while loop still traverses the
array starting from element 0
while ((i < 10) && (!found)) {
if (marks [i] == key) {
found = true;
}
i = i + 1;
Searching for a Value
The condition for staying in the loop
includes checking the value of the found
variable.
i = 0;
while ((i < 10) && (!found)) {
if (marks [i] == key) {
found = true;
}
i = i + 1;
11
9/1/2024
Searching for a Value
i = 0;
while ((i < 10) && (!found)) {
If the found variable becomes true,
if (marks [i] == key) {
!found evaluates to false,
found = true;
}
i = i + 1;
Searching for a Value
the entire condition evaluates to
false because of the &&, and the
loop will be exited.
while ((i < 10) && (!found)) {
if (marks [i] == key) {
found = true;
}
i = i + 1;
12
9/1/2024
Searching for a Value
i = 0;
When the while loop is while ((i < 10) && (!found)) {
used, we are responsible
for incrementing the loop
if (marks [i] == key) {
control variable as well as
checking that it is less found = true;
than 10. }
i = i + 1;
Searching for Array Elements that Satisfy
Certain Criteria
• It is often useful to check if the elements of the array satisfy certain criteria.
• Suppose we wish to find out how many values in the marks array are greater
than or equal to 80.
• We will need a variable, count, to keep track of the amount of values in the array that
satisfy the condition. count is set to zero, initially.
• Next, we need to traverse the array, checking each element to see if it is greater than or
equal to 80.
• If so, we add 1 to count.
13
9/1/2024
Code Snippet
int count;
count = 0;
for (i=0; i<10; i=i+1) {
if (marks [i] >= 80) {
count = count + 1;
}
cout << "Number of marks >= 80: " << count << endl;
Searching for Array Elements that Satisfy
Certain Criteria
• We can easily change the search criteria by modifying the condition of the if
statement.
if ((marks [i] >= 80) && (marks [i] <= 90)) { // all the marks between 80 and 90, inclusive.
count = count + 1;
}
14
9/1/2024
Searching for Array Elements that Satisfy
Certain Criteria
• We can also check how the elements of the array are related to each other.
• For example, we can find out how many adjacent elements in the marks array are
greater than or equal to 85
for (i=0; i<9; i=i+1) {
if ((marks [i] >= 85) && (marks [i+1] >= 85)) {
cout << marks [i] << " and " << marks [i+1] << endl;
}
}
Performing Statistical Operations on the
Elements of an Array
• A range of statistical operations can be performed on an array by traversing its
elements one by one in a for loop.
• Sum of the elements in the array
• Average of the elements in the array
• Highest value
• Location of the highest value
• Lowest value
• Location of the lowest value
15
9/1/2024
Copying Array Elements
• The elements of an array can be copied to another array using a for loop.
• Suppose that second is another integer array declared as follows:
int second [10];
• The values of the marks array can be copied to this new array as follows:
for (i=0; i<10; i=i+1) {
second [i] = marks [i];
}
Copying Array Elements
• The values from the marks array can be copied in reverse order by changing the
subscripts as follows:
for (i=0; i<10; i=i+1) {
second [9-i] = marks [i];
}
0 1 2 3 4 5 6 7 8 9
marks 74 91 87 65 79 95 84 81 56 75
0 1 2 3 4 5 6 7 8 9
second
16
9/1/2024
Arrays that are not Completely Filled
• An array may not always be filled to capacity and there may be empty locations.
• When working with arrays, it is customary to use a special integer variable to
keep track of the number of elements stored in an array.
• This number will usually be different from the size of the array since the array
may not be completely filled.
• For the marks array, let’s use a variable, numMarks, to keep track of the number
of elements stored in the array.
Arrays that are not Completely Filled
• Suppose we wish to open a text file, [Link], read the marks from the file, and
store them in the marks array.
• We do not know beforehand how many marks are in the file, but we know that
there will be no more than 10.
• Data is terminated with -1 to indicate that there are no more marks in the file.
17
9/1/2024
Code Snippet
ifstream inputFile;
int mark;
[Link] ("[Link]");
i = 0;
inputFile >> mark;
while (mark != -1) {
marks [i] = mark;
i = i + 1;
inputFile >> mark;
}
numMarks = i;
Arrays that are not Completely Filled
• If the file, [Link], only contains six integers, only six integers will be stored in
the marks array
• The value of numMarks will be 6
0 1 2 3 4 5 6 7 8 9
marks 74 91 87 65 79 95
First element, Last element, empty locations
location 0 location numMarks -1
18
9/1/2024
Arrays that are not Completely Filled
• Since we know that there are numMarks values in the marks array, whenever we
wish to perform an operation on the array, we must traverse the array from 0 to
numMarks - 1
for (i=0; i<numMarks; i=i+1) {
cout << marks [i] << endl;
}
• We do not use the size of the array (10) in the for loop.
• We use the number of elements which are stored in the array, which is
numMarks.
Arrays that are not Completely Filled
• The file [Link] may contain more values than the array marks can store.
• It is important to stop reading values from the file if there is no more space
available in the marks array.
• This can be done by exiting the loop if the number of elements already stored (i)
reaches the value of the size of the array (10, in this case).
while ((mark != -1) && (i < 10)) {
marks [i] = mark;
i = i + 1;
inputFile >> mark;
}
numMarks = i;
19