I need a hint on how to do this.
*
***
*****
*******
********
draw this figure
Using only 2 couts, one for
a single space, and one for
a single *. You can use
as many loops as you need
i know you have to use setw to do it
[364 byte] By [
k0b381] at [2007-11-20 11:32:51]

# 1 Re: I need a hint on how to do this.
I assume you meant this?
*
***
*****
*******
*********
If so, then the first point is that the first loop would loop 9 times for each row (because the length of the bottom row is 9).
It is true that you can do it using one cout for the a ' ' and one for a '*'. However, you will need to know the width of the console window. If you do not know the width of the console window, then you will also need a cout for a new line (cout << endl).
I spent a couple of minutes solving this and would give the following pointers.
1) Set an integer variable at the start of your main function (before any loop has occurred) that will record the longest row length (the maximum number of stars in a row, which in the case you showed would be 9). I'll call this variable longestRowLength.
2) Set another integer variable at the start of your main function (before any loops) that records the number of rows (i.e. the number the first loop should count to). The number of rows can be calculated as follows, numberOfRows = (longestRowLength/2)+1.
3) Create your first loop which will be the outer loop and will count from the number of rows down to 0 - this is a decrement loop.
for (int i= numberOfRows; i--;)
{
...
}
The first thing to do inside this loop is declare two integer variables, the first one is the number of stars the current row contains, and the other is the number of spaces in the row which need to be printed first. These can be calculated as follows
int numberOfStars = longestRowLength - i*2;
int numberOfSpaces = (longestRowLength - numberOfStars)/2;
Now all you need to do is create two consecutive loops within the outer loop, but after the above variable declarations. The first loop, loops over the number of spaces (this loop contains std::cout << ' '; ). The second loop, loops over the number of stars (this loop contains std::cout << '*'; ). The last thing that you need to do is add a cout statement for a new line just before the closing curly brace of the outer loop (std::cout << std::endl; ).
The above is possibly more than a pointer, it's the solution in words, but at least you still need to code it. :) I hope this helps.
# 2 Re: I need a hint on how to do this.
you don't use setw?
# 3 Re: I need a hint on how to do this.
i dont understand
# 4 Re: I need a hint on how to do this.
No, it is not needed (if it is indeed true that the couts are restricted such that one is definitely for ' ' and the other definitely for '*'). As I said in my above post, if this is the case, then you need one extra cout for the end of line.
However, if the problem is to create the output using two couts only, and one cout is for '\n' and the other is for '*', then it is possible to create the output using only these two couts in conjunction with setw.
# 5 Re: I need a hint on how to do this.
oh ok just wondering because thats what he just learned in class and i thought we would have to apply setw to it
# 6 Re: I need a hint on how to do this.
Why is this giving me an error?
#include <iostream.h>
int main()
{
int a;
int b;
char num1=42;
for (b=1;b<=9;b++)
{
cout<<" ";
}
for (a=1;a<=1;a++)
{for (a=;a<=3;a++)
{
cout<<num1<<endl;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
# 7 Re: I need a hint on how to do this.
nvm found the error
# 8 Re: I need a hint on how to do this.
*
***
*****
*******
*********
i have to make that, sorry it didnt come out right
# 9 Re: I need a hint on how to do this.
a *
a ***
a *****
a*******
a *********
there i hope its right this time
# 10 Re: I need a hint on how to do this.
GRRRR its suppose to look like
xxxxxxx*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxx***xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxx*****
*******xxxxxxxxxxxxxxxxxx
xxxxxxxx*********
ignore the x's just look at the *
# 11 Re: I need a hint on how to do this.
finally thats what its suppose to look like sry i couldnt get it to come out right in the other posts
# 12 Re: I need a hint on how to do this.
GRRRR its suppose to look like
xxxxxxx*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxx***xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxx*****
*******xxxxxxxxxxxxxxxxxx
xxxxxxxx*********
ignore the x's just look at the *
Extra Credit Lab: draw this figure
Using only 2 couts, one for
a single space, and one for
a single *. You can use
as many loops as you need
# 13 Re: I need a hint on how to do this.
linux3[45]% more temp.cpp
#include <iostream>
using namespace std;
int main( int argc, char** argv )
{
for( int i = 0; i < 10; ++i )
{
if( i % 2 ) // Only print odd lines
{
// Print the *'s
for( int j = 0; j < i; ++j )
cout << "*";
// Print the ' ''s (in case you need to link triangles in the future.)
for( int x = 0; x < ( 10 - i ); ++x )
cout << " ";
cout << endl;
}
}
return 0;
}
linux3[46]% g++ -Wall temp.cpp -o temp
linux3[47]% temp
*
***
*****
*******
*********
linux3[48]%
# 14 Re: I need a hint on how to do this.
nice lol
# 15 Re: I need a hint on how to do this.
that still doesnt work... it doesnt make this
xxxxxxx*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxx***xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxx*****
*******xxxxxxxxxxxxxxxxxx
xxxxxxxx*********
# 16 Re: I need a hint on how to do this.
that still doesnt work... it doesnt make this
xxxxxxx*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxx***xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxx*****
*******xxxxxxxxxxxxxxxxxx
xxxxxxxx*********
Modify the loops and I'm sure you can come up with your solution :).
# 17 Re: I need a hint on how to do this.
ive tried modifying the loops and it still gives me
*
***
*****
*******
*********
# 18 Re: I need a hint on how to do this.
who made c++ so confusing