Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 1: Use dynamic array to solve this exercise Write a native C++ program

ID: 3875820 • Letter: E

Question

Exercise 1: Use dynamic array to solve this exercise
Write a native C++ program that allows an unlimited number of student’s data (student’s ID, names, score, subject (at least one)) to be entered and stored in an array allocated in the free store. The initial array size should be five elements. The program should create a new array with five additional elements when necessary and copy values from the old array to the new.
The program should be able to perform the following operations:
- Read and store new student’s data into the list;
- Delete student from the list;
- Sort the student by score
- And print the list of student. The output should be as follows: Student’s ID, followed by his names, followed the subject, followed by the test score, followed by the test grade. Assume the following grade scale: [90 – 100] = A; [80 – 90[ = B; [70 – 80[ = C; [60 – 70[ = D; [0 – 60[= F; (More than 100 and less than 0) = N.

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;
struct student
{
   char name[15];
   char subject[15];
   char id[10];
   char grade;
   int score;
};

bool my_compare(student a, student b)
{
   if(a.score < b.score)
       return true;
   else
       return false;
}
void Add_student(student *&arr, int &size, int &limit)
{
   int flag = 0;
   student *temp;
   if(size == limit)
   {
       limit = limit + 5;
       temp = new student[limit];
       int i;
       for(i=0;i<size;i++)
           temp[i] = arr[i];
       flag = 1;
   }
  
   student add;
   cout<<"Enter details of new Student ";
   cout<<"Enter Name- ";
   cin>>add.name;
   cout<<"Enter Id- ";
   cin>>add.id;
   cout<<"Enter subject- ";
   cin>>add.subject;
   cout<<"Enter score- ";
   cin>>add.score;
   if(add.score > 100 || add.score < 0)
       add.grade = 'N';
   else if(add.score >= 90)
       add.grade = 'A';
   else if(add.score >= 80)
       add.grade = 'B';
   else if(add.score >= 70)
       add.grade = 'C';
   else if(add.score >= 60)
       add.grade = 'D';
   else
       add.grade = 'F';

   if(flag == 1)
   {
       temp[size] = add;
       free(arr);
       arr= temp;
   }
   else
       arr[size] = add;
   size++;
   return ;

}

void delete_student(student *arr, int &size)
{
   int flag = 0;
   if(size == 0)
   {
       cout<<"nothing to delete ";
       return ;
   }
   char remove_id[10];
   int i;
   cout<<"enter student's id to delete ";
   cin>>remove_id;
   for(i=0;i<size;i++)
   {
       if(strcmp(arr[i].id,remove_id)==0)
       {
           while(i<(size-1))
           {
               arr[i] = arr[i+1];
               i++;
           }
           flag =1 ;
           break;
       }
   }
   if(flag == 0)
       cout<<"enter correct student id ";
   else
       size--;
   return ;
}


void print_list(student *arr, int size)
{
   int i;
   if(size == 0)
   {
       cout<<"nothing in the list ";
       return ;
   }
   cout<<"printing students list ";
   for(i=0;i<size;i++)
   {
       cout<<arr[i].id<<" ";
       cout<<arr[i].name<<" ";
       cout<<arr[i].subject<<" ";
       cout<<arr[i].score<<" ";
       cout<<arr[i].grade<<" ";
       cout<<endl;
   }
   return ;
}

int main()
{
   student *arr = new student[5];
   int size = 0, limit = 5;
   int choice;
   while(1)
   {
       cout<<"1-Store new student’s data into the list ";
       cout<<"2-Delete student from the list ";
       cout<<"3-Sort the list according to score ";
       cout<<"4-Print the list ";
       cout<<"Anything else to exit ";
       cout<<"Enter your choice ";
       cin>>choice;
       if(choice == 1)
           Add_student(arr,size,limit);
       else if(choice == 2)
           delete_student(arr,size);
       else if(choice == 3)
           sort(arr, arr+size, my_compare);
       else if(choice == 4)
           print_list(arr, size);
       else
           break;
   }
   cout<<"exit ";
   return 0;
}