array loading and reading

Feb 22, 2012 at 2:42pm
I am having trouble getting an array to print, correctly, what is loaded. It gives me number's that are in the 1000's. ????

any help? Here is the text:

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>

void main(void)

{
clrscr();
int I;
int g[3][3];
int opt;

for (I=0;I<=3;I++);
{
g[I][3]=1;
}

for (I=0;I<=3;I++)
{
opt=g[I][3];
cout<<opt<<" ";
}
}
Feb 22, 2012 at 2:45pm
Array size is 3. So index range is between 0 and 2.
Mar 6, 2012 at 3:45pm
To elaborate on shadow123's point,

declaring an array of size [4] allocates 4 places, and then a null terminator which tells your computer where the array ends. The places you store NUMBERS in are going to be 0 1 2 and 3, the final location, position 4, is permanently reserved for that terminator. Without it, your array would take up all the memory on your computer, and as such it can't be removed (that I know of). If you want an array that takes up any size you need, I'd recommend vectors and .pushback or std::strings
Mar 6, 2012 at 3:51pm
Programmers always count from 0. ;-)
Topic archived. No new replies allowed.