Code:
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<iomanip>
using namespace std;
const int size = 1000;
bool AddMarks(double AllMarks[], int &TotalRec, double &Mark);
bool DisplayMarks(double AllMarks[], int TotalRec, double& Mark);
bool CalcAvg(double AllMarks[], int TotalRec, double &Avg);
bool CalcStdDev(double AllMarks[], int TotalRec, double &Avg, double &StdDev);
bool DelMarks(double AllMarks[], int TotalRec, double &DelMarks );
bool FindMarks(double AllMarks[], int TotalRec, double &Occur, double& UserInput);
bool MarkOccur(double AllMarks[], int TotalRec, double &AllOccur);
int main(){
double AllMarks[size] = {};
double choice, Mark, Avg, StdDev, deleMark, UserInput, Occur, AllOccur;
int TotalRec = 0;
while (Mark >= 0 || Mark <= 100){
cout << "MAIN MENU" << endl;
cout << "0. Exit" << endl;
cout << "1. Add a Student Mark" << endl;
cout << "2. List all Student Marks" << endl;
cout << "3. Calculate the Average Mark" << endl;
cout << "4. Calculate the Standard Deviation" << endl;
cout << "5. Delete a Student Mark" << endl;
cout << "6. Find the number of Students via a Mark" << endl;
cout << "7. Display Distinct Marks & their Occurrences\n" << endl;
cout << "Your Choice -> ";
cin >> choice;
while(choice == 0){
return 0;
}
if(choice == 1){
AddMarks(AllMarks, TotalRec, Mark);
}
else if(choice == 2){
DisplayMarks(AllMarks, TotalRec, Mark);
}
else if(choice == 3){
CalcAvg(AllMarks, TotalRec, Avg);
}
else if(choice == 4){
CalcStdDev(AllMarks, TotalRec, StdDev, Avg);
}
else if(choice == 5){
DelMarks(AllMarks,TotalRec, deleMark );
}
else if(choice == 6){
FindMarks(AllMarks, TotalRec, Occur, UserInput);
}
else if(choice == 7){
MarkOccur(AllMarks, TotalRec, AllOccur);
}
else if(cin.eof() || cin.fail()){
cin.clear();
cin.ignore();
cout << "\nOPTION IS INVALID ENTER ANOTHER OPTION!\n" << endl;
}
else{
cout << "\nOPTION IS INVALID ENTER ANOTHER OPTION!\n" << endl;
}
system("pause");
system("cls");
}
return 0;
}
bool AddMarks(double AllMarks[], int& TotalRec, double& Mark){
int i;
cout << "\nAdd a Student Mark -> ";
cin >> Mark;
AllMarks[TotalRec] = Mark;
TotalRec++;
i++;
if (Mark <= 0 || Mark > 100){
cin.clear();
cin.ignore();
cout << "\nInvalid Student Mark.\n" << endl;
TotalRec--;
}
else if(cin.eof() || cin.fail()){
cin.clear();
cin.ignore();
cout << "\nInvalid Value.\n" << endl;
TotalRec--;
}
cout << "\n";
return AllMarks[i];
}
bool DisplayMarks(double AllMarks[], int TotalRec, double& Mark){
cout << "\nAll STUDENT MarkS:\n " << endl;
for (int i = 0; i < TotalRec; i++){
cout << AllMarks[i] << " ";
}
cout << "\n\n";
return AllMarks;
}
bool CalcAvg(double AllMarks[], int TotalRec, double &Avg){
double total = 0;
for (int i = 0; i < TotalRec; i++){
total = total + AllMarks[i];
}
Avg = total/TotalRec;
cout << "\nThe Avg of all Student Marks is " << Avg << endl;
cout << "\n";
cout << setiosflags(ios::showpoint)
<< setiosflags(ios:: fixed)
<< setprecision(2);
return Avg;
}
bool CalcStdDev(double AllMarks[], int TotalRec, double& StdDev, double &Avg){
float sum = 0;
for(int i = 0; i < TotalRec; i++){
sum += pow((AllMarks[i] - Avg), 2);
StdDev = sqrt(sum /(TotalRec - 1));
}
cout << "\nThe standard deviation of all Student Marks is " << StdDev << endl;
cout << "\n";
cout << setiosflags(ios::showpoint)
<< setiosflags(ios:: fixed)
<< setprecision(2);
return StdDev;
}
bool DelMarks(double AllMarks[],int TotalRec, double& deleMark){
cout << "\nEnter the Mark to be deleted: ";
cin >> deleMark;
if (deleMark <= 0 || deleMark > 100){
cin.clear();
cin.ignore();
cout << "\nInvalid Student Mark.\n" << endl;
return 0;
}
else if(cin.eof() || cin.fail()){
cin.clear();
cin.ignore();
cout << "\nInvalid Value.\n" << endl;
return 0;
}
for(int i = 0; i < TotalRec; i++){
if(AllMarks[i] == deleMark){
AllMarks[i] = deleMark;
AllMarks[i] = AllMarks[i+TotalRec];
AllMarks[i+TotalRec] != '\0';
TotalRec -= TotalRec;
cout << "\nStudent Mark " << deleMark << " is deleted from the database" << endl;
cout << "\n";
return 0;
}
else{
cout << "Student Mark doesn't exist.\n";
return 0;
}
}
}
bool FindMarks(double AllMarks[], int TotalRec, double& Occur, double& UserInput){
cout << "\nEnter a Student Mark that you want to find: ";
cin >> UserInput;
Occur = 0;
if (UserInput < 0 || UserInput > 100){
cin.clear();
cin.ignore();
cout << "\nInvalid Student Mark." << endl;
return 0;
}
else if(cin.eof() || cin.fail()){
cin.clear();
cin.ignore();
cout << "\nInvalid Value." << endl;
return 0;
}
for(int i = 0; i < TotalRec; i++){
if(UserInput == AllMarks[i]){
Occur++;
}
}
cout << "\nThere are " << Occur << " student that achieved a Mark of " << UserInput << endl;
cout << "\n";
}
bool MarkOccur(double AllMarks[], int TotalRec, double& AllOccur){
int indexOfMin, i ,j;
for(i=0; i<TotalRec; i++){
indexOfMin = i;
for (j=i+1; j<TotalRec; j++)
if (AllMarks[j]<AllMarks[i])
indexOfMin = j;
swap (AllMarks[i], AllMarks[indexOfMin]);
}
cout << "\nMarkS OccurRENCES\n";
for(int i = 0; i < TotalRec; i++){
cout << AllMarks[i] << " " << "1" << endl;
}
}
void swap (int &x, int &y ){
int temp;
temp = x;
x = y;
y = temp;
}