Thread: For loop to make patterns

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    The part of the assignment that I can not figure out is:
    "use a nested loop for the patterns. In the inner loop you may use an if structure to determine what to print on each line."

    The pattern I need to make is attached. So Far I am able to collect the data and I have the validation loops, but I can not make the pattern.

    I know it will be something like this:

    int row,col;

    Code:
    //printing the pattern to the screen
    	for (row=1;row<=5;row++)
    	{
    		for (col=1;col<=5;col++)
    		{
    			//if the column is less than or equal to the row -> print (*)
    			//else -> print a space ( )
    			if(col<=row)
    			{
    				printf ("*");
    			}
    			else
    			{
    				printf (" ");
    			}
    		}
    		printf ("\n");
    	}
    Any ideas to help me form the loops to generate the patterns?
    Last edited by zfite; 03-07-2011 at 09:25 PM.

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Perhaps post what your program does rather than post the output of a different program?
    As it is it's rather confusing.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    Code:
    for (row=1;row<=h;row++)
    	{
    		for (col=1;col<=h;col++)
    		{
    			
    			if(col<=row)
    			{
    				printf (" ");
    			}
    			else
    			{
    				printf ("*");
    			}
    		}
    		printf ("\n");
    	}
    That is what I have come up with to make the first pattern, but it is missing one line.

    OK, I am needing to create the program I attached. So far I have

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	//variable declarations
    	int h, w;
    	int row, col;
    
    	//getting the height and a validation loop
    	printf("Enter the height [between 4 and 13(inclusive): ");
    	scanf("%d",&h);
    	while(h<4|| h>13)
    	{
    		printf("Error: The height should be between 4 and 13(inclusive) \n");
    		printf("Re-Enter the height: ");
    		scanf("%d",&h);
    	}
    
    
    	//getting the width and a validation loop
    	printf("Enter the width [between 3 and 10(inclusive): ");
    	scanf("%d",&w);
    	while(w<3 || w>10)
    	{
    		printf("Error: The width should be between 3 and 10(inclusive) ");
    		printf("Re-Enter the width: ");
    		scanf("%d",&w);
    	}
    
    	
    	for (row=1;row<=h;row++)
    	{
    		for (col=1;col<=h;col++)
    		{
    			
    			if(col<=row)
    			{
    				printf (" ");
    			}
    			else
    			{
    				printf ("*");
    			}
    		}
    		printf ("\n");
    	}
    
    	
    
    	
    	//Exit program
    	return 0;
    }

  4. #4
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    If you start from 0 you will get an extra line?

    Also I am not sure if there is a problem with the h++, it may be increasing
    h inside the loop where you do not intend it to.

    The example you show is not missing one line, this is why your post is s confusing.
    You are posting an example of a problem you do not have, which is not helpful.
    Last edited by esbo; 03-07-2011 at 10:46 PM.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    What I have attached are the patterns that I need to make by using for loops. I have attached the code I have, the assignment, and my professor's example.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well what you posted won't work because you start of with row and col equal to the same number, 0. So when you reach this condition
    Code:
    if (col<=row)
    {
        printf(" ");
    }
    col is always less than or equal to row in this nested loop, so a leading space is always printed, at least once. Instead you should notice something about the actual pattern,
    Code:
    ****** 
     ***** 
      ****
       ***
        **
         *
    The actual loops must be a little different than what you have. Each line is the same amount of characters but the number of spaces is equal to the line number you are on - 1. So use that to your advantage. Don't be afraid to change or make a copy of the user's input variable to figure out what you are doing.

  7. #7
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by zfite View Post
    What I have attached are the patterns that I need to make by using for loops. I have attached the code I have, the assignment, and my professor's example.
    I have been a bit confused as to what was what.

    I think maybe the problem is

    Code:
    	for (row=1;row<=h;row++) // here in the test, row is 1
    	{
    		for (col=1;col<=h;col++)
    		{
    			
    			if(col<=row) // but here row is 2!! because it has been incremented by the row++
    			{
    Last edited by esbo; 03-07-2011 at 11:13 PM.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    esbo, clearly you have forgotten how for loops work. The inner for will complete before the outer for will increment row. Then the inner loop starts anew. So the number of times the outer loop executes controls the number of times the inner loop executes. The comments in your post are not true.

  9. #9
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by whiteflags View Post
    esbo, clearly you have forgotten how for loops work. The inner for will complete before the outer for will increment row. Then the inner loop starts anew. So the number of times the outer loop executes controls the number of times the inner loop executes. The comments in your post are not true.
    Maybe I have, I then to avoid using for loops and just use while loops with breaks in them
    to exit the loop, I find that works best for me as I do not have to try and remember how other people code works.

    When you get a tricky bug you tend to begin to doubt everything - lol.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The box is dead easy.

    if your array index is 0 or your array index is width -1, you need to print an asterisk, else print an empty char: ' '.

    In the wedge pattern, do you notice any relationship between the height, and the number of stars?

    height is 0, spaces printed = 0
    height is 1, spaces printed = 1

    etc.
    Last edited by Adak; 03-08-2011 at 12:05 AM.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    The box may be easy but that is the one I need help with now, I have completed the first pattern.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    Can Any One Make This Attached Pic Pattern


    This Is A Challenge !
    Last edited by snayyer; 03-08-2011 at 09:46 AM.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by snayyer View Post
    This Is A Challenge !
    No, it's a thread hijack. Don't do that...

  14. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    can someone please help me make the box pattern?

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For the box pattern, think of two columns - one for left side, and one for the right. Height is just the number of rows for the box.

    Pseudo code could be similar to this:
    Code:
    for row gets 0;row is less than height; increment row 
      if row equals 0 or row equals height-1
         print width number of stars //top and bottom of the box.
    
      Repeat above for loop, with cols this time, and leave out the top and bottom if
      statement. Use something like this, instead:
    
         if col == 0 or col == width-1
            print one star
          else
            print one space char
    
       end inner for loop
    end outer for loop
    @snayyer: What have you tried for logic, on this pattern? Start a thread!
    Last edited by Adak; 03-08-2011 at 10:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 09:51 PM
  2. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  3. Is there a way to make text in color patterns?
    By Darkman in forum C++ Programming
    Replies: 2
    Last Post: 01-09-2004, 03:38 AM
  4. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM