Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This example shows how loops are used to process arrays. The following declarati

ID: 3771323 • Letter: T

Question

This example shows how loops are used to process arrays. The following declaration is

used throughout this example:

double sales[10];

int index;

double largestSale, sum, average;

The first statement declares an array sales of 10 components, with each component

being of type double. The meaning of the other statements is clear.

a. Initializing an array: The following loop initializes every component

of the array sales to 0.0.

for (index = 0; index < 10; index++)

sales[index] = 0.0;

b. Reading data into an array: The following loop inputs the data

into the array sales. For simplicity, we assume that the data is

entered from the keyboard.

for (index = 0; index < 10; index++)

cin >> sales[index];

c. Printing an array: The following loop outputs the array sales.

For simplicity, we assume that the output goes to the screen.

for (index = 0; index < 10; index++)

cout << sales[index] << " ";

d. Finding the sum and average of an array: Because the array

sales, as its name implies, represents certain sales data, it is natural

to find the total sale and average sale amounts. The following C++

code finds the sum of the elements of the array sales and the

average sale amount:

sum = 0;

for (index = 0; index < 10; index++)

sum = sum + sales[index];

average = sum / 10;

e. Largest element in the array: We now discuss the algorithm to

find the first occurrence of the largest element in an array—that is, the

first array component with the largest value. However, in general, the

user is more interested in determining the location of the largest

element in the array. Of course, if you know the location (that is,

the index of the largest element in the array), you can easily determine

the value of the largest element in the array. So let us describe the

algorithm to determine the index of the first occurrence of the largest

element in an array—in particular, the index of the largest sale amount

in the array sales. We will use the index of the first occurrence of the

largest element in the array to find the largest sale.

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];

Explanation / Answer

#include <iostream>
using namespace std;

int main()
{
double sales[10];
int index,maxIndex;
double largestSale, sum, average;

cout<<"Initializing array values ";
for(index = 0;index<10;index++)
sales[index] = 0.0;
cout<<"Initialisation complete ";

cout<<"Taking input ";
for(index = 0;index < 10;index ++ )
cin>>sales[index];

cout<<"Display of array on screeen ";
for(index = 0;index < 10;index ++ )
cout<<sales[index]<<" ";

sum = 0;
for(index = 0;index < 10;index ++ )
sum = sum + sales[index];
average = sum/10;
cout<<" Average sale = "<<average<<endl;

maxIndex = 0;
for(index = 0;index < 10;index ++ )
if(sales[maxIndex] < sales[index])
maxIndex = index;

largestSale = sales[maxIndex];
cout<<"Largest sale = "<<largestSale<<" at index "<<maxIndex<<endl;

return 1;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote