Starting from:
$25

$19

Homework #4 (C Programming for Beginners - OnLine)


4.1 Here is a listing of a program, use a paper computer and answer how many times each loop is executed

a) How many times this loop is executed?

int i = 0;

while (i++ < 10) {

printf("Hello World: %d\n", i);
}

b) How many times this loop is executed if we changed the counter increment to ++i ?

i = 0;

while (++i < 10) {
printf("Hello World: %d\n", i);

}

    c) How many times this loop is executed, if it is executed immediately after the loop in #b, i.e. i is not reinitialized before the loop?

while (++i < 10) {

printf("Hello World: %d\n", i);
}

4.2 Remember our famous homework#1 program? This program draws a box 20 characters in width and

10 characters in height, using a '-' for horizontal line and '|' for vertical line.

printf("Using brutal force: \n");

printf("--------------------
\n");
printf("|
|\n");
printf("|
|\n");
printf("|
|\n");
printf("|
|\n");
printf("|
|\n");
printf("|
|\n");
printf("|
|\n");
printf("|
|\n");
printf("--------------------
\n");


Only println() is used for this purpose. A good solution could involve a loop, which now you know. So, write the solution - using loop.

Hint: One way to solve this would be by writing a loop to draw top line and nested loops to draw the middle lines and later similar loop like top for the bottom line.
4.2 Change the while loop in 4.2 to do..while loop to print the same box.

4.4 Change the while loop in 4.3 to for - loop to print the same box

4.5 Modify the program in 4.4 and use variables for horizontal, vertical characters instead of hard coded values like '|', '- '. Ask user for all these values height, width, and characters to draw ('|', '-') instead of using hard coded values.

4.5 Modify the program in 4.5 so that it repeats the drawing by asking user to continue. If user enters 'y' then continue drawing more boxes.

More products