Thread: need help printing this pattern

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    1

    need help printing this pattern

    I am having trouble coming up with the function code for this. User will input a size which corresponds to the number of rows and number of columns that will be printed. For example, if the user gives a size of 5, this is what will be printed:

    $$$$$
    $$$$5
    $$$55
    $$555
    $5555

    Any help would be appreciated.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Would you happen to know this guy?

    http://cboard.cprogramming.com/showt...hlight=Pattern
    Sent from my iPadŽ

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    here is a sample code which will print you the above formatted aoutput. it dont ask for any row an coloum input, you got to work through it. think about the code - which bit of the code will change to get your requirenmnet -

    Code:
    #include<stdio.h>
    
    int main()
    {
        int i,j,k;
        
        for(i=0;i<5;i++)
        {
            for(j=5-i;j>0;j--)
                printf("%c",'$');
            for(k=i;k>0;k--)
                printf("%d",5);
            
            printf("\n");
        }
    
        getchar();
        return 0;
    }
    
    /*my ouptput
    $$$$$
    $$$$5
    $$$55
    $$555
    $5555
    */
    ssharish2005

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    And the spoon-feeder of the week award goes to ....

    How does blurting out the whole answer help the OP understand anything?

    There's every likelyhood that they'll just hand it in without really understanding anything at all, which makes their next assignment all the more difficult.

    Oh well,

  5. #5
    Registered User
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    40
    I wasn't going to post my whole code here because I thought it might be a give away but since it has already been done, I will. I hope that is OK.

    Before I post it, one caveat though...

    I am very new to C. I have been exposed to it for a whole 4 weeks and I've only learnt a few functions and control structures so I am sure that this is nowhere near the best way to do this. If you are learning, you might not want to take much notice of this. Perhaps some of the more experienced people here can offer a comment on what I could have done better.

    Anyway, for what it's worth, here's my code. Please don't laugh...

    Code:
    #include <stdio.h>
    
    int main (void) {
    	int rows, row_num, filled, unfilled, the_num; /* declaring the variables for use in the loops and user entry */
    	char the_char = '$'; /* Declaring the spacer character. As per example, it is set to a $ but could be anything*/
    	
    	/* The user interface bit. Asks for the size of the grid. Works best with numbers 1-9 but will work with any
    number (although the results look weird) */
    	printf("Enter the number of columns/rows: ");
    	scanf("%d", &rows);
    	
    	/* Setting up the values for the variables, at this stage they are all either the same as the number 
    entered by the user or are zero */
    	the_num = rows; /* Used to store the number to print in the grid */
    	filled = 0; /* Used to keep track of how many numbers should appear on the line */
    	unfilled = rows; /* Used to keep track of how many spacer characters are on the line */
    	row_num = 0; /* Used to keep track of which row or line is being printed */
    	
    	/* This is the outer loop. Each loop prints 1 line of $'s and Numbers */
    	while (row_num < rows) {
    		
    		unfilled = (rows - row_num); /* This sets the number of $'s to print to 1 less on each line */
    		
    		/* This is the first of 2 inner loops. This one prints the spacer character ($ in this case) */
    		while (unfilled > 0) {
    			printf ("%c", the_char);
    			unfilled = (unfilled - 1);
    		}
    		
    		filled = row_num; /* This sets the number of numbers to print to 1 more on each line */
    		
    		/* This is the second of the 2 inner loops. This one prints the number */
    		while (filled > 0) {
    			printf("%d", the_num);
    			filled = (filled - 1);
    		}
    		
    		printf("\n"); /* This prints a new line so that, on the next outer loop, the text prints on it's own line */
    		
    		row_num = (row_num + 1); /* Keeps track of how many lines there have been and ends if it reaches the number entered */
    	}
    	
        return 0;
    }
    Well, at least it works.

    I have commented the hell out of it in the hopes that it will help you understand what I've done and why I've done it.

    Any questions or comments? Let me know.
    Last edited by tvsinesperanto; 03-04-2006 at 06:46 AM. Reason: Code was too wide for most sceens. Inserted some <cr>'s

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Printing Patterns
    By ferniture in forum C Programming
    Replies: 11
    Last Post: 11-17-2008, 10:00 PM
  3. Printing a pattern correctly???
    By Basia in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 04:34 PM
  4. Printing a Pattern of Stars
    By ProgrammingDlux in forum C++ Programming
    Replies: 13
    Last Post: 03-01-2002, 08:38 AM
  5. text pattern recognition
    By mtsmox in forum C++ Programming
    Replies: 5
    Last Post: 02-27-2002, 08:38 AM