How i can generate the followning pattern by using for loop and one printf! please help! im stuck!!
the pattern
*
**
***
****
*****
******
*******
********
*********
**********
How i can generate the followning pattern by using for loop and one printf! please help! im stuck!!
the pattern
*
**
***
****
*****
******
*******
********
*********
**********
Hint:
You need two for-loops to do this.
Code:for (line = 0; line < nr_of_lines; line++) for (nr_of_stars = 0; nr_of_stars < line; nr_of_stars++) /* end of hint */
I believe this has the output that you want.
EnjoyCode:x=6,_x=7,x_=7;main_(x,_x) {return(x>0?x_--,main_(x- printf("%c",_x),_x):x_);} main(){x>0?main_(main_(x, 32),42),x_+=_x+=1,printf ("%c",'\n'),--x,main():0;}
-Prelude
My best code is written with the delete key.
here is the code u want, dont listen to prelude!
Code:int main () { int line, star; for(line=1; line<=10; line++) { for(star=1; star<=line; star++) { printf("*"); } printf("\n"); } system("PAUSE"); return 0; }
>here is the code u want
I can guarantee that the formatting didn't allow spaces in the OP's post. The triangle is supposed to look like this:
So it's not the code he/she wants.Code:* *** ***** ******* ********* ***********
>dont listen to prelude!
Do listen to Prelude if you want to actually learn something instead of simply copy/pasting code to turn in for homework. I can guarantee that if you decipher the program I posted you'll learn quite a bit more about C than if you simply solve the problem at hand and forget about it. And please don't try to solve homework problems, you'll only encourage dependency on others more.
-Prelude
My best code is written with the delete key.
i thought he was making a game, cause i made a game like that once, oops, didn't think it would be homework.
anyways what u wrote is impossible to read for someone who just started programming.
>what u wrote is impossible to read for someone who just started programming
So think about how much they could learn by taking the time to figure it out.
-Prelude
My best code is written with the delete key.
Thank u all for helping me!
Dont worry Prelude! When i posted my question, i was expecting only a hint to lead me to the solution, i didnt want anyone to do my homework to me!
Now, i will try to write the program by myself and to read your code!! ( i guess it will take me a million years to write a code like that!!)
ps: what does system("pause") do?!
prints:
"please enter a key to continue"
you press a key it continues with program,
its called system because its a system command (i think)
thanx for the new info!!
Would you mind explaining your code to me, Prelude?
I think I got it, except I'm having trouble understanding the ternary operators. For example, I don't get how the fricking ',' in line 2 works? If x>0, is the return value supposed to be x_-- or main_(x-printf("%c",_x)? Arggggh! Not to mention that horrible construct of Satan in the second to last line!Code:x=6,_x=7,x_=7;main_(x,_x) {return(x>0?x_--,main_(x- printf("%c",_x),_x):x_);} main(){x>0?main_(main_(x, 32),42),x_+=_x+=1,printf ("%c",'\n'),--x,main():0;}
I believe they have a name for this type of code...
unmaintainable. Quite difficult to look at and understand, what I do is spread it out, read it then compact it again.
example:
Code:x=6,_x=7,x_=7;main_(x,_x) {return(x>0?x_--,main_(x- printf("%c",_x),_x):x_);} main(){x>0?main_(main_(x, 32),42),x_+=_x+=1,printf ("%c",'\n'),--x,main():0;}Code://spread out as much as possible int x=6,_x=7,x_=7; main_(x,_x) { return(x>0?x_--,main_(x- printf("%c",_x),_x):x_); } main() { x>0?main_(main_(x,32),42),x_+=_x+=1,printf("%c",'\n'),--x,main():0;}
Asking the right question is sometimes more important than knowing the answer.
Please read the FAQ
C Reference Card (A MUST!)
Pointers and Memory
The Essentials
CString lib
It's generally called obfuscated code, there are competitions with this kind of stuff too.
I don't imagine prelude writes her everday code like this( at least I hope not!!! ).
I suspect she was just showing off
Last edited by C_Coder; 05-04-2002 at 03:10 PM.
All spelling mistakes, syntatical errors and stupid comments are intentional.
Personally I love doing other peoples homework and I don't care whether or not they learn anything.
The ternary operator works like an if..else statement with some restrictions. For example, you can't use continue, break, or return from a ternary expression and under normal circumstances only one operation can be performed in each segment:I think I got it, except I'm having trouble understanding the ternary operators. For example, I don't get how the fricking ',' in line 2 works?
condition ? one operation : one operation;
This is where the comma operator comes in. By using the comma operator you can place a sequence point in the middle of an expression where otherwise the only way to place a sequence point would be an end of statement semicolon. So with the comma operator you can perform any number of operations in the ternary expression. It's a neat trick that you should avoid except when writing obfuscated code:
condition ? operation, operation, operation : operation, operation;
This is a tricky move. If the condition returns true then the function will recurse and perform the proper decrements. Notice that the function does not return if the condition is met. If the condition returns false then a value is truly returned. Think of it like this:If x>0, is the return value supposed to be x_-- or main_(x-printf("%c",_x)? Arggggh!
Code:if ( x > 0 ) { x_--; main_ ( x - printf ( "%c", _x ), _x ) } else return x_;I assume you mean this?Not to mention that horrible construct of Satan in the second to last line!
x>0?main_(main_(x,32),42),x_+=_x+=1,printf("%c",'\ n'),--x,main():0;
That part I enjoyed making. I went out of my way to get main_ to return the correct value so that it could be used as an argument to itself.
>Quite difficult to look at and understand, what I do is spread it out, read it then compact it again.
It also helps to make unique names so that it's easier to follow what variable has what value. The choice of main_ for the other function was no accident.
>I don't imagine prelude writes her everday code like this( at least I hope not!!! ).
No, I like my job.
>I suspect she was just showing off
Sort of. This is the code I use to answer the triangle homework question on IRC.
-Prelude
My best code is written with the delete key.