for() statements problem

hello. can u give me the code for this output:
1
2
3
4
5
    *
   ***
  *****
 *******
*********


using the ' for() ' statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream.h>
#include <conio.h>

void main ()
{
	int col=20,row=5;


	for (int x=1;x<=5;x++,row++,col++)
	{
		gotoxy(col,row);
		for(int y=1;y<=5*2;y++)
		{
			cout<<"*";
		}
		cout<<endl;
	}

}

this output is square like this one:

**********
**********
**********
**********
**********
Last edited on
Edit: You edited your post and changed the question ;-) Don't know what to suggest this time.
Last edited on
what does gotoxy(col,row); do? you should count how many * you have made and then make a linebreak.
gotoxy(col,row); to put the output in the middle.
still count how many stars you made, like this
1
2
3
4
5
6
7
if(count != 1)
{
cout<<"*";
}else
{
cout<<"/n";
}


please post a new question instead of just editing your current topic
1
2
3
4
5
6
7
if(count != 1)
{
cout<<"*";
}else
{
cout<<"/n";
}

what's this? for what is that?
where I'll put that?
it is an example, as i said, count how many * you have made with a int variable and then just check to see if you should make a new line and start over with more *
i cant understand what your trying to say. T.T
sorry forgot that you already had made some code, my bad ;) but i think that the easiest way to do it is to make a for loop for each row that you want to make. that's the only solution that jumps to mind ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
#include <iomanip>

int main() { 
	
	int depth = 0;
	
	do {
		cout << "Enter depth: ";
		cin >> depth;
	} while (depth < 1);
			
	int i = 0;
	int b = 0;

	for (i; i < depth; i++) {
		cout << setw(depth - i) << "";
		for (b = 0; b < ((i * 2) + 1); b++) {
			cout << "*";
		}
		cout << endl;
	}

	return 0;
}
@Mathes: Don't just post code. The OP is unlikely to learn anything if you do that.
well the question was:
"can u give me the code for this output:"

so i thought i give him the code and if he posts any questions ill try to explain.

but your right, a little explanation right away wouldnt hurt me, so here we go:

i think everything till line 18 is no big deal. when you want the exact output of your example, you would enter 5.

i think the hardest part here is to get setw(depth - i) and b < ((i * 2) +1)
so ill explain how i got these. i think the implementation should be easy to understand after that.

with depth and the knowing about how the triangle is build we can get a lot of usefull values (exactly every value we need to print it ;) )

depth: the number of rows, entered
nos: number of stars in a row (thats what i called b in the code), rownumber (i) * 2 + 1 (since we go with 1, 3, 5, 7, 9 etc and started with i = 0)
maxwidth of a row (the nos of the last row) is (depth - 1) * 2 + 1, because we started with i = 0, the max i can be i = depth - 1
the last thing we need is the number of blanks/spaces before we start printing the * in the rows.
since we know the nos and maxwidth, the number of blanks is:
1
2
3
4
5
nob = (maxwidth - nos) / 2
= (((depth -1) * 2  + 1) - (i * 2 + 1)) / 2
= (2depth - 2 +1 - 2i  - 1) / 2
= (2depth - 2i - 2) / 2
= (depth - i - 1)


you may wonder why i have setw(depth - i) in the code, well thats just because i think it doesnt look good, when the triangle hits the edge(?) of the shell. so by not using "-1" i just shifted the whole thing 1 position to the right.
if that confuses you, im sorry. just use setw(depth - i - 1) if you want to implement the fomula 1 by 1. ;-)
when you understand this calculations i think its easy to understand the code.
Last edited on
I understand your intent entirely but I have a feeling that our OP here doesn't care about understanding the code. If you give it to them, they will just copy and paste it into their assignment and enjoy the free grade you gave them.

Few, if any, who just post "can i hav code plz" are actually interested in learning.
lol. im not posting my assignment here.
this is just how i learn how to make some code.

mathes. thanks for the code but there's another code.
#include <iomanip> should not be in there.
should be #include <iostream> only.
my teacher only use that. and for() statements.

1
2
3
I understand your intent entirely but I have a feeling that our OP here doesn't care about understanding the code. If you give it to them, they will just copy and paste it into their assignment and enjoy the free grade you gave them.

Few, if any, who just post "can i hav code plz" are actually interested in learning. 


im not posting my assignment. what i'm posting is finished assignment given.
i just want to know how is the answer. my teacher is not giving the code even it's finish.
hello. can u give me the code for this output:


Hmm....
lol...hilarious
@ecomoda:
you're welcome.
if you are not allowed to use iomanip, just remove it and change the
setw(depth - i - 1) to
1
2
3
for (b = 0; b < (depth - i - 1); b++) {
			cout << " ";
		}
same result
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;


int main() {

string homeWork = "hello. can u give me the code for this output:";
string homeWork1 = "lol. im not posting my assignment here.";

string homeWork2 = "#include <iomanip> should not be in there. 
should be #include <iostream> only my teacher only use that. and for() statements.";

string homeWork3 = "im not posting my assignment. what i'm posting 
is finished assignment given. i just want to know how is the answer. 
my teacher is not giving the code even it's finish";

bool isAssignment = 1;

if(isAssignment == 1){

cout << homeWork << endl << endl;
cout << homeWork1 << endl << endl;
cout << homeWork2 << endl << endl;
cout << homeWork3 << endl;

}else{

 cout <<" I could have sworn.... " << endl;

}

return 0;
}



I'm sorry, I'm just having alittle fun. I couldn't resist. :)

I'm curious though, was there a better way for me to code that?
Last edited on
thanks mathes.
now i know how that happen.

i'll mark this as finish.

close thread. (^_^)

thanks to all.
Topic archived. No new replies allowed.