Hello,

I've been wondering about how to make shapes out of stars (*). Like a rectangle and different stuff.


Is there a way for that ? and how ?


Thanks all,

The simple answer is:

std::cout << "***\n***\n***\n";

and you'll have a square of stars. But you probably want to use loops. So what do you know about loops already?

Aha .. I got that ..

what if I wanted the square to surround some writing
----
answering your question ... I know "While loop" I have not yet perfected it I am still reading and looking .

Aha .. I got that ..

what if I wanted the square to surround some writing
.

cout<<"*******************\n";
cout<<"*  Writing stuff  *\n";
cout<<"*******************\n";
cout<<"*******************\n";
cout<<"*  Writing stuff  *\n";
cout<<"*******************\n";

Thanks for replying .. I already did that .. I was looking for a sort of special way

Try Lua, or there is a way to do them in console using for loops.

Thanks for replying .. I already did that .. I was looking for a sort of special way

Ahh but there is, you just have to create it. Here is an example :

#include<iostream>
#include<string>

using namespace std;

void SquareString(string str)
{
	int len = str.length();

	int max = len * 2;

	if(max < 3) max = 3;

	for(int i = 0; i < max; i++)
		cout<<"*";

	cout<<endl;

	cout<<"*";

	int Lim = max/4 - 1;

	for(int i = 0; i < Lim; i++)
		cout<<" ";
	
	cout<<str;

	if(str.size() % 2 == 1)
		Lim++;

	for(int i = 0; i < Lim; i++)
		cout<<" ";
	
	cout<<"*"<<endl;	
	

	for(int i = 0; i < max; i++)
		cout<<"*";
}
int main()
{
		
	for(int i = 0; i < 26; i++)
	{				
		string str =  "";
		str += char ( 'A' + i);

		for(int j = 1; j < i+1; j++)
			str += str[0];

		SquareString(str);
		cout<<"\n\n";
	}
	return 0;
}

see this code :

# include <iostream>
using namespace std;

int main ()
{
//a
	int i,j;
	for ( j=1 ; j<=5 ; j++)
	{
		for ( i=1 ; i<=j ; i++)
		{
			cout << "*"; 			
		}

		cout<< endl;
	} 

	cout << endl << endl; 


   //b
	for (j=5; j>=1; j--)
	{
		for (i=1; i<=j; i++)
		{
			cout<<"*";
		}
		cout<<endl;
	}

	cout << endl << endl;


    // C
	for (j=1; j<=5; j++)
	{
		for (i=1; i<=j; i++)
		{
			cout<<" ";
		}
		for (i=j; i<=5; i++)
		{
			cout<<"*";
		}
		cout<<endl;
	}
	cout << endl << endl;


	//D
		for (j=1; j<=5; j++)
	{
		for (i=j; i<=5; i++)
		{
			cout<<" ";
		}
		for (i=1; i<=j; i++)
		{
			cout<<"*";
		}
		cout<<endl;
	}
	return 0;
}

I understood the first two shapes

a and b

but I could not figure out how "c" and "d" work

:(

Well, what are you trying to accomplish? What shape are you trying to make? Map it out on graph paper, with coordinates. These almost all have nested loops. The outer loop draws the lines. If you have seven lines, you'll likely go through the outer loop 7 times. The inner loop (or inner loops, plural) involve drawing a character at a time. First you figure out what the pattern is. How many characters do you need to draw on a particular line? Those are the asterisks usually. The spaces pad the figure till your first asterisk. Always draw the figure on graph paper first, find a pattern, break it down in simple steps on paper, THEN try to program it.

Well, what are you trying to accomplish? What shape are you trying to make? Map it out on graph paper, with coordinates. These almost all have nested loops. The outer loop draws the lines. If you have seven lines, you'll likely go through the outer loop 7 times. The inner loop (or inner loops, plural) involve drawing a character at a time. First you figure out what the pattern is. How many characters do you need to draw on a particular line? Those are the asterisks usually. The spaces pad the figure till your first asterisk. Always draw the figure on graph paper first, find a pattern, break it down in simple steps on paper, THEN try to program it.

I tried to do what you said

I couldn't :(

I just don't get the logic or the patteren

I had an exam about making a equilateral triangle

but each time I kept doing a rhombus

and I tell you if I was asked to do a rhombus I would screw up :'(

..... help anyone ? :(

Thanks a lot

Making an equilateral triangle with asterisks is probably impossible. Isosceles is more doable:

*
  ***
 *****
*******

Look at the shape. What's the pattern? Here's a skeleton:

int num1, num2, num3;
// assign value to num1

for (int i = 0; i < num1; i++)
{
    // assign values to num2, num3

    for (int j = 0; j < num2; j++)
    {
        cout << ' '; // space
    }
    for (int j = 0; j < num3; j++)
    {
        cout << '*'; // asterisk
    }

    cout << endl;
}

Figure out num1, num2, num3 by looking at the pattern, assign them to their appropriate values, and you're done. Same thing with rhombus/diamond. Use the same skeleton:

*
  ***
 *****
*******
 *****
  ***
   *
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.