Thread: Need help to output columns

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    7

    Exclamation Need help to output columns

    Hey I have made a program that prints out 100 random numbers, but the numbers are printed in a long list. I want the output to be in 5 columns of 20 numbers each column, I've tried various ways and the output would always be an endless loop. ( I've used a for loop ) the code here is a full working code with the long list. Please help :/

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <time.h>
    
    
    int main(void){
    	
      int counter,ascending,sort,search;
      int files[100];
       
     
      printf("Random file numbers\n");
     
    	srand(time(NULL));
    	for(counter=0;counter<100;counter++){
        files[counter] = rand()%9999+1;
    	printf("%d\n", files[counter]);
      }
    I think this part of the code is enough rather than the whole code which is too long.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try using nested for loops: the outer loop will loop 20 times; the inner loop will loop 5 times.
    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

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    7
    I know this will get me a facepalm or something but I'm really new to programming, I added this and I got an endless loop output
    Code:
     for (counter1=0; counter1<5; counter1++){   
            for (counter=0; counter<20; counter++){
            
    		printf ("%d", files[counter]);
    		}
            printf ("\n");
        }
        
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    As is often the case, the issue can be approached in many ways. One such alternate still using a single loop is: test the result of the value of the counter variable modulus 5 within the loop to decide when to print a newline instead of always printing a newline as you currently do.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kokoys
    I know this will get me a facepalm or something but I'm really new to programming, I added this and I got an endless loop output
    With nested loops, each loop should have its own counter variable.
    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

  6. #6
    Registered User
    Join Date
    May 2015
    Posts
    7
    I don't know what the problem is, I'm always getting an endless loop, can you show me an example please ?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kokoys
    I don't know what the problem is, I'm always getting an endless loop, can you show me an example please ?
    The problem is that because you reuse the variable named counter in both the inner and outer loops, you keep resetting counter to 0 when you should not do so. Consider this program that prints 4 columns of asterisks with 3 asterisks per column:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i;
        for (i = 0; i < 3; i++)
        {
            int j;
            for (j = 0; j < 4; j++)
            {
                printf("*");
            }
            printf("\n");
        }
        return 0;
    }
    Last edited by laserlight; 05-04-2015 at 11:14 AM.
    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

  8. #8
    Registered User
    Join Date
    May 2015
    Posts
    7
    I'm using counter and counter1

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you actually need the array? I assumed that you either would change your array to a 2D array, or get rid of it since you don't seem to need more than one variable.

    If you prefer to keep the array and have it be a 1D array, then you can either compute the index, or you may find hk_mp5kpdw's suggestion to be simpler for such a case.

    Quote Originally Posted by Kokoys
    I'm using counter and counter1
    In that case your loop will not be an infinite loop.
    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

  10. #10
    Registered User
    Join Date
    May 2015
    Posts
    7
    I have to keep the array, that's the problem, It's part of a task I'm doing, I don't know how to do what hk_mp5kpdw told me to, ( I tried, but I failed )

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the code that you tried?
    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

  12. #12
    Registered User
    Join Date
    May 2015
    Posts
    7
    I tried something like this but still failing
    Code:
    srand(time(NULL));
    	for(counter=0;counter<100;counter++){
        files[counter] = rand()%9999+1;
    	for(counter1=0;counter1<5;counter++){
    		for(a=1;a<20;a++){
    			if(counter1 == 20){
    				counter1 = 0;
    					printf("\n");
    				}
    			}
    		}
    	
    
    
    	printf("%d\n", files[counter]);
      }

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to use hk_mp5kpdw's suggestion, then you will not have nested loops.
    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

  14. #14
    Registered User
    Join Date
    May 2015
    Posts
    7
    I'll just leave it as it is, I can't do it, I can't waste any more time on it I have loads off stuff to work on, thanks for helping.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding columns in output
    By AutoIt Addict in forum C Programming
    Replies: 2
    Last Post: 10-26-2013, 09:03 PM
  2. Output Columns form a file
    By Cdrwolfe in forum Tech Board
    Replies: 2
    Last Post: 03-22-2006, 05:12 AM
  3. Formatting output into aligned columns
    By Kheila in forum C++ Programming
    Replies: 8
    Last Post: 11-14-2005, 09:47 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output in columns
    By stryker1080 in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2005, 12:58 PM