ANSWERS TO LOOP EXERCISES



 1. (a)  i = 10, j = 45
    (b)  i = 10, j = 2^10 = 1024
    (c)  i = 15, j was not initialized so it can have any integer value


 2.  It reads in a sequence of integers and prints their sum. The

           while(scanf("%d", &val) != EOF)

    is a C idiom that repeatedly reads in integers and stores them
    in variable val. The scanf() function returns EOF if there is no more
    input; this signals the end of the while loop.


 3.        #include <stdio.h>
           #include <math.h>
           int main(void) {
              double val, sum = 0.0, sum2 = 0.0, avg, stddev;
              int n = 0;
              while (scanf("%lf", &val) != EOF) {
                 n++;
                 sum += val;
                 sum2 += val*val;
              }
              avg = sum / n;
              stddev = sqrt(sum2 / n - avg * avg);
              printf("Average = %f\n", avg);
              printf("Standard deviation = %f\n", stddev);
              return 0;
           }

           The "%lf" is needed with scanf() to read in doubles, but "%f"
           works with printf().  The sqrt() function is in the math library;
           in order to compile you should #include <math.h> and you may
           need to compile with "gcc myprog.c -lm" to link in the math library.


4.         #include <stdio.h>
           int main(void) {
              int pos = 0, neg = 0;     /* counter variables       */
              int val;                  /* integer just read in    */

              while (scanf("%d", &val) != EOF) {
                 if (val > 0)
                    pos++;
                 else if (val < 0)
                    neg++;
              }

              if (pos > neg)
                 printf("positive\n");
              else if (pos < neg)
                 printf("negative\n");
              else
                 printf("equal\n");
              return 0;
           }
 
     This could also be done with a single counter variable that equals the
     number of positive integers minus the number of negative ones.


5.         #include <stdio.h>
           int main(void) {
              int val;                  /* integer just read in             */
              int prev = 0;             /* previous integer                 */
              int streak = 0;           /* streak length of current integer */
              int best_val = 0;         /* value of best streak             */
              int best_streak = 0;      /* streak length of best integer    */

              while (scanf("%d", &val) != EOF) {
                 if (prev == val)
                    streak++;
                 else
                    streak = 1;

                 if (streak > best_streak) {
                    best_streak = streak;
                    best_val = val;
                 }
                 prev = val;
              }
              printf("Streak of %d %d's in a row.\n", best_streak, best_val);
              return 0;
           }

    We note that the   is EOF on Unix systems. For DOS machines, 
    use .


6.  (b)    #include <stdio.h>

           int main(void) {
              int height, width, i, j;   
              printf("Enter the height followed by the width.\n");
              scanf("%d %d", &height, &width);
              for (j = 0; j < height; j++) {
                 for (i = 0; i < width; i++) {
                    printf("*");
                 }
                 printf("\n");
              }
              return 0;
           }


7.         #include <stdio.h>

           int main(void) {
              int n, i, j;   
              printf("Enter the size of the triangle.\n");
              scanf("%d",&n);
              for (j = 0; j < n; j++) {
                 for (i = 0; i < n; i++) {
                    if (i >= j)
                       printf("*");
                    else
                       printf(".");
                 }
                 printf("\n");
              }
              return 0;
           }



8.         #include <stdio.h>
           
           int main(void) {
              int i, j, m, n;
              double x, y;
              printf("Enter number of x and y points.\n");
              scanf("%d %d", &m, &n);
              for (j = 0, y = 1.0; j < n; j++, y -= 1.0/(n-1)) {     
                 for (i = 0, x = 0.0; i < m; i++, x += 1.0/(m-1)) {   
                    printf("(%.2f, %.2f) ", x, y);
                 }
                 printf("\n");
              }
              return 0;
           }