Thread: Array doesn't show correct value

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    Array doesn't show correct value

    I want to store value taken by user into array and it should be print on single

    For example

    array = 22, 44, 25, 18, 12

    I wrote following program

    Code:
    #include<stdio.h>int main (void)
    {
        int i;
        int array[5];
        
        for(i=0; i < 5 ; i++)
        {
          scanf("%d",&array[i]);
        
        }
        
         printf("%d", array[i]);
        
        return 0;
    
    }
    What's wrong in program ?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,631
    You're trying to print an array element that is out of bounds.

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by jimblumberg View Post
    You're trying to print an array element that is out of bounds.
    I didn't get you

    This is example of self define array

    Code:
     #include<stdio.h>
    int main() {
       int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
       int loop;
    
       for(loop = 0; loop < 10; loop++)
          printf("%d ", array[loop]);
          
       return 0; }
    The output should look like this 1 2 3 4 5 6 7 8 9 0

    And I am trying to create user define array

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,631
    The output should look like this 1 2 3 4 5 6 7 8 9 0
    Yes, that is what that code prints, so what is the problem?

    In your previous post you were trying to print array[i] after the loop and i is outside the bounds of the array. You do realize that you can't print the entire array with a single call, right?

  5. #5
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by jimblumberg View Post
    Yes, that is what that code prints, so what is the problem?

    In your previous post you were trying to print array[i] after the loop and i is outside the bounds of the array. You do realize that you can't print the entire array with a single call, right?
    I am trying to convert following steps into program

    1. Enter array numbers
    2. Get the numbers into array
    3. Print it on one single line like 5, 2,9, 30

    I tried to make program see post 1. I don't have idea on third point. can you give me some advice on it

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,631
    I tried to make program see post 1. I don't have idea on third point. can you give me some advice on it
    Look at your second post, you're printing the array correctly there.

  7. #7
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by jimblumberg View Post
    Look at your second post, you're printing the array correctly there.
    Is it okay

    Code:
    #include<stdio.h>
    
    int main (void)
    {
        int i, j;
    	
        int array[5];
         
        for(i=0; i < 5 ; i++)
        {
          scanf("%d",&array[i]);
         
        }
         
    	for(j=0; j < 5 ; j++)
        {
          printf("%d ", array[j]);
        }
        
         
        return 0;
     
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,463
    Looks good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-07-2017, 05:14 PM
  2. Replies: 18
    Last Post: 08-31-2012, 10:17 AM
  3. It doesn't return a correct result...
    By pantera in forum C++ Programming
    Replies: 5
    Last Post: 01-16-2010, 05:23 PM
  4. Program doesn't show up
    By Krasimir11 in forum C Programming
    Replies: 7
    Last Post: 01-05-2006, 06:21 PM
  5. per: normal doesn't point in correct direction
    By Silvercord in forum Game Programming
    Replies: 3
    Last Post: 05-16-2003, 08:39 AM

Tags for this Thread