Thread: Printing an array. (Using a for-loop)

  1. #1
    C Newbie
    Join Date
    Oct 2011
    Posts
    59

    Question Printing an array. (Using a for-loop)

    Code:
    #include <stdio.h>
    
    void display_array(int array[], int c);
    
    
    int main(void)
    {
    	int c, k, array[20];
    	
    	for(c = 0, k = 0; c < 20; c++)
    	{
    		printf("Enter a whole number, to end enter: -1. ");
    		scanf("%d", &k);
    		
    		if(k == -1)
    		    break;
    		    
    		array[c] = k;
    	}
    	
    	display_array(array, c);
    	
    	getch();
    }
    
    
    void display_array(int array[], int c)
    {
    	for(c = 0; c < 20; c++)
    	{
    		if(array[c] == )
    		    break;
    		
    		printf("%d\n", array[c]);
    	}
    }
    Now, the problem I get is when a user exits, leaving me with a partially-filled array. Let's say they enter values for 0-8, so 9-19 are left blank, when I run the loop it prints random values for 9-19.

    Is there a way to write an if statement to check for whether or not a value has been entered?

    Thanks in advance,
    ~Alan

  2. #2
    spaghetticode
    Guest
    Why not simply create a counter variable, increment it with each entered value and use it as a loop counter in your function? May not be the best solution, but the first coming to my mind.

  3. #3
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by dennis.cpp View Post
    Why not simply create a counter variable, increment it with each entered value and use it as a loop counter in your function? May not be the best solution, but the first coming to my mind.
    This what you had in mind?

    Code:
    #include <stdio.h>
    
    void display_array(int array[], int c);
    
    
    int main(void)
    {
    	int c, k, array[20];
    	
    	for(c = 0, k = 0; c < 20; c++)
    	{
    		printf("Enter a whole number, to end enter: -1. ");
    		scanf("%d", &k);
    		
    		if(k == -1)
    		    break;
    		    
    		array[c] = k;
    	}
    	
    	display_array(array, c);
    	
    	getch();
    }
    
    
    void display_array(int array[], int c)
    {
    	int k;
    	for(k = 0; k < c; k++)
    	{
    		printf("%d\n", array[k]);
    	}
    }

  4. #4
    spaghetticode
    Guest
    Yea, kind of. I would have done it a bit differently, but a) seems to hit the idea there and b) I'm a beginner too, so both of us could be way off the track here.

  5. #5
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by dennis.cpp View Post
    Yea, kind of. I would have done it a bit differently, but a) seems to hit the idea there and b) I'm a beginner too, so both of us could be way off the track here.
    Well it worked, so w/e.

  6. #6
    spaghetticode
    Guest
    Learning something is so not about w/e.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing letters using for loop
    By arighnasarkar in forum C Programming
    Replies: 2
    Last Post: 01-03-2011, 06:21 AM
  2. Loop isn't printing
    By disruptor108 in forum C Programming
    Replies: 5
    Last Post: 06-13-2006, 11:10 AM
  3. Loop printing arrays
    By CHurst in forum C Programming
    Replies: 2
    Last Post: 12-14-2005, 08:13 PM
  4. problem with printing a structed array using for loop
    By Prezo in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2002, 09:00 AM
  5. printing values loop.
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 07-02-2002, 02:56 PM

Tags for this Thread