1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Programming in C, creating an Array user gets to input

  1. Nov 9, 2012 #1
    1. The problem statement, all variables and given/known data

    Alright, so for my engineering programming class I have an assignment where I have to create a program with an array, and then do things with the array, such as create a search function for it, and list how many numbers fall within a certain range, ect, ect.

    So I actually just need some help getting started on this particular assignment. I need to create an Array where the user inputs 20 numbers from the keyboard. Unfortunately all the examples we are given, the arrays have values already assigned to them. I have no idea how to let a user create the data for the area to be displayed.

    2. Relevant equations

    For example

    int main()
    /* use initializer list to initialize array n */
    int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
    int i; /* counter */

    printf( "%s%13s\n", "Element", "Value" );

    /* output contents of array in tabular format */
    for ( i = 0; i < 10; i++ ) {
    printf( "%7d%13d\n", i, n[ i ] );
    } /* end for */
    return 0; /* indicates successful termination */

    All the values are given, and I know how to do that just fine. I have no idea how to let a user input the values for the array though.

    3. The attempt at a solution

    I tried some different things with scanf("%s", n); where "n" is the name of the array, but all the results always ended up wonky, giving me wild values once displayed that were definitely not anything I input.

    How do I make an array where the values are put in by the user? The rest of the assignment I think I have a handle on, but every example involving arrays from our book, the values in the array are decided in the program itself. Thanks in advance.
  2. jcsd
  3. Nov 10, 2012 #2


    Staff: Mentor

    When you post code, it's considered good manners to put [ code ] and [ /code ] tags around your code (omit the spaces, though). This preserves your indentation, making your code easier to read.

    I have done this for your code.
    You are not using the right conversion specifier - the "%s" part. That's the specifier you use to read in a string. Your array is of type int, so the conversion specifier should be "%d".

    Also, and this is very important, the input argument to scanf has to an expression that represents an address. In fact, n does represent an address, but to input values to an array you're going to have to have a loop that iterates through the array. Having a 2nd argument of n will mean that all of the values you type in go to the same location in memory - not good.

    Declare the array, but don't include an initializer list. The array values will get set in a loop, something like this.

    Code (Text):

    int list[5];  // Uninitialized array
    int i;

    for (i = 0; i < 5; i++)
        printf("Enter a value: ");
        scanf("%d", &list[i]);
  4. Nov 10, 2012 #3
    Thank you very much kind sir. Sorry for the breach of etiquette. I fairly new here, and I haven't used the homework forum much.

    Your solution worked like a charm though, and now I can get all of the other parts taken care of.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook