# stored values in elements of array, help

• 12-09-2004
houler
stored values in elements of array, help
I wrote this to get the feel of how values are stored in the array.

when I run the program i get this:

1 is 1075160376
2 is 1
3 is 2
4 is 3
5 is 4

1 is 1
2 is 2
3 is 3
4 is 4
5 is 5

How do i solve this problem?
Code:

```#include <stdio.h> int main(void) {   int array[5];   int a, num;   for(a = 1; a <= 5; a++)   {       printf("Enter a number: ");       scanf("%d", &num);       array[num] = num;   }       printf("\n1 is %d", array[0]);       printf("\n2 is %d", array[1]);       printf("\n3 is %d", array[2]);       printf("\n4 is %d", array[3]);       printf("\n5 is %d", array[4]);   return 0; }```
• 12-09-2004
itsme86
Well...what are you inputting for numbers? My guess is that you didn't enter one for index 0.
• 12-09-2004
houler
I'm inputting 1,2, 3, 4, 5 to see if they actually get stored into the elements in order, and to check if 1 matches element 1, 2 matches with element 2, and so forth.
• 12-09-2004
itsme86
Look at the difference between the indexes that you're using to store values in (1 through 5) and the ones you're printing (0 through 4). Do you see the problem yet?
• 12-09-2004
prog-bman
array elements start at zero just to let you know.
you might want to do something like this
Code:

```int i; int num for(i = 0; i < 5; i++) {       printf("Enter a number: ");       scanf("%d", &num);       array[i] = num; }```
• 12-09-2004
houler
Quote:

Originally Posted by itsme86
Look at the difference between the indexes that you're using to store values in (1 through 5) and the ones you're printing (0 through 4). Do you see the problem yet?

Heh. I got it thanks.