need some guidance

flower pattern using astericks

Page 1 of 1

2 Replies - 1061 Views - Last Post: 18 November 2008 - 12:05 PM Rate Topic: -----

#1 anam   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-November 08

need some guidance

Posted 18 November 2008 - 10:41 AM

I have an assignment.It is due friday.

1. Write a program that takes two numbers (height and width) as input and prints the flower pattern according to the input numbers.
Example
Say width = 7 and height = 5. Flower according to these dimensions will be as follows:

view the diagram in the attachment
the image is appearing distorted in the post.I am refering to question 1 in the assignment.




Note: This is not a diamond. You have to make a flower pattern containing four triangles and one rectangle separated by 2 character space.


I have printed a diamond having lines= height +2width and columns=height +2width.I have figured out a relation where not to print the astericks but It could only be implemented using if statement when i try to use if statement after another if the program runs but it shows no output.so, can anyone tell me how to use two if statements in my program.
my code.
height is y
width is x



int limit=0;
int x=0,y=0;
cin>>x;
cin>>y;
int line;
for (line=1;line<=(x+2*y);line++){
int column;
for (column=1;column<=((x+2*y));column++){


if ((column<=((x+2*y)/2)+1-limit)  || (column>=((x+2*y)/2)+1+limit) )  {

cout<<" ";}
else
cout<<"*";


}
cout<<endl;
if (line<=((x+2*y)/2))
limit++;
else
limit--;
}

return 0;
}


I want to put other if statements after the first one but it does not work.
if ((column=y) || (column=y-1))
if ((column=2*x) || (column=2*x-1))
if ((line=y) || (line=y+1))
if ((line=2*x-1) || (line=2*x-2))


after the first one but it does not work
if ((column<=((x+2*y)/2)+1-limit)  || (column>=((x+2*y)/2)+1+limit) )  {
here
cout<<" ";}


Can anyone guide me.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: need some guidance

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5238
  • View blog
  • Posts: 14,031
  • Joined: 18-April 07

Re: need some guidance

Posted 18 November 2008 - 11:43 AM

Lets put aside the code you have worked on so far for now. Lets go back and take a look at the picture and note a few things.

1) The middle square is the width and height of the user entered numbers. Obviously we are going to need a loop to print that out.

2) The big end of each top and bottom triangle is equal to the width of the triangle while the big end of each left and right triangle is the height of the rectangle.

3) Each left/right triangle will go from 1 to the height of the rectangle or the height of the rectangle to 1. Obviously we are going to need loops for this as well and to account for spaces to stagger them.

4) Each top / bottom triangle either goes from 1 to the width of the rectangle or the width of the rectangle down to 1.


So by knowing the height and width of the rectangle that the user enters, we know some of the boundaries of each of these shapes where asterisks are going to be printed.

Next we have to figure out how we are going to manipulate the spaces around the shapes to get them to line up with one another. Use the image in the assignment to figure this out.

Doing a little counting we can see the first asterisk at the top is going to be about 8 spaces. We figured this out by counting from the left side of the rectangle to the left. Why the left side of the rectangle? Because that is where the left bottom corner of the top and the top left corner of the triangles meet. So counting left we have 2 spaces, then three rows of asterisks. This is 5 (we might have to add a few spaces to this count), then in the top triangle we see there is 3 more rows upwards.

So we know that we have 8 spaces on the left for the first row, 7 spaces on the second row, 6 on the third row, 5 on the fourth row (last row of triangle), two rows of spaces and then we can start the middle section.

Figure out these dimensions first and write them down. Look for the patterns that they make and then you will have an idea of where you put your loops and conditions.

Through our discussion we have identified the boundaries of our loops for shapes, and then the spaces needed to get shapes into position. All that is left to do is plug in the variables and run some tests. Always use the sample input at first and compare its output with that in the assignment before you continue. You could make things even easier and hardcode those sample values into the program at the top, that way you save yourself some time of having to enter them over and over again. Once you have the patterns down, you can then replace the hardcode with user inputted values and retest.

Hope this helps with the overall processing of this assignment. :)

This post has been edited by Martyr2: 18 November 2008 - 11:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 anam   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-November 08

Re: need some guidance

Posted 18 November 2008 - 12:05 PM

Thank you very much for giving such an elaborate explanation.I really appreciate your help.I will start working on it right now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1