0% found this document useful (0 votes)
18 views

Lecture 06

The document discusses arrays in C++. Arrays allow storing multiple values of the same data type. The document explains that declaring multiple variables becomes inefficient for large amounts of data, whereas arrays provide a more compact way to store data using a single variable name. It then defines arrays as collections of a fixed number of components of the same data type. The document shows how to declare and initialize arrays, access elements, and perform common operations like finding the largest/smallest element using loops to iterate through the array.

Uploaded by

aw.aw.free321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lecture 06

The document discusses arrays in C++. Arrays allow storing multiple values of the same data type. The document explains that declaring multiple variables becomes inefficient for large amounts of data, whereas arrays provide a more compact way to store data using a single variable name. It then defines arrays as collections of a fixed number of components of the same data type. The document shows how to declare and initialize arrays, access elements, and perform common operations like finding the largest/smallest element using loops to iterate through the array.

Uploaded by

aw.aw.free321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Arrays

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.

Next, consider the following statements:


list[3] = 10;
list[6] = 35;
list[5] = list[3] + list[6];
You can also declare arrays as follows:
const int ARRAY_SIZE = 10;
int list[ARRAY_SIZE];
That is, you can first declare a named constant and then use the value of the named
constant to declare an array and specify its size.
Processing One-Dimensional Arrays
Some of the basic operations performed on a one-dimensional array are initializing,
inputting data, outputting data stored in an array, and finding the largest and/or
smallest element. Moreover, if the data is numeric, some other basic operations are
finding the sum and average of the elements of the array. Each of these operations
requires the ability to step through the elements of the array. This is easily
accomplished using a loop. For example, suppose that we have the following
statements:
e. Largest element 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];

You might also like