6 Replies - 1167 Views - Last Post: 24 February 2013 - 09:49 PM Rate Topic: -----

#1 J.Bob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-February 13

Program that prints a pattern of asteriks HELP! :(

Posted 24 February 2013 - 12:27 PM

Ok so i have to make a program where the output is
*
**
***
****
*****
****
***
**
*
but so far I'm only getting
*
**
***
****
*****
how would i do the second part i have tried using for loops but i still can't get it to work here is my program

//Main Program
int main()
{
    
    //Declare Variables
    int i;
    int j;
    int z;
    
    //Process
    for (i=0;i<6;i++)
    {
        for (j=0;j<i;j++)
        {
            cout << "*";

            for (z=j;z>j;i--)
           {
                cout << "*";
           }
        }
            cout << "\n";
    }


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 25 February 2013 - 04:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Program that prints a pattern of asteriks HELP! :(

#2 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: Program that prints a pattern of asteriks HELP! :(

Posted 24 February 2013 - 01:42 PM

You might want to decrease the value of j by one before the second loop so it won't print the big line 2 times. Also, you might want to break that 3 level for's into 2 level 2 :) that could be easier to understand
And your last for loop is kinda messep up ... might want to check that

This post has been edited by TwoOfDiamonds: 24 February 2013 - 01:43 PM

Was This Post Helpful? 1
  • +
  • -

#3 michael072  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 100
  • Joined: 08-February 11

Re: Program that prints a pattern of asteriks HELP! :(

Posted 24 February 2013 - 01:53 PM

Quote

We all know what this is and there are variants on it but...
Instructions: Get a number from the user and make an increasing number of dots then a decreasing number of dots until you have a diamond
1 *
2 ***
3 *****
4 *******
5 *****
6 ***
7 *


Plan first. Bang on keyboard second. So what is the logic of this? Well... Lets look at the results and work backwards. There are several rows. The first row has 1 dot, then 3, then 5, then 7, then 5, then 3, then 1. There's a pattern. It works from 1 UP to the target number by twos then back down again. In the back of our minds it seems like there must be a loop going on there for the rows. Ok... outline it..
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	 
	namespace DiamondPattern
	{
	    class Program
	    {
	        static void Main(string[] args)
	        {
	 
	            #region Instructions
	            //Instructions: Get a number from the user and make an increasing
	            // number of dots then a decreasing number of dots until you have a diamond
	 
	            //   *
	            //  ***
	            // *****
	            //*******
	            // *****
	            //  ***
	            //   *
	 
	            // Pattern: 1, 3, 5, 7, 5, 3, 1
	 
	            // Plan: Loop from one up to target.  Loop from target back down to one
	            #endregion
	 
	            #region Fields/Variables
	            #endregion Variables
	 
	            #region Get the target number from the user
	            #endregion
	 
	            #region Loop going up
	            #endregion Loop going up
	 
	            #region Loop counting down
	            #endregion Loop counting down
	 
	            #region Get [ENTER] from user just to hold the console open
	            #endregion
	 
	        }
	    }
	}



Well, that makes it seem easy. Now start filling in the logic with some code. As you can see we haven't tried to do it all at once. We're just breaking down the big problem into a few small easy to solve tasks.

Attached Image

So what would be the next part? You have to calculate how many spaces to output, followed by the right number of asterisks. If you stop to think about it, its simple math. Half the askterisks are left of the center, the other half right. For that you need to know how many asterisks are going to be on the longest row. What's that pattern again? 1, 3, 5, 7, 9 ...
It looks a lot like (RowNumber * 2) + 1

Row 0 ... 0 * 2 + 1 = 1
Row 1 ... 1 * 2 + 1 = 3
Row 2 ... 2 * 2 + 1 = 5
Row 3 ... 3 * 2 + 1 = 7


Yep, that works!
So the length of the longest line is.... (UserInput * 2) + 1
How do we figure the number of spaces from that? That's easy, count it out when we type it

Row 0 ... 8 spaces, 1 asterisk
Row 1 ... 6 spaces, 3 asterisks
Row 2 ... 4 spaces, 5 asterisks
Row 3 ... 2 spaces, 7 asterisks
Row 4 ... 0 spaces, 9 asterisks


Again, we have a couple easy-to-see patterns. From row 0 to row X the spaces go from TotalNumberOfAsterisksInLongestLine-1 downward by 2, and the asterisks go upward from 1 to TotalNumberOfAsterisksInLongestLine by two. That sounds like a couple loops for each row doesn't it? A loop to make the spaces then a loop to make the asterisks.

Since this is the homework for so many students we aren't going to just give you the code. We've already given you a start on planning, and given you some insight on how to solve this puzzle.


http://www.dreaminco...cle-you-wanted/

This is a common homework assignment tlhIn`toq has made a good post about it and I think it could be worth the read for you.

This post has been edited by michael072: 24 February 2013 - 01:57 PM

Was This Post Helpful? 1
  • +
  • -

#4 J.Bob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-February 13

Re: Program that prints a pattern of asteriks HELP! :(

Posted 24 February 2013 - 01:59 PM

View PostTwoOfDiamonds, on 24 February 2013 - 01:42 PM, said:

You might want to decrease the value of j by one before the second loop so it won't print the big line 2 times. Also, you might want to break that 3 level for's into 2 level 2 :)/> that could be easier to understand
And your last for loop is kinda messep up ... might want to check that

Yeah it was messed up but I kept on working on it and i figured it and got it to work i split them up so one part would go 1-5 and then the second part goes from 4-1 but i figured it out but Thanks Anyways :)
Was This Post Helpful? 0
  • +
  • -

#5 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: Program that prints a pattern of asteriks HELP! :(

Posted 24 February 2013 - 02:02 PM

My pleasure ^_^
Was This Post Helpful? 0
  • +
  • -

#6 J.Bob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-February 13

Re: Program that prints a pattern of asteriks HELP! :(

Posted 24 February 2013 - 02:03 PM

@Michael072
yeah that's the second part of my program that i have to code for but i only need 1,3,5,7... but thank you for the website and for your help! :)
Was This Post Helpful? 0
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Program that prints a pattern of asteriks HELP! :(

Posted 24 February 2013 - 09:49 PM

Well, if you could rename the variables as row_counter and column_counter instead of i and j, it will help you to understand the working better...and yes, you can do that using only 2 for loops.

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1