Friday, July 31, 2009

Question about a program in C language,?

can someone give me d code to print d following series in C.


1


2 2


3 3 3


.


.


.


.


and so on...........


i actualy want it in form of a tree.


1 in d centre, 2 on either side of 1 on next line and hence......

Question about a program in C language,?
#include %26lt;stdio.h%26gt;





int main(void)


{





//represents the row


for(int i=0;i%26lt;10;i++){


//puts spaces in that aligns to the right side


if(i!=0){


for(int p=0;p%26lt;i;p++){


printf(" ");


}


}


//draws the numbers in column


for(int k=10-i;k%26gt;0;k--){


printf("%d",k);


}


//new line


printf("\n");





return 0;


}
Reply:This so-called "best answer" didn't even compile! So to even suggest that this was the best answer is ridiculous. Report It

Reply:for(L=1; L%26lt;=3; L++)


{


for(i=1; i%26lt;=40-L; i++)


printf(" ");


for(j=1, x=1; j%26lt;=2*L; j++,x++)


printf("%d",x);


}





try that.. ijust freehanded it.. i dont really know if it works..


just modify it if there are problems..you can do it..
Reply:Ok, I am going to mark this "interesting". It strikes me that we want as simple a solution as possible, as long as it works in standard (ANSI) C. Here goes:





#include %26lt;stdio.h%26gt;


/* Width of output */


#define W 80


/* Maximum count */


#define M 10





/* Generate i repetitions of character c, returning a string */


char *reps(int i, char c, char *buf)


{


buf[i] = '\0';


while (i) buf[--i] = c;


return buf;


}





int main(void)


{


int i;


char buf[M];





/* We use the * format option to choose field width, and


this is used to center the line. reps() is used to generate


the output data. */


for (i = 1; i %26lt; M; ++i) printf("%*s\n",(W + i) / 2, reps(i, '0' + i, buf));


return 0;


}





This works -- with the caveat that the even strings are always offset from the odd length strings (we can't print 1/2 of a character).





Another solution is to use two printf()'s. The first would generate a null string with padding to the length of the select string. Then, we replace the padding spaces with the target character, and printf() the result with padding to effect the desired centering. We still need to scan the print string for the replacement, so it ends up being more complex than this code. Or, we could use counters, and putc()...
Reply:Here you go (this one actually works) but your prof will never believe you came up with this on your own. :-)





//////////////////////////////////////...


#include %26lt;string.h%26gt;


#include %26lt;stdio.h%26gt;





#define MAXNUM 20





int main(int argc, char **argv)


{


int i, j;


char numbuf[3] = {0x30, 0x20, 0};


char outbuf[MAXNUM * 2 + 1];


char padbuf[MAXNUM * 2];





// calc what the width from left edge to center


// will be for the bottom row


int basewidth = (MAXNUM * 2) - 1;





// fill padding buffer with spaces, to


// longest length needed


for (i = 0; i %26lt; MAXNUM * 2; i++)


padbuf[i] = 0x20;





for (i = 1; i %26lt; MAXNUM + 1; i++)


{


// add 0x30 to last digit of iterator


// to get ascii value of chars '1' - '0'


numbuf[0] = (i % 10) + 0x30;





// truncate output buffer


outbuf[0] = 0;





// fill output buffer with repetition of number


// buffer


for (j = 0; j %26lt; i; j++)


strcat(outbuf, numbuf);





// shorten the padding buffer by null-terminating


// it at the correct length for this level


padbuf[basewidth - ((i * 2) - 1)] = 0;


printf("%s%s%s\r\n", padbuf, outbuf, %26amp;outbuf[2]);


}


return (0);


}

love song

No comments:

Post a Comment