Last edited on
Arrays start at zero.
An array declared as T array[5]; has elements 0, 1, 2, 3, and 4.
Try taking out the = sign in the for loop, and the 1 should be a zero because, as helios (so annoying, before me :P) put it, arrays start at 0:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
int main(){
int i, a[5]={1,2,3,4,5},sum;
sum = 0;
for(i=0; i<5; i++)
sum+= a[i];
std::cout<<sum;
}
|
See if you can figure out what difference it makes.
DAMNIT HELIOS!
Oh and iostream.h doesn't exist. The STL uses iostream with no extension.
Furthermore main must return int. The code above prints 15, and works.
Last edited on
Arrays are zero-based. You start at 1 and read beyond the bounds of a. 19 is just one of any values you might thus receive.