Like Tree1Likes
  • 1 Post By Adak

Trying to set two arrays which print out on one line

This is a discussion on Trying to set two arrays which print out on one line within the C Programming forums, part of the General Programming Boards category; I'm trying to make this set two arrays each of 10 numbers, which are defined by the user, and then ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    81

    Trying to set two arrays which print out on one line

    I'm trying to make this set two arrays each of 10 numbers, which are defined by the user, and then print out on one line.

    Currently, it asks and stores the first 10, then asks the second but immediately prints a peculiar number.

    What am I doing wrong?

    Should I have two counts instead of one?

    Code:
    #include<stdio.h> 
    void main()
    
    
    {
    
    
    int list[10];
    int list1[10];
    int count = 0;
    
    
    
    
    printf("Enter 10 numbers:");
    
    
        while (count < 11)
    
    
            {
                
            scanf("%d", &list[count]);
            
            count++;
            
            break;
            
            }
            
    printf("Enter a new set of 10 numbers:");
            
        while (count < 21)
    
    
            {
                
            scanf("%d", &list1[count]);
            
            count++;
            
            break;
            
            }
            
    printf("%d%d", list, list1);
            
            
    return;
    
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to set the count variable to 0, before each of the loops. In C you nearly always want:

    Code:
    i=0;
    while(i < 10)  //not <= 10, or < 11 array[10] is out of bounds
       scanf("%d",  &array[i]);
    I like the idea of the while loop, but 80% of the time when there's a loop problem, changing over to a for loop, solves the problem. It makes you think more clearly about controlling the loop.

  3. #3
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    67
    Code:
    for (count = 0; count < 10; count ++)
        printf("%d ", list[count]);
    puts("");
    for (count = 0; count < 10; count ++)
        printf("%d ", list1[count]);

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Code:
    #include<stdio.h> void main()
    
    
    {
    
    
    int list[10];
    int list1[10];
    int count = 0;
    
    
    
    
    printf("Enter 10 numbers:");
    
    
    	while (count < 10)
    
    
    		{
    			
    		scanf("%d", &list[count]);
    		
    		count++;
    		
    		break;
    		
    		}
    		
    printf("Enter a new set of 10 numbers:");
    		
    	count=0;
    
    
    	while (count < 10)
    
    
    		{
    			
    		scanf("%d", &list1[count]);
    		
    		count++;
    		
    		break;
    		
    		}
    		
    printf("%d%d", list, list1);
    	
    		
    		
    return;
    
    
    }
    Fader, I did follow your advice, please don't feel I ignored you, but it produced a similar problem so I kept my code like this with the count reset added. It still produces the same problem as before though, how come?

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    list and list1 aren't integers. You can't print them with %d. You need to loop through each index, and print it one by one, just like you're reading them.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Quote Originally Posted by King Mir View Post
    list and list1 aren't integers. You can't print them with %d. You need to loop through each index, and print it one by one, just like you're reading them.
    Thanks, now it stores all twenty numbers but when it comes to print it shows a list of weird numbers (2000000 something). I got rid of my break; because I thought maybe they were causing difficulties?

    Code:
    #include<stdio.h> void main()
    
    
    {
    
    
    int list[10];
    int list1[10];
    int count = 0;
    
    
    
    
    printf("Enter 10 numbers:");
    
    
    	while (count < 10)
    
    
    		{
    			
    		scanf("%d", &list[count]);
    		
    		count++;
    		
    		}
    		
    printf("Enter a new set of 10 numbers:");
    		
    	count=0;
    
    
    	while (count < 10)
    
    
    		{
    			
    		scanf("%d", &list1[count]);
    		
    		count++;
    		
    		}
    		
    	count=0;
    	
    	while (count < 10)
    
    
    		{
    			
    		printf("%d ", &list[count]);
    		
    		count++;
    		
    		}
    		
    	
    	count=0;
    
    
    	while (count < 10)
    
    
    		{
    			
    		printf("%d ", &list1[count]);
    		
    		count++;
    		
    		}
    				
    return;
    
    
    }

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    do this:
    printf("%d ", list1[count]);

    Unlike scanf, printf doesn't require pointers for most parameters.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No need for all that code:

    Code:
    #include<stdio.h> 
    
    
    int main(void)
    {
       int list[10];
       int list1[10];
       int count = 0;
    
       printf("Enter 20 numbers:\n");
       
       while (count < 10)
       {
    	scanf("%d", &list[count]);
    	count++;
       }
    		
       printf("Enter a new set of 10 numbers:\n");
       count=0;
       while (count < 10)
       {
    	scanf("%d", &list1[count]);
    	count++;
       }
       count=0;
       while (count < 10)
       {
    	printf("%d ", list[count]);
    	count++;
       }
       count=0;
       while (count < 10)
       {
    	printf("%d ", list1[count]);
    	count++;
       }
       return 0;
    }
    Try that - no & in the printf()'s. Just in the scanf()'s.
    MK27 likes this.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    That's great, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 08-07-2011, 10:55 PM
  2. how to open files and print line by line in shell
    By omega666 in forum Linux Programming
    Replies: 4
    Last Post: 04-15-2011, 05:54 PM
  3. print the data in a line
    By munna_dude in forum C Programming
    Replies: 4
    Last Post: 06-16-2010, 10:10 AM
  4. Print Certain Line in C
    By mancode1009 in forum C Programming
    Replies: 7
    Last Post: 08-27-2008, 08:47 PM
  5. print line by line from a file
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 02-18-2005, 01:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21