Practice

Find Subject



What is the exact out of the following program?  Show your intermediate work.

 // Find Subject
 //
 #include <stdio.h>
 #define ITEMS 5
 #define CHOICES 3

 int find(int value, int list[], int size)
 {
         int i = 0;

         while (i < size && value != list[i]) {
                 printf("%04d ", list[i]);
                 i++;
         }
         putchar('\n');

         return i;
 }

 void display(int list[], int n)
 {
         int i;

         for (i = 0; i < n; i++)
                 printf("%04d%c ", list[i], i < n - 1 ? ',' : ' ');
         putchar('\n');
 }


 int main(void)
 {
         int i, j, n = 0;
         int subject[ITEMS] = {100, 120, 110, 130, 140};
         int choice[CHOICES] = {140, 200, 120}, selected[ITEMS];

         printf("Subjects : ");
         display(subject, ITEMS);
         printf("Choices  : ");
         display(choice, CHOICES);

         for (i = 0; i < CHOICES; i++) {
                 j = find(choice[i], subject, ITEMS);
                 if (j >= 0 && j < ITEMS) {
                         selected[n] = choice[i];
                         n++;
                 }
         }

         printf("Selected : ");
         display(selected, n);

         return 0;
 }
  Top  Go Back to the Top of this Page



  Designed by Chris Szalwinski   Copying From This Site