L-9 MCS 260 Mon 10 Sep 2001

Below are listings for programs we discussed in class.
/* L-9 MCS 260 Mon 10 Sep 2001 : Finding minimum of user given integers */

#include<stdio.h>
#include<limits.h>      /* we need highest possible maximal value */

int main(void)
{
   int n;               /* user given integer */
   int min=INT_MAX;     /* initialization of the minimum */

   printf("Give sequence of integers : ");

   while (scanf("%d", &n) == 1)
      if (n < min)
         min = n;

   printf("Minimal value in sequence is %d.\n", min);

   return 0;
}
The following program uses a for loop and requires the user to give in the length of the sequence.
/* L-9 MCS 260 Mon 10 Sep 2001 : Finding minimum of n given integers */

#include<stdio.h>

int main(void)
{
   int nb;              /* number of integers user will give */
   int n;               /* some user given integer */
   int i;               /* used as counter */
   int min;             /* will be minimal value */

   printf("How many elements in sequence ? ");
   scanf("%d", &nb);

   if (nb > 0)
   {
      printf("Give sequence of %d integers : ", nb);

      scanf("%d", &min);          /* initialization of minimum */

      for (i = 1; i < nb; i++)    /* reads nb-1 numbers */
      {
         scanf("%d", &n);
         if (n < min)
            min = n;
      }

      printf("Minimal value in sequence is %d.\n", min);
   }

   return 0;
}
It is a good exercise to write the first program with a do. Next we see a situation where a do is also appropriate :
/* L-9 MCS 260 Mon 10 Sep 2001 : guess a number with do */

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main(void)
{
   int nb;              /* the number computer generated */
   int n;               /* guess of the user */
   int cnt = 0;         /* counts the number of trials */            

   srand(time(NULL));   /* use time as random seed */

   nb = rand() % 10;    /* random in the range 0..9 */

   printf("Guess my digit, a number in 0..9.\n");

   do
   {
      printf("  Give a number : "); scanf("%d", &n);

      cnt++;

   } while (n != nb);

   printf("Congratulations, you found it after %d trial", cnt);
   if (cnt == 1)
      printf(".\n");
   else
      printf("s.\n");

   return 0;
}
The program below is equivalent, but uses a for :
/* L-9 MCS 260 Mon 10 Sep 2001 : guess a number with for */

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main(void)
{
   int nb;              /* the number computer generated */
   int i;               /* counts number of trials */
   int n = -1;          /* guess of the user */

   srand(time(NULL));   /* use time as random seed */

   nb = rand() % 10;    /* random in the range 0..9 */

   printf("Guess my digit, a number in 0..9.\n");

   for (i = 0; n != nb; i++) 
   {
      printf("  Give a number : "); scanf("%d", &n);
   }

   printf("Congratulations, you found it after %d trial", i);
   if (i == 1)
      printf(".\n");
   else
      printf("s.\n");

   return 0;
}
And you can also write the same program with a while loop :

/* L-9 MCS 260 Mon 10 Sep 2001 : guess a number with while */

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main(void)
{
   int nb;              /* the number computer generated */
   int n = -1;          /* guess of the user */
   int cnt = 0;         /* counts the number of trials */            

   srand(time(NULL));   /* use time as random seed */

   nb = rand() % 10;    /* random in the range 0..9 */

   printf("Guess my digit, a number in 0..9.\n");

   while (n != nb)
   {
      printf("  Give a number : "); scanf("%d", &n);
      cnt++;
   } 

   printf("Congratulations, you found it after %d trial", cnt);
   if (cnt == 1)
      printf(".\n");
   else
      printf("s.\n");

   return 0;
}