Lecture 06
Lecture 06
Recall that a data type is called simple if variables of that type can store only one value at
a time. In contrast, in a structured data type, each data item is a collection of other data
items. Simple data types are building blocks of structured data types. The first structured
data type that we will discuss is an array.
Before formally defining an array, let us consider the following problem. We want to write a
C++ program that reads five numbers, finds their sum, and prints the numbers in reverse order.
In previous lectures , you learned how to read numbers, print them, and find the sum. The
difference here is that we want to print the numbers in reverse order. This means we cannot print
the first four numbers until we have printed the fifth, and so on. To do this, we need to store all
of the numbers before we start printing them in reverse order. From what we have learned so far,
the following program accomplishes this task
#include <iostream>
using namespace std;
int main()
{
int item0, item1, item2, item3, item4;
int sum;
cout << "Enter five integers: ";
cin >> item0 >> item1 >> item2 >> item3 >> item4;
cout << endl;
sum = item0 + item1 + item2 + item3 + item4;
cout << "The sum of the numbers = " << sum << endl;
cout << "The numbers in the reverse order are: ";
cout << item4 << " " << item3 << " " << item2 << " “ << item1 << " " << item0 << endl;
return 0; }
This program works fine. However, if you need to read 100 (or more) numbers and print them in reverse
order, you would have to declare 100 variables and write many cin and cout statements. Thus, for large
amounts of data, this type of program is not desirable.
An array is a collection of a fixed number of components all of the same data type. A
one-dimensional array is an array in which the components are arranged in a list form.
This section discusses only one-dimensional arrays. Arrays of two dimensions or more are
discussed later.
The general form for declaring a one-dimensional array is:
Suppose i is an int variable. Then, the assignment statement:
list[3] = 63;
is equivalent to the assignment statements:
i = 3;
list[i] = 63;
If i is 4, then the assignment statement:
list[2 * i - 3] = 58;
stores 58 in list[5] because 2 * i - 3 evaluates to 5. The index expression is evaluated first, giving the position
of the component in the array.
We assume that maxIndex will contain the index of the first occurence of the
largest element in the array sales. The general algorithm
is straightforward. Initially, we assume that the first element in the list is
the largest element, so maxIndex is initialized to 0. We then compare
the element pointed to by maxIndex with every subsequent element
in the list. Whenever we find an element in the array larger than the
element pointed to by maxIndex, we update maxIndex so that it
points to the new larger element. The algorithm is as follows:
maxIndex = 0;
for (index = 1; index < 10; index++)
if (sales[maxIndex] < sales[index])
maxIndex = index;
largestSale = sales[maxIndex];