Array question

I just started reading about Arrays and I'm a bit confused about the "i" part of it. So i starts at 0(lowest value of an array?.) So it will keep adding 1 to i, until it's the same as size? And when you input the grades[i] it will represent the corresponding value and add it to the total sum?

Why isn't it returning the sum/size right away after the first i, since it says return sum/size right below it?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Example program
#include <iostream>
#include <string>
double average (int grades[], int size);
int main()
{
   int grades[]={1,3,4,2,33,4,2,5};
std::cout<<average(grades,8);
}
double average (int grades[], int size){
    double sum = 0.0;
    for (int i = 0; i < size; i++)
        sum += grades[i];
    return sum/size;
}
}
Last edited on
You should remove the last curly bracket and it'll work fine..........

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
double average (int grades[], int size);
int main()
{
   int grades[]={1,3,4,2,33,4,2,5};
std::cout<<average(grades,8);
}

double average (int grades[], int size){
    double sum = 0.0;
    for (int i = 0; i < size; i++)
        sum += grades[i];
    return sum/size;
}
The code works(without the last bracket), my question was about the logic behind it though. I think I get it now anyways.
The reason i starts at 0 is because arrays first "index" is 0. I think of the "index" more of an "offset" from the first address. Basically a for loop is like thisfor(initalize; condition; increment) The reason it doesn't return after the first iteration is because the loop does not end until it reaches the break condition. This might help explain better
http://www.cplusplus.com/doc/tutorial/control/

The for loop
The for loop is designed to iterate a number of times. Its syntax is:

for (initialization; condition; increase) statement;

Like the while-loop, this loop repeats statement while condition is true. But, in addition, the for loop provides specific locations to contain an initialization and an increase expression, executed before the loop begins the first time, and after each iteration, respectively. Therefore, it is especially useful to use counter variables as condition.

It works in the following way:

initialization is executed. Generally, this declares a counter variable, and sets it to some initial value. This is executed a single time, at the beginning of the loop.
condition is checked. If it is true, the loop continues; otherwise, the loop ends, and statement is skipped, going directly to step 5.
statement is executed. As usual, it can be either a single statement or a block enclosed in curly braces { }.
increase is executed, and the loop gets back to step 2.
the loop ends: execution continues by the next statement after it.

Here is the countdown example using a for loop:

1
2
3
4
5
6
7
8
9
10
11
// countdown using a for loop
#include <iostream>
using namespace std;

int main ()
{
  for (int n=10; n>0; n--) {
    cout << n << ", ";
  }
  cout << "liftoff!\n";
}
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!



The three fields in a for-loop are optional. They can be left empty, but in all cases the semicolon signs between them are required. For example, for (;n<10;) is a loop without initialization or increase (equivalent to a while-loop); and for (;n<10;++n) is a loop with increase, but no initialization (maybe because the variable was already initialized before the loop). A loop with no condition is equivalent to a loop with true as condition (i.e., an infinite loop).

Because each of the fields is executed in a particular time in the life cycle of a loop, it may be useful to execute more than a single expression as any of initialization, condition, or statement. Unfortunately, these are not statements, but rather, simple expressions, and thus cannot be replaced by a block. As expressions, they can, however, make use of the comma operator (,): This operator is an expression separator, and can separate multiple expressions where only one is generally expected. For example, using it, it would be possible for a for loop to handle two counter variables, initializing and increasing both:

1
2
3
4
for ( n=0, i=100 ; n!=i ; ++n, --i )
{
   // whatever here...
}



This loop will execute 50 times if neither n or i are modified within the loop:

http://www.cplusplus.com/doc/tutorial/control/for_loop.png

n starts with a value of 0, and i with 100, the condition is n!=i (i.e., that n is not equal to i). Because n is increased by one, and i decreased by one on each iteration, the loop's condition will become false after the 50th iteration, when both n and i are equal to 50.
Thanks for the detailed reply. The resource I'm studying taught arrays before for loops, yet used for loops to teach arrays. I understand it a bit better now.
Topic archived. No new replies allowed.