Thread: User assigned values into an Array

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    32

    User assigned values into an Array

    Hey guys! I have been using this site for a while now I gotta say, I love it!

    I have a question now, though. I am writing a very simple program where a user can input 10 GPA's and then list them in either ascending or descending order. I am running into an issue though. I have tried two different ways so far, but neither seem to be working.

    This first way gets the loop that I want, but when the last 'printf' is called, it always displays random numbers...
    Code:
    main()
     {
        float iGpa[10];
        int x=0;
    
        while (x<10){
            printf("Please enter 10 GPA's\n: ", x++);
            scanf("%f", &iGpa[x]);
        }
        printf("%f", iGpa[10]);
    }
    ...And for this one, I get a SEGFAULT error
    Code:
    main()
    {
    
        float iGpa[10];
        int x=0;
    
        for(x=0;x<10;x++){
            printf("Enter 10 GPA's: ",x++);
            scanf("%f", &iGpa[10]);
        }
        prinf("%f", iGpa[10]);
    }
    My suspicion is either my code to assign the values into the array is incorrect, of my code to display the array is incorrect, but I can not figure out a which/how.

    Also, I am pretty sure I could get it if I assign each GPA to it's own variable, but I would rather a loop for cleaner code.

    Any help is greatly appreciated, Thanks!
    Last edited by Mark Labarbara; 11-12-2011 at 01:55 PM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    In the first, you are incrementing x before you scanf, increment in scanf and that problem is solved.

    In both you are referring to an out of bounds index. Remember array indexes start with zero and end with N-1.

    To print each element of the array, use a for loop.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by Tclausex View Post
    In the first, you are incrementing x before you scanf, increment in scanf and that problem is solved.

    In both you are referring to an out of bounds index. Remember array indexes start with zero and end with N-1.
    I know that the indexes will be 0-9 (which=10), but how would I tell it to assign the values in iGpa[0], iGpa[1], etc... in the loop? Do I need to remove the [10] from the scanf?

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    In a for loop:

    Code:
    int i;
    int myArray[10];
    
    /*Placing stuff into an array*/
    for(i = 0; i < 10; i++)
    {
         myArray[i] = /* some value */
    }
    
    /* Some intermediate steps... */
    
    /*Printing values from an array*/
    for(i = 0; i < 10; i++)
    {
         printf("myArray[%d] = %d", i, myArray[i]);
    }
    Last edited by failure67; 11-12-2011 at 02:14 PM.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Your first code was correct up to the printf and needing to increment x in scanf.

    Yes. iGpa[10] refers to nothing - it is out of bounds of the array. And using an incrementing variable is how to step through an array.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by failure67 View Post
    In a for loop:

    Code:
    int i;
    int myArray[10];
    
    /*Placing stuff into an array*/
    for(i = 0; i < 10; i++)
    {
         myArray[i] = /* some value */
    }
    
    /* Some intermediate steps... */
    
    /*Printing values from an array*/
    for(i = 0; i < 10; i++)
    {
         printf("myArray[%d] = %d", i, myArray[i]);
    }
    Thanks for the help, but I already know that. The problem I am having is getting the array values to be assigned by the user input.

    Quote Originally Posted by Tclausex View Post
    Your first code was correct up to the printf and needing to increment x in scanf.

    Yes. iGpa[10] refers to nothing - it is out of bounds of the array. And using an incrementing variable is how to step through an array.
    After changing the x++ to scanf, it still doesn't work right. When the last printf is called, it just displays the last gpa entered (i.e. if the last gpa was 3 it will display 3.00000).

    I am sorry, but I am just not understanding what you are saying, I suppose.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Code:
    main()
     {
        float iGpa[10];
        int x=0;
    
        while (x<10){
            printf("Please enter 10 GPA's\n: ");
            scanf("%f", &iGpa[x++]);
        }
        
    }
    This is correct! Just moved where the increment occurred.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by Tclausex View Post
    Code:
    main()
     {
        float iGpa[10];
        int x=0;
    
        while (x<10){
            printf("Please enter 10 GPA's\n: ");
            scanf("%f", &iGpa[x++]);
        }
        
    }
    This is correct! Just moved where the increment occurred.
    If I do it that way, how would I print the contents of the array?

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by Mark Labarbara View Post
    If I do it that way, how would I print the contents of the array?
    That was already answered.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by Tclausex View Post
    That was already answered.
    When I do it that way
    Code:
    main()
    {
        float iGpa[10];
        int x;
    
    
        while (x<10){
            printf("Enter 10 GPA's\n: ");
            scanf("%f", &iGpa[x++]);
        }
        for (x=0;x<10;x++){
            printf("\niGpa[%d] = %f", x, iGpa[x]);
        }
    }
    the output is
    Code:
     
    $ ./GPA
    iGpa[0]= 0
    iGpa[1]= 536870912
    iGpa[2]= 536870912
    iGpa[3]= 0
    etc...
    ...
    It never lets me input the ten GPA's and prints random numbers.
    Last edited by Mark Labarbara; 11-12-2011 at 03:37 PM.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    What is your input?

    Of course the output is bad. Look at your printf format specifiers and ask yourself if they match the variables' data types.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    Hint: What is %d and %f used for?

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by Tclausex View Post
    What is your input?

    Of course the output is bad. Look at your printf format specifiers and ask yourself if they match the variables' data types.
    Sorry about that, typed it wrong. Still doesn't work though...

    Quote Originally Posted by failure67 View Post
    Hint: What is %d and %f used for?
    integer and float. But when I fixed that, the program still won't let me input numbers and outputs different random numbers.

  14. #14
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Your problem is yet another format specifier - look at your scanf.

  15. #15
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Fixed, still not it.

    Does the same thing, doesn't let me input any numbers and goes straight to the for loop and prints random numbers.

    Thanks again for all of your help, guys. I really appreciate it
    Last edited by Mark Labarbara; 11-12-2011 at 03:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-01-2010, 08:22 PM
  2. Huge pre-assigned values to variables
    By cpudaman in forum C++ Programming
    Replies: 5
    Last Post: 01-09-2008, 04:48 AM
  3. why automatic variables are assigned with garbage values
    By srinivasg in forum C Programming
    Replies: 1
    Last Post: 11-08-2005, 07:14 AM
  4. How to let the user input values for an array...
    By TerranAce007 in forum C++ Programming
    Replies: 2
    Last Post: 08-24-2002, 03:54 AM
  5. Array elements values not being assigned
    By Sardien in forum C Programming
    Replies: 4
    Last Post: 02-11-2002, 01:45 PM