Thread: Populate An Array With Numbers

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    27

    Populate An Array With Numbers

    I am having problems with populating an array with int numbers. Since I am a new programmer, I do not know why my VS 2008 is not completing the code block. In other words when I execute the program, a blank console screen opens. I then input a number and press enter. After I press enter the program stops in a suspended state. What I have discovered is that I have to input a \o for the program to know that it is the end of my input. I though thought that when I pressed enter, VS 2008 would automatically place this end of string designator.

    Secondly, I have problems with index variable. When I loop one time or when I press enter after inputing a number, my index number advances to 8. My question is why the index does not advance to 2?

    Code:
    #include <stdio.h>
    
    int const limit = 8;
    int numbers[limit];
    int main(void)	
    {	
         for(int index = 0; index < limit; index++)		
         {		
         scanf("%d", &numbers[limit]);
         }
    return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have to type eight numbers, not one number.

    Also note that you are throwing (all of) the numbers away, since you do not store the input in a valid memory location.

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Code:
    scanf("%d", &numbers[index]);
    Last edited by nimitzhunter; 01-08-2011 at 02:57 AM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    27
    nimitzhunter and tabstop,

    Thank you both for your reply.

    What I have learned is that first off, the input into the console screen needs to be a number and a space. When the numbers to be stored is less than the total array elements, a \o can be used to end the input and not fully populate the complete array.

    Apparently, when only one element is input into an array with 8 elements, the for statement advances the index to the total of the array elements (ie. 8 total elements) when the enter key is pressed after entering only one element.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mcertini View Post
    nimitzhunter and tabstop,

    Thank you both for your reply.

    What I have learned is that first off, the input into the console screen needs to be a number and a space. When the numbers to be stored is less than the total array elements, a \o can be used to end the input and not fully populate the complete array.

    Apparently, when only one element is input into an array with 8 elements, the for statement advances the index to the total of the array elements (ie. 8 total elements) when the enter key is pressed after entering only one element.
    None of your last paragraph is true, and I'm not even sure what you mean by the second paragraph, as it is not possible to type an end-of-string character '\0' at the keyboard at all.

    If you mean that you literally typed \o, as in the character backslash and the character o, then what happened is that each of your input statements failed, since \ is not a digit, meaning the value of numbers[limit] (if it existed, which it doesn't) wasn't changed.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That is to say, this is what is supposed to happen (you type eight numbers, and they get added to the array (assuming you made the limit->index fix as pointed out above):

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. populate array problem
    By DaleZ in forum C# Programming
    Replies: 3
    Last Post: 11-24-2010, 01:41 AM
  2. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  3. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. getting numbers in an array
    By ct26torr in forum C Programming
    Replies: 6
    Last Post: 03-04-2003, 10:31 AM