UMBC CMSC 201
Fall '08

UMBC | CSEE | 201 | 201 F'08 | lectures | projects | help

Loop Problems

Write the code for each of the problems below, then click on the problem name to see the answer. The code is well-commented so you can understand what each segment does.

  1. Using a sentinel value of -1, write a while loop that will ask the user to enter positive integers to be summed. The user will enter the sentinel to indicate that she's finished entering the values to be summed.

  2. Using a sentinel value of -1, write a do-while loop that will ask the user to enter positive integers to be summed. The user will enter the sentinel to indicate that she's finished entering the values to be summed.

  3. Using nested for loops write code that will produce the following "ascii art box". ********* * * * * *********

  4. Challenge problem:
    Using nested for loops write code that will produce the following "ascii art window". ********* * * * * * * ********* * * * * * * *********

Loop Problem Solutions

1. the while loop implementation: #include <stdio.h> int main() { int value, total; /* Give directions to user */ printf("This program adds a list of numbers.\n"); printf("Signal end of list with a -1.\n"); /* Initialize total */ total = 0; /* Get the first integer from the user */ printf("Enter an integer, -1 to end : "); scanf("%d", &value); /* Continue to get integers from the user until * a -1 is entered, accumulating into total */ while (value != -1) { total += value; printf("Enter an integer, -1 to end : "); scanf("%d", &value); } /* Print the total */ printf("The total is %d\n", total); return 0; }


2. the do-while loop implementation: #include <stdio.h> int main() { int value, total; /* Give directions to user */ printf("This program adds a list of numbers.\n"); printf("Signal end of list with a -1.\n"); /* Initialize total */ total = 0; /* Get integers from the user until ** a -1 is entered */ do { printf("Enter an integer, -1 to end : "); scanf ("%d", &value); /* don't add the sentinel to the total */ if(value != -1) { total += value; } }while (value != -1); /* Print the total */ printf("The total is %d\n", total); return 0; }


3. nested for loop implementation of drawing an empty box: #include <stdio.h> int main() { int i, j; /* for each of the 4 rows */ for (i = 0; i < 4; i++) { /* for each of the 9 columns */ for (j = 0; j < 9; j++) { /* if this is the top border or the botton * border print stars all the way across */ if (i == 0 || i == 3) { printf("*"); } /* for all rows that are neither the top * nor bottom border */ else { /* only print a star if this is the * the right or left border */ if (j == 0 || j == 8) { printf("*"); } /* spaces need to be printed for * the box's interior */ else { printf(" "); } } } printf("\n"); } return 0; }


4. nested for loop challenge of drawing a 4-pane window: #include <stdio.h> int main() { int i, j; /* for each of the 7 rows */ for (i = 0; i < 7; i++) { /* for each of the 9 columns */ for (j = 0; j < 9; j++) { /* if this is the top border the center * row or the botton border print stars * all the way across */ if (i == 0 || i == 3 || i == 6) { printf("*"); } /* for all rows that are neither the top * border, the center row, nor bottom border */ else { /* only print a star if this is the * the right border the center column * or left border */ if (j == 0 || j == 4 || j == 8) { printf("*"); } /* spaces need to be printed for * each pane's interior */ else { printf(" "); } } } printf("\n"); } return 0; }



UMBC | CSEE | 201 | 201 F'08 | lectures | projects | help

Monday, 25-Aug-2008 16:22:03 EDT