FANDOM


/* Tomi Babalola and Kevin Duke */
/* Lab 07 */
 
#include <stdio.h>
#define ARRAYSIZE 35
#define NOT_FOUND -1
 
/*function that takes averages*/
void averagefunction(int array[], int numofscores)
{
   int counter;
   double averagetotal;
   int sum=0;
   counter=0;
   while(counter <= (numofscores - 1)) 
     {
       sum = array[counter] + sum;
       counter++;
     }
   averagetotal = ((double)sum) / ((double)numofscores);
   printf("Average test score: %2.2f\n", averagetotal);
}
 
/*the function that searches the array */
int search(int array[], int i, int n)
{
    int count = 0;
 
    while(array[count] != n)
     {
         count++;
      if(count > (i - 1))
        {
         count = -1;
         break;
         }
      }
 
   return count;
}
 
/*main function*/
int main(void)
{
   int array[ARRAYSIZE], input, numofscores, x;
   input = 0;
   numofscores = 0;
   int find;
   printf("To stop executing, enter -1\n");
/*get test scores from scanf*/
   while (input <= ARRAYSIZE && x != EOF)
   {
      printf("Enter test score %d: ", (input + 1));
      scanf("%d", &x);
      array[input] = x;
      input++;
   }
 
   printf("\n");
/*print the number of scores entered*/
   printf("Number of scores entered: %d", (input - 1));
 
   printf("\nTest scores entered: ");
   for(numofscores = 0; numofscores < (input - 1); numofscores++) {
   printf("%d ", array[numofscores]);
}
   printf("\n");
/*go to average and search function*/
   averagefunction(array, (input - 1));
   search(array, input , numofscores);
 
   printf("Enter test score to search for: ");
   scanf("%d", &numofscores);
 
 
   find = search(array, input, numofscores);
   if(find == -1)
      printf("\nThe score %d does not occur in the array.\n", numofscores);
   else
      printf("\nThe score %d first occurs at index %d.\n", numofscores, find);
   return 0;
}