required output:
can any one please tell me the logic for the required output ?????Code:1 2 2 3 3 3
required output:
can any one please tell me the logic for the required output ?????Code:1 2 2 3 3 3
Last edited by cooldude; 09-08-2009 at 11:04 AM.
This is the first and probably last time I'm going to do someone's homework:
Code:#include <stdio.h> void /* To fail even harder */ main() { printf(" 1\n 2 2\n3 3 3\n"); }
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
You must know, since you're able to do it, right? How'd you make that triangle of digits for your post?
Maybe you took the first number (1) and divided the width of the base of the triangle (which is not even drawn yet), by two.
Idea's in pseudo-code.
Once you get down the steps you can use, in simple terms, then it's easy to start changing it into code, although you'll undoubtedly change it as you go. Thing is, you'll have a starting point, for your program.Code:begin outer loop: (maybe for(i = 0; i < 4; i++) { begin loop for each row: spacesBetweenDigits = SizeofBaseOfTriangle / (numberToPrint+1) print spacesBetweenDigits number of spaces. then print numberToPrint and increment numberYouPrinted by one. loop until numberYouPrinted == numberToPrint print newline } loop until all the rows have been printed
Last edited by Adak; 09-08-2009 at 11:23 AM.
k thanks a lot guys.........
Actually, it is a good practice to make the return type of main() int:
Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])
Okay, I really did it for you this time. 109 chars. Anybody think they can do better? I dare you all ;-).
(note: input through stdin. DONT try to pass it as a parameter ;-)Code:c;main(a,b){a-1||(b=getchar()-48);printf("%*d",b--,c=a);while(--c)printf(" %d",a);puts("\n");b&&main(a+1,b);}
Edit: 103 (I'm bored)
And last for 101Code:c;main(a,b){b=a-1?b:getchar()-48;for(printf("%*d",b--,c=a);c--;printf(c?" %d":"\n",a));b&&main(a+1,b);}
I just HAD to break the 100-score. So for 99:Code:c;main(a,b){for(printf("%*d",b=a-1?b:getchar()-48,c=a);c--;printf(c?" %d":"\n",a));--b&&main(a+1,b);}
Come on, nobody else daring a game of programming golf?Code:b,c;main(a){for(printf("%*d",b=a-1?b:getchar()-48,c=a);c--;printf(c?" %d":"\n",a));--b&&main(a+1);}
Last edited by EVOEx; 09-08-2009 at 03:15 PM.