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
instead of:
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;
}