Thread: can't display values of the array

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    27

    can't display values of the array

    Q:Create an array having 10 elements of type int called lab1_array. Populate the arrayelements individually prompting the user each time for an integer value. Output the
    values to the user after all 10 are inputted in the following format:
    lab1_array[0] = 1
    lab1_array[1] = 2
    lab1_array[2] = 3
    .
    .
    lab1_array[8] = 9
    lab1_array[9] = 10


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    
    
        int lab1_array[9],i;
    
    
    
    
    
    
          for(i=0;i<10;++i){
              printf("Enter value : ");
              scanf("%d",&lab1_array[i]);
    
    
          }
    
    
        printf("lab1_array[%d] = %d \n",i,lab1_array[i]);
    
    
    
    
     return 0;
    
    
    }
    this is what I got so far and im lost .. guide me please T.T

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Place the printf() statement in a loop. It is not in one.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    27
    Quote Originally Posted by grumpy View Post
    Place the printf() statement in a loop. It is not in one.
    it is still not correct as I get
    lab1_array[10] = some big number

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Look at how you got data into the array. You need to do the same thing to print each element of the array.

    And if you have a 10-element array like you have here, then accessing element 10 of the array is undefined behavior. Valid indexes for a 10-element array range from 0 to 9.

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    27
    Quote Originally Posted by rags_to_riches View Post
    Look at how you got data into the array. You need to do the same thing to print each element of the array.

    And if you have a 10-element array like you have here, then accessing element 10 of the array is undefined behavior. Valid indexes for a 10-element array range from 0 to 9.
    thank you sir , I did what you said like this

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    
    
        int lab1_array[9],i;
    
    
    
    
    
    
          for(i=0;i<=9;++i){
              printf("Enter value : ");
              scanf("%d",&lab1_array[i]);
    
    
          }
    
    
        for(i=0;i<=9;++i){
    
    
              printf("lab1_array[%d] = %d \n",i,lab1_array[i]);
          }
    
    
    
    
    
    
     return 0;
    
    
    }
    everything is correct except the last array index value is wrong ??

  6. #6
    Registered User
    Join Date
    Feb 2014
    Posts
    27
    this is the output im getting
    can't display values of the array-newprogrammmmmmmmmm-png

    as you can see the last array index is wrong ...what am I doing incorrectly ?

  7. #7
    Registered User
    Join Date
    Feb 2014
    Posts
    27
    please anyone ... I just cant figure out why the last array index is wrong T.T

  8. #8
    Registered User
    Join Date
    Jan 2014
    Posts
    3
    Quote Originally Posted by hummingdev View Post
    please anyone ... I just cant figure out why the last array index is wrong T.T
    do a < 10

    instead of a 9 for your loops and increase your array index to 10

  9. #9
    Registered User
    Join Date
    Feb 2014
    Posts
    27
    Quote Originally Posted by raphaelriv View Post
    do a < 10

    instead of a 9 for your loops
    still the same output as above , the last index wrong

  10. #10
    Registered User
    Join Date
    Jan 2014
    Posts
    3
    increase your index to 10

  11. #11
    Registered User
    Join Date
    Feb 2014
    Posts
    27
    that's not what the question wants

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Feb 2014
    Posts
    27
    Quote Originally Posted by laserlight View Post
    What is your current code?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    
    
        int lab1_array[9],i;
    
    
    
    
    
    
            for(i=0;i<=9;i++){
              printf("Enter value : ");
              scanf("%d",&lab1_array[i]);
    
    
          }
    
    
            for(i=0;i<=9;i++){
    
    
              printf("lab1_array[%d] = %d \n",i,lab1_array[i]);
          }
    
    
    
    
    
    
     return 0;
    
    
    }

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to format your code properly, especially with regards to indentation. For example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int lab1_array[9], i;
    
        for (i = 0; i <= 9; i++) {
            printf("Enter value : ");
            scanf("%d", &lab1_array[i]);
        }
    
        for (i = 0; i <= 9; i++) {
            printf("lab1_array[%d] = %d \n", i, lab1_array[i]);
        }
    
        return 0;
    }
    Next, raphaelriv's advice is sound. Generally, if you have an array of 9 elements and want to iterate over each element of the array in a "forward" direction, you would write:
    Code:
    for (i = 0; i < 9; i++)
    Notice the use of < rather than <=.

    But here you are required to have a 10 element array, so this declaration was wrong to begin with:
    Code:
    int lab1_array[9]
    and subsequently your loop condition should be i < 10, not i < 9. This happens to be equivalent to i <= 9, but the i < 10 form is canonical when we start looping from i = 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Feb 2014
    Posts
    27
    thank you so much for explaining it

    but the problem still persist as I have
    Code:
    #include <stdio.h>
     #include <stdlib.h>
    
    int main()
    {
        int lab1_array[9], i;
    
        for (i = 0; i <10; i++) {
            printf("Enter value : ");
            scanf("%d", &lab1_array[i]);
        }
    
        for (i = 0; i <10; i++) {
            printf("lab1_array[%d] = %d \n", i, lab1_array[i]);
        }
    
        return 0;
    }
    but it is giving me the last array index value wrong

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display average pixel values
    By defunktlemon in forum C# Programming
    Replies: 2
    Last Post: 02-04-2013, 09:48 PM
  2. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 01:32 PM
  3. How to display the values of a struct
    By m88g88 in forum C Programming
    Replies: 4
    Last Post: 02-15-2010, 06:07 PM
  4. Array won't display new values?
    By Kespoosh in forum C++ Programming
    Replies: 9
    Last Post: 03-17-2003, 09:22 PM
  5. Need help with display of array
    By BalanusBob in forum C# Programming
    Replies: 1
    Last Post: 04-19-2002, 11:19 PM