stored values in elements of array, help

This is a discussion on stored values in elements of array, help within the C Programming forums, part of the General Programming Boards category; I wrote this to get the feel of how values are stored in the array. when I run the program ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    10

    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

    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;
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Well...what are you inputting for numbers? My guess is that you didn't enter one for index 0.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    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.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    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?
    If you understand what you're doing, you're not learning anything.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,806
    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;
    }
    Woop?

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-19-2008, 01:36 PM
  2. Sum all odd array elements?
    By Marth_01 in forum C Programming
    Replies: 9
    Last Post: 11-04-2008, 09:47 PM
  3. way to check 3 elements of array and set 4th
    By Syneris in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2006, 10:30 AM
  4. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:26 AM
  5. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 09:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21