Beginner question about arrays

I just have one question about the syntax of arrays. In the book, they gave me the exercise of having to get the largest and smallest element/value from an array. In both of the for loops for calculating the smallest and largest number there is the variable minIndex and maxIndex and at the same time I have those same variables on different lines surrounded by array subscripting operators. My question is what is the difference between maxIndex and sales[maxIndex].......or minIndex and sales[minIndex}. When I went to output maxIndex and sales[maxIndex], they both output the same exact thing. What's the difference?

Line 36: maxIndex
Line 37: sales[maxIndex]

Line: 44: minIndex
Line 45: sales[minIndex]





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>  //Practicing Arrays
#include <iomanip>
using namespace std;

int main(){

    double sales[10];
    int index, maxIndex =0, minIndex =0;
    double largestSale, smallestSale, sum=0, average;

    cout << "Enter ten numbers: " << endl << endl;
    for (index=0; index < 10; index++)          //Loop to enter the numbers
        {
            cin >> sales[index];
        }

cout << "\nThe Ten Numbers: ";
for (index =0; index < 10; index++)             //Loop to output numbers
    cout << sales[index] << " ";

for (index =0; index < 10; index++)             //Loop to get total
    sum += sales[index];

    average = sum / 10;
    cout << "\nSum: " << sum << endl;           //Sum
    cout << "Average: " << average << endl;     //Average

cout << "The numbers backwards are: ";          //Backwards numbers
for (index =9; index >= 0; index--)
    cout << sales[index] << " ";
    cout << endl;


/* here are the loops*/
for (index =0; index <10;index++)               //Calculating for the largest of numbers
    {
        if(sales[maxIndex] < sales[index])
        maxIndex = index;
        largestSale = sales[maxIndex];
    }
    cout << "Largest Number: " << largestSale << endl;

for (index=0; index <10; index++)               //Calculating for the smallest of numbers
    {
        if(sales[minIndex] > sales[index])
        minIndex = index;
        smallestSale = sales[minIndex];
    }
    cout << "Smallest Number: " << smallestSale << endl;
}


I know there is a difference because when I tried to equal maxIndex to largestSale, the largest number is ALWAYS the second number before the last
The variables "minIndex" and "maxIndex" are stand ins for integers, so those are turned into numbers when you code is run. When you see stuff like this just thing "This is supposed to be the number that variable currently represents."
maxIndex stores the actual value of the index that that the largest value in the array resides. Therefore, it's a value anywhere from 0-n where n is the size of the array - 1. To get the actual largest value, sales[maxIndex] will return the actual value that is the largest in that array. The same for minIndex, but for the smallest.

I hope that makes sense.
Ok thanks a lot guys. Makes sense.
Topic archived. No new replies allowed.