|
-
June 4th, 2002, 07:31 PM
#1
General Array Question
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;
}
}
-
June 4th, 2002, 07:49 PM
#2
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
-
June 5th, 2002, 10:45 AM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|