Printing an array. (Using a for-loop)

This is a discussion on Printing an array. (Using a for-loop) within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> void display_array(int array[], int c); int main(void) { int c, k, array[20]; for(c = 0, k = ...

  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
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  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
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  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
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Learning something is so not about w/e.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

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, 05: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, 07: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


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