hello,
how can i get an o/p that's basically if i/p is 4
then o/p should be
*
**
***
****
using for loop
ican get only one * per line
so how can i get o/p in reqd format
please suggest!
hello,
how can i get an o/p that's basically if i/p is 4
then o/p should be
*
**
***
****
using for loop
ican get only one * per line
so how can i get o/p in reqd format
please suggest!
deja vu. this homework assignment shows up quite often.
Think counter.... think spitting out the number of asterisks that is in your counter.... think incrementing the counter each loop iteration.
That is all. I hope nobody gives you any code.
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
hey iam not asking for code: afer here iam kind of cofused. how can i have multiple * is what iam struck at
# include <iostream.h>
main()
{
int n ,i;
cout<<"enter pos no";
cin>>n;
for ( i=1;i<=n;i++)
cout<<"*";
}
return 0;
}
ok, so I imagine what you are seeing is n stars. This is because you are drawing one star per iteration. But what you want to do is drop down a line each iteration right? you know how to do that I assume. plus... each iteration needs to draw "i" stars. another loop inside maybe? understand where I'm going?
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
thank you for the tip.i got the o/p
Code tags added by HammerCode:# include <iostream.h> main() { int n ,i,j; cout<<"enter pos no"; cin>>n; for ( i=1;i<=n;i++) { for ( j=1;j<=i;j++) { cout<<"*"; } cout<<endl; } return 0; }
Last edited by GMK; 11-07-2002 at 02:13 PM.
glad to hear it. I wasn't attacking you're post by the way. I just wanted to head anyone else off who might post the code.
also, use code tags and your code will keep its indenting.
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
hello,
i waswondering why do we need to add the return 0 at the end of each prg. why warning is generated if this is not used and what is difference if we use void in the main function
void main (void) and void main()
ANSI standard main returns an int. What does this mean to the system? very little. void works in many compilers but some people and some compilers stick to the ANSI standard religiously.
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
if figuring out that type of puzzle provided a sense of accomplishment, you can further challenge yourself by trying to display the triangle as an isosoles(sp?) triangle (2D pyramid), and once you have that figured out try to draw a diamond shape. It is easier/prettier if you use only an odd number of * per row for these shapes.
Or prelude's fractal triangle
I would still like to see that code again, i can't find it anymore.. Links anyone?
~Inquirer
Compilers:
GCC on Red Hat 8.1 (Primary)
GCC on Mac OS X 10.2.4 (Secondary)
Others:
MinGW on XP
>I would still like to see that code again, i can't find it anymore.. Links anyone?
It was probably something like this:
-PreludeCode:#include <iostream> int main() { int i, j; for ( i = 0; i < 16; i++ ) { for ( j = 0; j <= i; j++ ) std::cout.put ( ~i & j ? ' ' : '1' ); std::cout.put ( '\n' ); } return 0; }
My best code is written with the delete key.
>It was probably something like this:
Actually, it was exactly like this...
(I liked it, too. )Code:#include <iostream> const int N = 16; int main () { for ( int n = 0; n < N; ++n ) { for ( int k = 0; k <= n; ++k ) std::cout.put ( ( ~n & k ) ? ' ' : '*' ); std::cout.put ( '\n' ); } std::cin.get(); }
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
hello,
i would like to try that right angle triangle and also prelude's fractal triangle
how should the o/p look like ?
please suggest
thanx
This question didn't get answered last time:
>> What exactly does "( ~n & k )" test for?
Thanks!
~Inquirer
Compilers:
GCC on Red Hat 8.1 (Primary)
GCC on Mac OS X 10.2.4 (Secondary)
Others:
MinGW on XP
Since this is Prelude's code, she can feel free to yell at me...not really...but let's take a shot.
'~' inverts the bits of a given value, in this case, 'n'. ('~' is typically used in a destructor.)Code:std::cout.put ( ( ~n & k ) ? ' ' : '*' );
(You can see where this is going already, can't you?)
The bit values of 'n' are inverted then 'AND'ed against the bits of 'k'. Both must be '1' for the AND expression to evaluate the bit to '1', otherwise the bit evaluates to '0'.
Ex.:
n = 0001
~n = 1110
k =0101, therefore,
~n & k = 0100
The rest is semi-self-explanatory (I think that's a new word ). If the value of (~n & k) is '0' (false), we print an asterisk. If any of the bits evaluate to '1', the value is "true" - for those extremely new to the language, anything other than '0' is "true" - and we print a space character.
Play with it on paper to really convince yourself. Note that when the inner loop has completed, we go to the next line.
(Danged if I know how that young lady figured this out, though. )
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow