Click to See Complete Forum and Search --> : General Array Question


Evrardo
June 4th, 2002, 07:31 PM
I wrote a simple program to find out some things about arrays. I found out what I needed to know, except when I cout the array, it gives me some weird numbers at the very end. I was just wondering if there is a problem with my code or what I can do to get rid of it. here's the code:

____________________________________________________

#include <iostream.h>

const int SIZE=10;

void main()
{
int array[SIZE]={0};
int value, num1, i;

cout <<"Enter the section of the array:\n";
cin >>value;

cout <<"Enter the number to go into that section:\n";
cin >>num1;

array[value]=num1;

for(i=0; i<=SIZE; i++)
{
cout <<array[i] <<endl;
}
}

prosh0t
June 4th, 2002, 07:49 PM
for(i=0; i<SIZE; i++)
because for array of size 10 it used #'s 0-9 as index's, not 0-10: that would be an array of size 11

zach
June 5th, 2002, 10:45 AM
Since C/C++ does not perform any bounds checking, the compiler does not complain about you trying to access an element which lies outside your array. The for-loop goes one step to far and gets a memory location behind your array. What is found there is interpreted as integer but can be anything.