//Include the required header files.

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <string.h>

 

//Declare the function.

void addGrade(int *array,int size);

double average(int *array,int size);

int sum(int *array,int size);

int getSize(int *array,int size);

 

//Define the main function.

int main(void)

{

  //Define the variables

  static int size=100;

  int array[size];

  int count=0;

  int choice;

  int cnt, length,i=0;

  double avg;

 

  //Initialize the array

  for(i=0;i<100;i++)

  array[i]=-1;

  do

  {

    //Display menu

    printf("\nMain Menu");

    printf("\n1. Add Grade");

    printf("\n2. Average Grade");

    printf("\n3. Number of Certain Letter Grade");

    printf("\n4. Number of Grades Entered");

    printf("\n5. Exit");

    printf("\nEnter Choice:\n\n");

    scanf("%d", &choice);

   

    //Display error message for invalid choice

    if(choice < 1 || choice > 5) {

    printf("Invalid Selection.\n\n");

    }

    //Switch Function for different choices from menu

    switch(choice)

    {

     

 //Match the user choice.

      case 1:

      addGrade(array, count);

      count++;

      break;

      case 2:

      avg = average(array, count);

      printf("The Average is :%.2f\n",avg);

      break;

      case 3:

      cnt = sum(array, size);

      if(cnt>0)

      printf("Count = %d\n",cnt);

      else

      printf("Invalid Choice.");

      break;

      case 4:

      length = getSize(array, size);

      printf("No of total grades = %d \n", length);

      break;

      case 5:

      printf("Exited Program.");

      break;

     

      //Execute default if no case match.

      default:

      break;

    }

  }

  //Check to continue in program.

  while(choice != 5);

  return 0;

}

 

 

 

//Define the function addGrade().

void addGrade(int *array, int size)

{

  //Define the variable.

  int  grade;

 

  //Check for arry out of bound.

  if(size<100)

  {

    //Prompt the user to enter the grade.

    printf("\n\nEnter grade:");

    scanf("%d",&grade);

   

 

 

 

 

 

    //Check for valid grade.

    while(grade<0 || grade>100)

    {

     printf("\n\nEnter grade:");

     scanf("%d",&grade);

    }

    //Store the grade in array.

    array[size]=grade;

  }

  else

   printf("Arary is full\n");

}

 

//Define the function average().

double average(int *array, int size)

{

  //Define the variables.

  double t = 0;

  int iter = 0;

 

  //Iterate through the array.

  while(iter<size)

  {

    //Evaluate the sum of array elements.

    t += array[iter];

    iter++;

  }

  //Return the average of array.

  return t/(double)iter;

}

 

//Define the function sum().

int sum(int *array, int size)

{

  //Define the variable.

  char uchoice;

  int iter, countA = 0, countB = 0;

  int countC = 0, countD = 0, countF = 0;

 

  //Prompt the user to enter letter grade.

  printf("\nEnter a letter grade to find its total count:");

  scanf(" %c", &uchoice);

 

  //Use lop to iterate through the elemnets

  //of the array.

  for(iter = 0; iter < size; iter++)

  {

    //Set the grade as per the array elements.

    if (array[iter] >= 90)

    {

 

      countA++;

    }

    else if (array[iter] >= 80 && array[iter] < 90)

    {

      countB++;

    }

    else if (array[iter] >= 70 && array[iter] < 80)

    {

      countC++;

    }

    else if (array[iter] >= 60 && array[iter] < 70)

    {

      countD++;

    }

    else if (array[iter] < 60 && array[iter]>-1)

    {

      countF++;

    }

  }

 

  //Check the grade and return the value for

  //the respective grade.

  if(uchoice == 'a' || uchoice == 'A')

  {

    return countA;

  }

  else if(uchoice == 'b' || uchoice == 'B')

  {

    return countB;

  }

  else if(uchoice == 'c' || uchoice == 'C')

  {

    return countC;

  }

  else if(uchoice == 'd' || uchoice == 'D')

  {

    return countD;

  }

  else if(uchoice == 'f' || uchoice == 'F')

  {

    return countF;

  }

  //Return -1 for sentential values.

  else

    return -1;

}

 

//Define the function getSize().

int getSize(int *array, int size)

{

  //Define the variables.

  int iter = 0, c = 0;

 

  //Iterate through the elements of the array.

  for(iter = 0; iter < 100; iter++)

  {

    //Check for non sentential array elements.

    if(array[iter] != -1)

    {

      //Increase the counter.

      c++;

    }

  }

return c;

}

