Thread: NEED homework help! Printing out patterns.

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97

    NEED homework help! Printing out patterns.

    Okay, so i just started programming a week ago and i know some stuff but am completely stumped with this.

    I can print out this pattern easy, where "-" = spaces

    *******
    -******
    --*****
    ---****
    ----***
    ---****
    --*****
    -******
    *******

    by doing
    Code:
    	for(r=0;r<9;r++)
    	{
    		if(r<5)
    		{
    			m=1;
    			b=0;
    			m1=-1;
    			b1=7;
    		}
    		else
    		{
    			m=-1;
    			b=8;
    			m1=1;
    			b1=-1;
    		}
    			for(s=0;s<(m*r)+b;s++)
    			printf(" ");
    			for(c=0;c<(m1*r)+(b1);c++)
    			printf("*");
    			printf("\n");
                     }
    but i cannot seem to do,

    ************************* 25
    **************** 16
    ********* 9
    **** 4
    * 1


    Code:
    		for(r=0;r<5;r++)
    	{
    		for(c=0;c<(r^2)+(-10*r)+25;c++)
    			printf("*");
    			printf("\n");
    	}
    i tried using a quadratic formula to find the slope instead of y=mx+b. but it didnt print out right. Any help would be kind.
    Last edited by omGeeK; 09-28-2010 at 06:33 PM.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You do know that ^ is the bitwise exclusive or operator in C, right? It is not the notation for raising a number to a power.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    umm yea just realized i put this in the wrong forum i am using c++ so yea

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    No worries at all. ^ is the bitwise exclusive or operator in C++ as well.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    lol okay then well maybe thats were i messed up, so what should i use.
    Last edited by omGeeK; 09-28-2010 at 07:03 PM.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You can #include <math.h> (<cmath> for C++) and call the pow() function.


    In this case, I would probably just multiply the number by itself though.
    Last edited by kermit; 09-28-2010 at 06:56 PM.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    problem solved, simply did (r*r). thanks a bunch

  8. #8
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Is there any reason why in this case you have a loop counting up from zero when a loop counting down would make more sense?

    e.g.
    Code:
    int i,j;
    for(i = 5; i >= 0; i--)
    {
            for(j=0; j <= i*i; j++)
            {
                     printf("*");
             }
    }
    Just makes the expression in the inner loop a bit less confusing.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    no reason at all, just how i was taught so i kinda got used to it.

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. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  4. Homework HELP - Problem Printing to file
    By brentshreve in forum C Programming
    Replies: 5
    Last Post: 04-23-2005, 02:11 AM
  5. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM