Tuesday, July 14, 2009

C PROGRAMMERS!! Write a program to do the following.....?

Write a program to do the following:


(a) use a loop to print out the numbers from 1 to 50; the end result should look like this:


1 2 3 4 5 6 7 8 9 10


11 12 13 14 15 16 17 18 19 20


21 22 23 24 25 26 27 28 29 30


31 32 33 34 35 36 37 38 39 40


41 42 43 44 45 46 47 48 49 50


(b) in main() you will have a loop that generates the line number for each line. It will pass


this line number to a function; the function will use the line number to determine the


column values to print out. note that the coordinates of any particular value are (row,


column) with (1, 1) in the upper left corner. NOTE: I rewrote this bullet point to make


the requirements more clear.


(c) do this without using arrays, any kind of lookup table, or just hard-coding values, i.e.,


don’t do this


if (row == 1)


printf(" 1 2 3 4 5 6 7 8 9 10");


(d) the numbers should line up

C PROGRAMMERS!! Write a program to do the following.....?
Merry Christmas. For New Years you need to promise yourself you are going to learn this stuff.





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





void print(int rownum)


{


int start, end;





start = rownum * 10 + 1;


end = start + 10;





for (; start %26lt; end; start++)


{


printf("%02d ", start);


}





printf("\n");


}








int main(int argc, char *argv[])


{


int row;





for (row = 0; row %26lt; 5; row++)


{


print(row);


}





}
Reply:use string formatting (printf), it accepts a format code, and some datas, then output the data in the way described by the format code.





for your particular need, you'll need to iterate on this ten times with for:


for (n = smallest; n %26lt; smallest + 10; n++)


printf("%2s ", n)





after that, print a newline





Note: The code above need to import stdio.h





EDIT:


The code above is simple, first the heart of the code is the string formatting:


printf(formatcode, data1[, data2, ...])





This line would print a string with a format defined in the format code. The format code ("%2s ") is a string, as you can see. This particular format code reads as follow: the data is inserted ("%") with width of 2 ("2") as a string ("s") and there is a space in the end (" ").





(string formatting is quite complex to be explained, so for further information about string formatting, see: http://www.cs.utah.edu/dept/old/texinfo/... )





Then, we iterate over the string formatting function ten times per function call using a for-loop. As we know, C (and most other programming language) doesn't automatically print a newline after a print function, so the next call to printf would print the next number beside the last number. At the end of the loop, we print a newline so the next call to the function would print everything on the next line.
Reply:dno if i can


No comments:

Post a Comment