Answer to Question #159518 in C++ for awais

Question #159518

A school maintains academic record of its students (of a class) in different arrays. The information stored is:

1.    Roll Number (in an integer array)

2.    Midterm Marks (in a float array)

3.    Final Marks (in a float array)

4.    Class of a student e.g. Class 1, Class 2, Class 3 (in an integer array) 

5.    The Grades based on final marks (in char array) 

a.     Final Marks less than 50 – F

b.    Final Marks: 50 to 59 (both inclusive) – D 

c.     Final Marks: 60 to 72 (both inclusive) – C

d.    Final Marks: 73 to 85 (both inclusive) – B

e.     Final Marks: 86 and above – A  

 

This program can display data from the existing student record. At the start of program, a menu will be displayed. The program will continue to work until e or E is entered. Error message will be displayed if any number (or a character) other than the given option is entered. Each array must have a size 100 and for the sake of my convenience, the arrays should be populated with exactly 50 student entries. The roll numbers must be unique – you have to take care of this. 

The main menu must be the following (the comments written in the menu below are for your understanding and SHOULD not be displayed as such):

1.    Sort and display all the records roll number wise in ascending order. /*When this option is selected, the output should be:

 

Roll No: 25  Midterm Marks: 10.5  Final Marks: 55     Class: 6  Grade: D

Roll No: 45  Midterm Marks: 8       Final Marks: 55     Class: 7  Grade: D

Roll No: 66  Midterm Marks: 36     Final Marks: 75.5  Class: 6  Grade: B

           .                    .                                .                             . 

  .

           .                    .                                .                             . 

  .

.                    .                                .                             .             outputs for options 2-8 must be similar to option 1/*

  .

2.    Sort and display all the records roll number wise in descending order. 

3.    Sort and display all records in ascending order based on marks in Midterm

4.    Sort and display all records in descending order based on marks in Midterm

5.    Sort and display all records in ascending order based on marks in Final

6.    Sort and display all records in descending order based on marks in Final

7.    Sort and display all records in ascending order based on Grade 8. Sort and display all records in descending order based on Grade 9. Add a new entry of a student. 

/*Assuming that the arrays got sorted after option 1 was selected, the new student entry which got added is shown below. Addition must be done on the next available index. You must ensure that the arrays are not full. In this option you will also make sure that the roll number that is being entered in unique:

 


10. Delete a student record based on his roll number. 

/*In this option, the user will enter a roll number and the whole data related to it must be deleted. The entries must be shifted so that there is no space in between any entry. Assuming that the user enters 66, the arrays after deletion are shown below:

11. Display record of all the students greater than X marks in final exam (in descending order with respect to marks obtained in final exam). The value of X will be entered by the user. 

12. Display record of all the students greater than X marks in final exam (in ascending order with respect to marks obtained in final exam). The value of X will be entered by the user. 

13. Display record of all the students less than or equal to X marks in final exam (in descending order with respect to marks obtained in final exam). The value of X will be entered by the user. 

14. Display record of all the students less than or equal to X marks in final exam (in ascending order with respect to marks obtained in final exam). The value of X will be entered by the user. 

15. Display record of all the students greater than X grade (in descending order with respect to grade). The value of X (character) will be entered by the user. 

16. Display record of all the students greater than X grade (in ascending order with respect to grade). The value of X (character) will be entered by the user. 

17. Display record of all the students less than or equal to X grade (in descending order with respect to grade). The value of X (character) will be entered by the user.

18. Display record of all the students less than or equal to X grade (in ascending order with respect to grade). The value of X (character) will be entered by the user.


1
Expert's answer
2021-01-29T03:42:24-0500
#include <bits/stdc++.h>
using namespace std;

// This function displays the menu
void displayMenu(){
    cout<<"\nEnter the key";
    cout<<"\na. Add student Details";
    cout<<"\nb. Sort and Display all records roll number wise in ascending order";
    cout<<"\ne. Exit the program.\n";
}

// function finds if id is already present or not
bool findId(int std_roll_number[],int n,int id){
    for(int i=0;i<n;i++){
        if(std_roll_number[i]==id)
        return true;
    }
    return false;
}
// this function assigns grade depending upon marks
char assignGrade(int marks){
    if(marks<50)
    return 'F';
    else if(marks>=50 && marks<=59)
    return 'D';
    else if(marks>=60 && marks<=72)
    return 'C';
    else if(marks>=73 && marks<=85)
    return 'B';
    else
    return 'A';
}
// this function add student to array
void addStudent(int std_roll_number[],float std_mid_marks[],float std_final_marks[],int std_class[],char std_grade[],int cnt){
    int id;
    cout<<"Enter Student ID : ";
    cin>>id;
    while(findId(std_roll_number,cnt,id)){
        cout<<"\nThis id already exist. Please re-enter the ID : ";
        cin>>id;
    }
    std_roll_number[cnt] = id;
    cout<<"Enter Mid Marks : ";
    cin>>std_mid_marks[cnt];
    cout<<"Enter Final Marks : ";
    cin>>std_final_marks[cnt];
    cout<<"Enter Class : ";
    cin>>std_class[cnt];
    std_grade[cnt] = assignGrade(std_final_marks[cnt]);
}
// this function displays student data
void displayStudentInfo(int std_roll_number[],float std_mid_marks[],float std_final_marks[],int std_class[],char std_grade[],int n){
    for(int i=0;i<n;i++){
        cout<<"\nRoll No: "<<std_roll_number[i]<<"  Mid term Marks: "<<std_mid_marks[i]<<"  Final Marks: "<<std_final_marks[i]<<"  Class: "<<std_class[i]<<"  Grade: "<<std_grade[i];
    }
}
// this function sort data by id
void sortById(int std_roll_number[],float std_mid_marks[],float std_final_marks[],int std_class[],char std_grade[],int n){
    for (int i=0; i<n; i++){
        for (int j=i + 1; j<n; j++){
            if (std_roll_number[i] > std_roll_number[j]){
                swap(std_roll_number[i],std_roll_number[j]);
                swap(std_mid_marks[i],std_mid_marks[j]);
                swap(std_final_marks[i],std_final_marks[j]);
                swap(std_class[i],std_class[j]);
                swap(std_grade[i],std_grade[j]);
            }
        }
    }
    displayStudentInfo(std_roll_number,std_mid_marks,std_final_marks,std_class,std_grade,n);
}

int main(){
    int std_roll_number[100];
    float std_mid_marks[100];
    float std_final_marks[100];
    int std_class[100];
    char std_grade[100];
    char option='a';
    int cnt=-1;
    while(tolower(option)!='e'){
        displayMenu();
        cin>>option;
        if(tolower(option)=='a'){
            cnt++;
            addStudent(std_roll_number,std_mid_marks,std_final_marks,std_class,std_grade,cnt);
        }else if(tolower(option)=='b'){
            sortById(std_roll_number,std_mid_marks,std_final_marks,std_class,std_grade,cnt+1);
        }else if(tolower(option)=='e'){
            cout<<"\nExiting the program\n";
            return 0;
        }else{
            cout<<"\nPlease enter a valid option\n";
        }
    }
    return 0;
}#include <bits/stdc++.h>
using namespace std;

// This function displays the menu
void displayMenu(){
    cout<<"\nEnter the key";
    cout<<"\na. Add student Details";
    cout<<"\nb. Sort and Display all records roll number wise in ascending order";
    cout<<"\ne. Exit the program.\n";
}

// function finds if id is already present or not
bool findId(int std_roll_number[],int n,int id){
    for(int i=0;i<n;i++){
        if(std_roll_number[i]==id)
        return true;
    }
    return false;
}
// this function assigns grade depending upon marks
char assignGrade(int marks){
    if(marks<50)
    return 'F';
    else if(marks>=50 && marks<=59)
    return 'D';
    else if(marks>=60 && marks<=72)
    return 'C';
    else if(marks>=73 && marks<=85)
    return 'B';
    else
    return 'A';
}
// this function add student to array
void addStudent(int std_roll_number[],float std_mid_marks[],float std_final_marks[],int std_class[],char std_grade[],int cnt){
    int id;
    cout<<"Enter Student ID : ";
    cin>>id;
    while(findId(std_roll_number,cnt,id)){
        cout<<"\nThis id already exist. Please re-enter the ID : ";
        cin>>id;
    }
    std_roll_number[cnt] = id;
    cout<<"Enter Mid Marks : ";
    cin>>std_mid_marks[cnt];
    cout<<"Enter Final Marks : ";
    cin>>std_final_marks[cnt];
    cout<<"Enter Class : ";
    cin>>std_class[cnt];
    std_grade[cnt] = assignGrade(std_final_marks[cnt]);
}
// this function displays student data
void displayStudentInfo(int std_roll_number[],float std_mid_marks[],float std_final_marks[],int std_class[],char std_grade[],int n){
    for(int i=0;i<n;i++){
        cout<<"\nRoll No: "<<std_roll_number[i]<<"  Mid term Marks: "<<std_mid_marks[i]<<"  Final Marks: "<<std_final_marks[i]<<"  Class: "<<std_class[i]<<"  Grade: "<<std_grade[i];
    }
}
// this function sort data by id
void sortById(int std_roll_number[],float std_mid_marks[],float std_final_marks[],int std_class[],char std_grade[],int n){
    for (int i=0; i<n; i++){
        for (int j=i + 1; j<n; j++){
            if (std_roll_number[i] > std_roll_number[j]){
                swap(std_roll_number[i],std_roll_number[j]);
                swap(std_mid_marks[i],std_mid_marks[j]);
                swap(std_final_marks[i],std_final_marks[j]);
                swap(std_class[i],std_class[j]);
                swap(std_grade[i],std_grade[j]);
            }
        }
    }
    displayStudentInfo(std_roll_number,std_mid_marks,std_final_marks,std_class,std_grade,n);
}

int main(){
    int std_roll_number[100];
    float std_mid_marks[100];
    float std_final_marks[100];
    int std_class[100];
    char std_grade[100];
    char option='a';
    int cnt=-1;
    while(tolower(option)!='e'){
        displayMenu();
        cin>>option;
        if(tolower(option)=='a'){
            cnt++;
            addStudent(std_roll_number,std_mid_marks,std_final_marks,std_class,std_grade,cnt);
        }else if(tolower(option)=='b'){
            sortById(std_roll_number,std_mid_marks,std_final_marks,std_class,std_grade,cnt+1);
        }else if(tolower(option)=='e'){
            cout<<"\nExiting the program\n";
            return 0;
        }else{
            cout<<"\nPlease enter a valid option\n";
        }
    }
    return 0;
}

Here is the output of above code.

Enter the key
a. Add student Details
b. Sort and Display all records roll number wise in ascending order
e. Exit the program.
a
Enter Student ID : 25
Enter Mid Marks : 10.5
Enter Final Marks : 55
Enter Class : 6

Enter the key
a. Add student Details
b. Sort and Display all records roll number wise in ascending order
e. Exit the program.
a
Enter Student ID : 25

This id already exist. Please re-enter the ID : 66
Enter Mid Marks : 36
Enter Final Marks : 75.5
Enter Class : 6

Enter the key
a. Add student Details
b. Sort and Display all records roll number wise in ascending order
e. Exit the program.
a
Enter Student ID : 45
Enter Mid Marks : 8
Enter Final Marks : 55
Enter Class : 7

Enter the key
a. Add student Details
b. Sort and Display all records roll number wise in ascending order
e. Exit the program.
b

Roll No: 25  Mid term Marks: 10.500000  Final Marks: 55.000000  Class: 6  Grade: D
Roll No: 45  Mid term Marks: 8.000000  Final Marks: 55.000000  Class: 7  Grade: D
Roll No: 66  Mid term Marks: 36.000000  Final Marks: 75.500000  Class: 6  Grade: B
Enter the key
a. Add student Details
b. Sort and Display all records roll number wise in ascending order
e. Exit the program.
e

Exiting the program

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog