code assignment 2

PHOTO EMBED

Tue Aug 31 2021 11:52:00 GMT+0000 (Coordinated Universal Time)

Saved by @_Ryan_ #c

#include<stdio.h>
#include<string.h>
#include<windows.h>

//check duplicate ID
int check_ID(int ID[], int i)
{
 int result;

 for(int j = 0; j < i; j++)
 {
    if(ID[i] == ID[j])
    {
      result = 1;
    }
 }
   return result;
}

//input student information
void InputStudentInfor(int ID[], float grade[], int total)
{

 int i, j, check_input ;
 char temp;

 for(i = 0; i < total; i++)
 {
    do
    {
       printf("\n--> Student %d | ID: ", i+1);
       fflush(stdin);
       check_input = scanf("%d%c", &ID[i], &temp);//check_input will check how many values are scanned

       if(check_input == 2 && temp != '\n' || check_input == 0)
       {
         printf("\n--- ERROR! can't enter character\n");
       }
       else if(ID[i] < 0)
       {
         printf("\n--- ERROR! please, enter positive number\n");
       }
       else if(check_ID(ID, i)== 1)
       {
         printf("\n--- Student ID already exists\n");
       }
       else
       {
         break;
       }

    }while(check_input == 2 && temp != '\n' || check_input == 0 || ID[i] < 0 || check_ID(ID, i)== 1);


    do
    {
       printf("\n--> Grade %d : ", i+1);//check_input will check how many values are scanned
       fflush(stdin);
       check_input = scanf("%f%c", &grade[i], &temp);

       if(check_input == 2 && temp != '\n' || check_input == 0 )
       {
         printf("\n--- ERROR! can't enter character\n");
       }
       else if(grade[i] < 0 || grade[i] > 10)
       {
         printf("\n--- ERROR! please, enter number from 1 to 10\n");
       }
       else
       {
         break;
       }

    }while(check_input == 2 && temp != '\n' || check_input == 0 || grade[i] < 0 || grade[i] > 10);
 }
}

//


// Output information of student entered
void OutputStudentInfor(int ID[],float grade[], int total)
{
 for(int i = 0; i < total; i++)
 {
    printf("|%d Student ID: [%d] | Grade: %.2f |\n",i+1 ,ID[i], grade[i]);
 }
}

// Find maximum grade
float FindMaxGrade(float grade[], int total)
{
 float max = grade[0];
 for(int i = 1; i < total;i++ )
 {
    if( max < grade[i])
    {
      max = grade[i];
    }
 }
    return max;
}

//Fin minimum grade
float FindMinGrade(float grade[], int total)
{
 float min = grade[0];
 for(int i = 1; i < total;i++ )
 {
    if( min > grade[i])
    {
      min = grade[i];
    }
 }
    return min;
}

void main()
{
 int total, choice, check_input;
 char temp;
 system("color 0A");//change the color of all characters to green in the console window

   // enter the number of elements of the array and check input data
 do
 {
   printf("--> Enter total number of  student: ");
   fflush(stdin);
   check_input = scanf("%d%c", &total, &temp);//check_input will check how many values are scanned

   if(check_input == 2 && temp != '\n' || check_input  == 0)
   {
     printf("\n--- ERROR! can't enter character<<\n\n");
   }
   else if(total <= 0)
   {
     printf("\n--- ERROR! please, enter value must be greater than 0\n\n");
   }
   else
   {
     break;
   }

 }while(total <= 0 || check_input == 2 && temp !='\n' || check_input == 0);

 system("cls");

 int ID[total];
 float grade[total];

 printf(">>> Enter student ID and grade\n");

 InputStudentInfor(ID, grade, total);
 system("cls");
 printf(">>> Finished entering data");
   //menu initialization
 do
 {

    printf("\n\n\n\t\tMENU");
    printf("\n****************************************\n");
    printf("* 1. Re-enter data                     *\n");
    printf("* 2. print ID and grade of student     *\n");
    printf("* 3. Find maximum grade                *\n");
    printf("* 4. Find minimum grade                *\n");
    printf("* 5. Exit                              *\n");
    printf("****************************************\n\n");

 //check input data
    do
    {
        printf("--> Enter choice: ");
        fflush(stdin);
        check_input = scanf("%d%c", &choice, &temp);//check_input will check how many values are scanned

        if(check_input == 2 && temp != '\n' || check_input  == 0)
        {
            printf("\n--- ERROR! can't enter character<<\n\n");
        }
        else if(choice > 5 || choice < 1)
        {
            printf("\n--- ERROR! please choose from 1 to 5\n\n");
        }
        else
        {
            break;
        }

    }while(check_input == 2 && temp != '\n' || check_input  == 0 ||  choice > 5 || choice < 1);

    system("cls"); //delete screen

    //function selection
    switch(choice)
    {
        case 1:
            system("cls");
            printf(">>> Re-enter data\n\n");

            InputStudentInfor(ID, grade, total);

            printf(">>> Finished entering data");
            system("cls");
            break;

        case 2:
            system("cls");
            printf(">>> List ID and grade of student\n\n\n");

            OutputStudentInfor(ID, grade, total);
            break;

        case 3:
            system("cls");
            printf(">>> Maximum grade: %.2f \n", FindMaxGrade(grade, total));
            break;

        case 4:
            system("cls");
            printf(">>> Minimum grade: %.2f \n", FindMinGrade(grade, total));
             break;

        default :
            printf("\n>>> Program has ended!\n");
            exit(0);
    }

 }while(choice <= 4);
}
content_copyCOPY