Hi everyone,

Today i have a question about how can i write one code for all shapes like, ******
*****
****
***
**
*

or other shapes like square or tringle.

I want to know the difference in every code.
Thanks alot.

Recommended Answers

All 18 Replies

There's isn't a magic code to draw 'all shapes'
I guess this is a homework assignment to learn loops?
If yes: Post your attempt here (are tell us what part you don't understand), and you'll get help.
if no: The easiest way to draw the shape in your post is: std::cout << "*****\n****\n***\n**\n*";

Or else you do a few math calculations, then set up a function to draw individual shapes....

For example, you could create a "square" function:

void draw_square(int sideLength);

which basically loops and outputs a square (of the specified size, using a loop) to the screen...

Advantage: you can create reusable shape-builders...to make more shapes...haha

EDIT: niek_e is right, what specifically do you need help with, rather than us just posting code...

There's isn't a magic code to draw 'all shapes'
I guess this is a homework assignment to learn loops?
If yes: Post your attempt here (are tell us what part you don't understand), and you'll get help.
if no: The easiest way to draw the shape in your post is: std::cout << "*****\n****\n***\n**\n*";

Our doctor want us learn how to make these shapes by using loops for exam question.She gave us a question ,we should use loop when solving it.

#include <iostream>
using namespace std; // So the program can see cout and endl

int main()
{
int n = 4;
int line, stars, loop;
for (line = 1; line <= n; line++)
{
                   stars = n - line + 1;
    for (loop = 1; loop <= stars; loop++)
        cout << "*";
    cout << endl;
}

}
The shape will be the same as what i draw.

Or else you do a few math calculations, then set up a function to draw individual shapes....

For example, you could create a "square" function:

void draw_square(int sideLength);

which basically loops and outputs a square (of the specified size, using a loop) to the screen...

Advantage: you can create reusable shape-builders...to make more shapes...haha

EDIT: niek_e is right, what specifically do you need help with, rather than us just posting code...

Could you tell me how can i use Math caculations to write these kind iof codes.
Becus sometime i found in other wbsites they use Math calculations.

Right, well, I'm not sure I understand what you are asking...

Which shapes do you need to learn how to draw? Just a square/triangle? That would be where I would start...

Assuming your exam is a written exam, I suspect your instructor wants to examine your ability to trace through the iterations of a loop...try to understand how the variables change after each iteration and how that affects your output (when outputting to the console).

As for math calculations...that could really mean anything. For example, if you were given the side length of a square, you could classify a calculation as determining the dimensions of the square, and consequently how to construct an appropriate loop to render it onto the screen/console.

Math calculations could mean extending a sequence of numbers (for example, the triangular numbers 1, 3, 6, 10, 15, ...) to construct a triangle.

Moreover, math calculations could also pertain to rendering shapes on a (cartesian) coordinate system.

So what do you mean by math calculations?

Right, well, I'm not sure I understand what you are asking...

Which shapes do you need to learn how to draw? Just a square/triangle? That would be where I would start...

So what do you mean by math calculations?

Please look to this code

#include <iostream>
using namespace std; // So the program can see cout and endl

int main()
{
int n = 4;
int line, stars, loop;
for (line = 1; line <= n; line++)
{
                   stars = n - line + 1;
    for (loop = 1; loop <= stars; loop++)
        cout << "*";
    cout << endl;
}
}

they use a simple math colculation stars = n - line + 1; in order to output the shape.However, i think there are a relationship between some shapes -correct me if iam wrong-
Which shapes i'd like to learn like differnt shapes of triangle ,sequsre, rectangle ,but i guess that triangle shapes will be come on the exam ^_^

Could any one answer my questions??

Could any one answer my questions??

I can't figure out what the questions are. The code you posted draws a triangle. Change it slightly to draw a square:

int main()
{
     int n = 4;
     int line, loop;
     for (line = 1; line <= n; line++)
     {
          for (loop = 1; loop <= n; loop++)
               cout << "*";
          cout << endl;
     }
     return 0;
}

You can change it slightly to draw a rectangle too. More changes make diamonds, inverted triangles, pyramids, etc. They almost all have one thing in common: a loop within a loop. The inner loop doesn't contain cout << endl; . The outer loop does.

Our doctor want us learn how to make these shapes by using loops for exam question.

sweet.

my doctor just wants me to bend over and take a deep breath. :(

i should look into changing plans.


.

Thank you for all your replies....

#include<iostream>
using namespace std;
int main()
{
	for(int x=1;x<=6;x++)
	{
		for(int y=1;y<=x;y++)
		{
			cout<<"*"; 
		}
		cout<<endl;
	}
}

finaly tou will get

*******
	******
	*****
	****
	***
	**
	*
commented: way to destroy your credibility on your very first post -2

let's see, here....

this thread is over a year old and is already solved.

so, you post a "solution" that actually does the backassward result of what you claim it does? not to mention a loop that goes from 1-6 will generate 7 stars?

looks to me like you've just come in here MAKING STUFF UP!

well friend I think thats you can do it by loops , playing with the cout''s also by using ( \t ) - ( \n ) .........
I think that your solution was the basic step for all others shapes
I will list some for you , I hope that its allowed

--------------------------------------------------------------------------------------

*
  ***
*****
    *
    *
    *
    *
    *
#include <iostream>
using namespace std;
//This program was written by the Programmer Ali Denno
void main ()
{
	int x, k, i, j;
	for(k=1;k<=8;k++)
	{
		if(k!=2 && k!=3)
		{
			for(i=1;i<=5;i++)
			{
				if(i==3)
					cout<<"*";
				else
					cout<<" ";
			}
			cout<<endl;
		}
		else
			if(k==2)
			{
				for(j=1;j<=5;j++)
				{
					if(j==1 || j==5)
						cout<<" ";
					else
						cout<<"*";
				}
				cout<<endl;
			}
			else
			{
				for(x=1;x<=5;x++)
				{
					cout<<"*";
				}
				cout<<endl;
			}
	}
}

--------------------------------------------------------------------------------------

***
  *      *
*          * 
*          *
  *      *
    ***
#include <iostream>
using namespace std;
//This Program was written by the programmer Ali Denno
void main()
{
	int x, i, j, b;
	for(x=1;x<=6;x++)
	{
		if(x==1 || x==6)
		{
			for(i=1;i<=7;i++)
			{
				if(i==3 || i==4 || i==5)
					cout<<"*";
				else
					cout<<" ";
			}
			cout<<endl;
		}
		else
			if(x==2||x==5)
			{
				for(j=1;j<=7;j++)
				{
					if(j==2||j==6)
						cout<<"*";
					else
						cout<<" ";
				}
				cout<<endl;
			}
			else
			{
				for(b=1;b<=7;b++)
				{
					if(b==1||b==7)
						cout<<"*";
					else
						cout<<" ";
				}
				cout<<endl;
			}
	}
}

--------------------------------------------------------------------------------------
an empty square

#include <iostream>
using namespace std;
//This Program was written by the programmer Ali Denno
void main()
{
	int i, j, k;
	for(k=1;k<=6;k++)
	{
		if(k==1||k==6)
		{
			for(i=1;i<=6;i++)
				cout<<"*";
			    cout<<endl;
		}
		else
		{
			for(j=1;j<=6;j++)
			{
				if(j==1||j==6)
					cout<<"*";
				else
					cout<<" ";
			}
			cout<<endl;
		}
	}
}

-----------------------------------------------------------------------------------

commented: More clueless flogging a dead horse with code (untagged) -7

help pls... how can i make a square with another square inside and my name in the middle using for loop

Write a nested for loop that will print the following output:

4321
432
43
4

+   +   +   +   +
+   +   +   +   
+   +   +       
+   +           
+               
+   +           
+   +   +       
+   +   +   +   
+   +   +   +   +
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.