﻿ C++ exercises and solutions: student records

# C++ exercises and solutions: student records

## C++ structure exercise: student records

Write a C++ program to keep records and perform statistical analysis for a class of 20 students. The information of each student contains ID, Name, Sex, quizzes Scores (2 quizzes per semester), mid-term score, final score, and total score.

The program will prompt the user to choose the operation of records from a menu as shown below:
==============================================

===============================================

1. Add student records

2. Delete student records

3. Update student records

4. View all student records

5. Calculate an average of a selected student’s scores

6. Show student who gets the max total score

7. Show student who gets the min total score

8. Find student by ID

9. Sort records by total scores

Note: All students records store in an array of structures

Solution:

To keep this solution simple and easy to follow, we divide this solution in to different steps: Step 1: Declaring a structure called student to store the records. The structure student contains nine members:

-stnumber(string) to store the student' id

-stname(array of characters) to store the student's name

-sex(char) to store the student's sex

-quiz1(float) to store the first quiz score

-quiz2(float) to store the second quiz score

-assignment(float) to store the assignment score

-midterm(float) to store the mid-term score

-final(float) to store the final score

-total(float) to store the total score

typedef struct student
{

string stnumber;
char stname;
char sex;
float quiz1;
float quiz2;
float assignment;
float midterm;
float final;
float total;

}; jibai kiaquan jia si ma de jibai2017-09-24 helperTo see step 2, click the arrow in the upper right corner2017-07-18 Julius BOBOBOBO MO 2016-01-30 Julius Aromin#include "stdafx.h" #include #include #include #include #include using namespace std; struct StudentData { int studentID; string first_name; string last_name; int exam1; int exam2; int exam3; int total; char ch; }; const int SIZE = 9; const int INFO = 4; // Function prototypes void openInputFile(ifstream &, string); void getTotal(StudentData[]); void getGrade(StudentData[]); void calcLowest(StudentData[], int &, int &, int &, int &, int[]); void calcHighest(StudentData[], int &, int &, int &, int &, int[]); void getAverage(StudentData[], int, double &, double &, double &, double &, double[]); void getStd(StudentData[], double &, double &, double &, double &, double &, double &, double &, double &, double[]); void print(StudentData[], int[], int[], double[], double[]); void sort(StudentData[]); int main() { // Variables StudentData arr[SIZE]; int lowest1, lowest2, lowest3, lowest4; // Stores lowest exam scores int highest1, highest2, highest3, highest4; // Holds highest exam scores double average1 = 0, average2 = 0, average3 = 0, average4 = 0; // Represents average of each exam double std1 = 0, std2 = 0, std3 = 0, std4 = 0; // Holds standard deviation for Exams 1-3 and Total int lowest[INFO] = {}; int highest[INFO] = {}; double average[INFO] = {}; double standardDeviation[INFO] = {}; ifstream inFile; string inFileName = "C:\\Users\\Lisa\\Desktop\\scores.txt"; // Call function to read data in file openInputFile(inFile, inFileName); // Read data into an array of structs for(int count = 0; count < SIZE; count++) { inFile >> arr[count].studentID >> arr[count].first_name >> arr[count].last_name >> arr[count].exam1 >> arr[count].exam2 >> arr[count].exam3; } // Close input file inFile.close(); // Get score total for each student getTotal(arr); // Determine grade for each student getGrade(arr); // Calculate lowest scores in each exam and total scores calcLowest(arr, lowest1, lowest2, lowest3, lowest4, lowest); // Calculate highest scores in each exam and total scores calcHighest(arr, highest1, highest2, highest3, highest4, highest); // Calculate average of each exam and the average of the total scores getAverage(arr, SIZE, average1, average2, average3, average4, average); // Calculate standard deviation of each category getStd(arr, std1, std2, std3, std4, average1, average2, average3, average4, standardDeviation); cout << "\n"; // Print unsorted data print(arr, lowest, highest, average, standardDeviation); cout << "\n"; // Sort data sort(arr); // Print sorted data print(arr, lowest, highest, average, standardDeviation); system("PAUSE"); return 0; } /** * Pre-condition: * Post-condition: */ void openInputFile(ifstream &inFile, string inFileName) { //Open the file inFile.open(inFileName); //Input validation if (!inFile) { cout << "Error to open file." << endl; cout << endl; return; } } /** * Pre-condition: * Post-condition: */ void getTotal(StudentData arr[]) { for(int i = 0; i < SIZE; i++) { arr[i].total = arr[i].exam1 + arr[i].exam2 + arr[i].exam3; } } /** * Pre-condition: * Post-condition: */ void getGrade(StudentData arr[]) { for(int i = 0; i < SIZE; i++) { if(arr[i].total >= 270) arr[i].ch = 'A'; else if(arr[i].total >= 240) arr[i].ch = 'B'; else if(arr[i].total >= 210) arr[i].ch = 'C'; else if(arr[i].total >= 180) arr[i].ch = 'D'; else arr[i].ch = 'F'; } } /** * Pre-condition: * Post-condition: */ void calcLowest(StudentData arr[], int &lowest1, int &lowest2, int &lowest3, int &lowest4, int lowest[]) { int smallest = 0; lowest1 = arr.exam1; lowest2 = arr.exam2; lowest3 = arr.exam3; lowest4 = arr.total; // Loop to determine lowest score from Exam1, 2, 3, and Total for (int i = 0; i < SIZE; i++) { if (lowest1 > arr[i].exam1) { lowest1 = arr[i].exam1; smallest = i; } if (lowest2 > arr[i].exam2) { lowest2 = arr[i].exam2; smallest = i; } if (lowest3 > arr[i].exam3) { lowest3 = arr[i].exam3; smallest = i; } if (lowest4 > arr[i].total) { lowest4 = arr[i].total; smallest = i; } } // Loop lowest values into an array of size 4 for(int index = 0; index < INFO; index++) { if(index == 0) lowest = lowest1; else if(index == 1) lowest = lowest2; else if(index == 2) lowest = lowest3; else if(index == 3) lowest = lowest4; else cout << "Fail!" << endl; } } /** * Pre-condition: * Post-condition: */ void calcHighest(StudentData arr[], int &highest1, int &highest2, int &highest3, int &highest4, int highest[]) { int biggest = 0; highest1 = arr.exam1; highest2 = arr.exam2; highest3 = arr.exam3; highest4 = arr.total; // Loop to determine highest score from Exam1, 2, 3, and Total for (int i = 0; i < SIZE; i++) { if (highest1 < arr[i].exam1) { highest1 = arr[i].exam1; biggest = i; } if (highest2 < arr[i].exam2) { highest2 = arr[i].exam2; biggest = i; } if (highest3 < arr[i].exam3) { highest3 = arr[i].exam3; biggest = i; } if (highest4 < arr[i].total) { highest4 = arr[i].total; biggest = i; } } // Loop highest values into an array of size 4 for(int index = 0; index < INFO; index++) { if(index == 0) highest = highest1; else if(index == 1) highest = highest2; else if(index == 2) highest = highest3; else if(index == 3) highest = highest4; else cout << "Fail!" << endl; } } /** * Pre-condition: * Post-condition: */ void getAverage(StudentData arr[], int size, double &average1, double &average2, double &average3, double &average4, double average[]) { int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0; // Get sum of each category (Exam1, 2, 3, and Total) for(int i = 0; i < SIZE; i++) { sum1 += arr[i].exam1; sum2 += arr[i].exam2; sum3 += arr[i].exam3; sum4 += arr[i].total; } // Calculate average for each category average1 += static_cast(sum1)/size; average2 += static_cast(sum2)/size; average3 += static_cast(sum3)/size; average4 += static_cast(sum4)/size; // Loop average values into an array of size 4 for(int index = 0; index < INFO; index++) { if(index == 0) average = average1; else if(index == 1) average = average2; else if(index == 2) average = average3; else if(index == 3) average = average4; else cout << "Fail!" << endl; } } /** * Pre-condition: * Post-condition: */ void getStd(StudentData arr[], double &std1, double &std2, double &std3, double &std4, double &average1, double &average2, double &average3, double &average4, double standardDeviation[]) { double deviationSum1 = 0, deviationSum2 = 0, deviationSum3 = 0, deviationSum4 = 0; for(int i = 0; i < SIZE; i++) { deviationSum1 += pow((arr[i].exam1 - average1), 2); deviationSum2 += pow((arr[i].exam2 - average2), 2); deviationSum3 += pow((arr[i].exam3 - average3), 2); deviationSum4 += pow((arr[i].total - average4), 2); } std1 = sqrt(deviationSum1 / ((SIZE) - 1)); std2 = sqrt(deviationSum2 / ((SIZE) - 1)); std3 = sqrt(deviationSum3 / ((SIZE) - 1)); std4 = sqrt(deviationSum4 / ((SIZE) - 1)); // Loop average values into an array of size for(int index = 0; index < INFO; index++) { if(index == 0) standardDeviation = std1; else if(index == 1) standardDeviation = std2; else if(index == 2) standardDeviation = std3; else if(index == 3) standardDeviation = std4; else cout << "Fail!" << endl; } } cout << "\n"; } /** * Pre-condition: * Post-condition: */ void sort(StudentData arr[]) { StudentData temp; for (int i = 0; i < (SIZE - 1); i++) { for (int j = i + 1; j < SIZE; j++) { if (arr[i].last_name > arr[j].last_name) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } }2015-10-12 Julius Aromin#include "stdafx.h" #include #include #include #include #include using namespace std; struct StudentData { int studentID; string first_name; string last_name; int exam1; int exam2; int exam3; int total; char ch; }; const int SIZE = 9; const int INFO = 4; // Function prototypes void openInputFile(ifstream &, string); void getTotal(StudentData[]); void getGrade(StudentData[]); void calcLowest(StudentData[], int &, int &, int &, int &, int[]); void calcHighest(StudentData[], int &, int &, int &, int &, int[]); void getAverage(StudentData[], int, double &, double &, double &, double &, double[]); void getStd(StudentData[], double &, double &, double &, double &, double &, double &, double &, double &, double[]); void print(StudentData[], int[], int[], double[], double[]); void sort(StudentData[]); int main() { // Variables StudentData arr[SIZE]; int lowest1, lowest2, lowest3, lowest4; // Stores lowest exam scores int highest1, highest2, highest3, highest4; // Holds highest exam scores double average1 = 0, average2 = 0, average3 = 0, average4 = 0; // Represents average of each exam double std1 = 0, std2 = 0, std3 = 0, std4 = 0; // Holds standard deviation for Exams 1-3 and Total int lowest[INFO] = {}; int highest[INFO] = {}; double average[INFO] = {}; double standardDeviation[INFO] = {}; ifstream inFile; string inFileName = "C:\\Users\\Lisa\\Desktop\\scores.txt"; // Call function to read data in file openInputFile(inFile, inFileName); // Read data into an array of structs for(int count = 0; count < SIZE; count++) { inFile >> arr[count].studentID >> arr[count].first_name >> arr[count].last_name >> arr[count].exam1 >> arr[count].exam2 >> arr[count].exam3; } // Close input file inFile.close(); // Get score total for each student getTotal(arr); // Determine grade for each student getGrade(arr); // Calculate lowest scores in each exam and total scores calcLowest(arr, lowest1, lowest2, lowest3, lowest4, lowest); // Calculate highest scores in each exam and total scores calcHighest(arr, highest1, highest2, highest3, highest4, highest); // Calculate average of each exam and the average of the total scores getAverage(arr, SIZE, average1, average2, average3, average4, average); // Calculate standard deviation of each category getStd(arr, std1, std2, std3, std4, average1, average2, average3, average4, standardDeviation); cout << "\n"; // Print unsorted data print(arr, lowest, highest, average, standardDeviation); cout << "\n"; // Sort data sort(arr); // Print sorted data print(arr, lowest, highest, average, standardDeviation); system("PAUSE"); return 0; } /** * Pre-condition: * Post-condition: */ void openInputFile(ifstream &inFile, string inFileName) { //Open the file inFile.open(inFileName); //Input validation if (!inFile) { cout << "Error to open file." << endl; cout << endl; return; } } /** * Pre-condition: * Post-condition: */ void getTotal(StudentData arr[]) { for(int i = 0; i < SIZE; i++) { arr[i].total = arr[i].exam1 + arr[i].exam2 + arr[i].exam3; } } /** * Pre-condition: * Post-condition: */ void getGrade(StudentData arr[]) { for(int i = 0; i < SIZE; i++) { if(arr[i].total >= 270) arr[i].ch = 'A'; else if(arr[i].total >= 240) arr[i].ch = 'B'; else if(arr[i].total >= 210) arr[i].ch = 'C'; else if(arr[i].total >= 180) arr[i].ch = 'D'; else arr[i].ch = 'F'; } } /** * Pre-condition: * Post-condition: */ void calcLowest(StudentData arr[], int &lowest1, int &lowest2, int &lowest3, int &lowest4, int lowest[]) { int smallest = 0; lowest1 = arr.exam1; lowest2 = arr.exam2; lowest3 = arr.exam3; lowest4 = arr.total; // Loop to determine lowest score from Exam1, 2, 3, and Total for (int i = 0; i < SIZE; i++) { if (lowest1 > arr[i].exam1) { lowest1 = arr[i].exam1; smallest = i; } if (lowest2 > arr[i].exam2) { lowest2 = arr[i].exam2; smallest = i; } if (lowest3 > arr[i].exam3) { lowest3 = arr[i].exam3; smallest = i; } if (lowest4 > arr[i].total) { lowest4 = arr[i].total; smallest = i; } } // Loop lowest values into an array of size 4 for(int index = 0; index < INFO; index++) { if(index == 0) lowest = lowest1; else if(index == 1) lowest = lowest2; else if(index == 2) lowest = lowest3; else if(index == 3) lowest = lowest4; else cout << "Fail!" << endl; } } /** * Pre-condition: * Post-condition: */ void calcHighest(StudentData arr[], int &highest1, int &highest2, int &highest3, int &highest4, int highest[]) { int biggest = 0; highest1 = arr.exam1; highest2 = arr.exam2; highest3 = arr.exam3; highest4 = arr.total; // Loop to determine highest score from Exam1, 2, 3, and Total for (int i = 0; i < SIZE; i++) { if (highest1 < arr[i].exam1) { highest1 = arr[i].exam1; biggest = i; } if (highest2 < arr[i].exam2) { highest2 = arr[i].exam2; biggest = i; } if (highest3 < arr[i].exam3) { highest3 = arr[i].exam3; biggest = i; } if (highest4 < arr[i].total) { highest4 = arr[i].total; biggest = i; } } // Loop highest values into an array of size 4 for(int index = 0; index < INFO; index++) { if(index == 0) highest = highest1; else if(index == 1) highest = highest2; else if(index == 2) highest = highest3; else if(index == 3) highest = highest4; else cout << "Fail!" << endl; } } /** * Pre-condition: * Post-condition: */ void getAverage(StudentData arr[], int size, double &average1, double &average2, double &average3, double &average4, double average[]) { int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0; // Get sum of each category (Exam1, 2, 3, and Total) for(int i = 0; i < SIZE; i++) { sum1 += arr[i].exam1; sum2 += arr[i].exam2; sum3 += arr[i].exam3; sum4 += arr[i].total; } // Calculate average for each category average1 += static_cast(sum1)/size; average2 += static_cast(sum2)/size; average3 += static_cast(sum3)/size; average4 += static_cast(sum4)/size; // Loop average values into an array of size 4 for(int index = 0; index < INFO; index++) { if(index == 0) average = average1; else if(index == 1) average = average2; else if(index == 2) average = average3; else if(index == 3) average = average4; else cout << "Fail!" << endl; } } /** * Pre-condition: * Post-condition: */ void getStd(StudentData arr[], double &std1, double &std2, double &std3, double &std4, double &average1, double &average2, double &average3, double &average4, double standardDeviation[]) { double deviationSum1 = 0, deviationSum2 = 0, deviationSum3 = 0, deviationSum4 = 0; for(int i = 0; i < SIZE; i++) { deviationSum1 += pow((arr[i].exam1 - average1), 2); deviationSum2 += pow((arr[i].exam2 - average2), 2); deviationSum3 += pow((arr[i].exam3 - average3), 2); deviationSum4 += pow((arr[i].total - average4), 2); } std1 = sqrt(deviationSum1 / ((SIZE) - 1)); std2 = sqrt(deviationSum2 / ((SIZE) - 1)); std3 = sqrt(deviationSum3 / ((SIZE) - 1)); std4 = sqrt(deviationSum4 / ((SIZE) - 1)); // Loop average values into an array of size for(int index = 0; index < INFO; index++) { if(index == 0) standardDeviation = std1; else if(index == 1) standardDeviation = std2; else if(index == 2) standardDeviation = std3; else if(index == 3) standardDeviation = std4; else cout << "Fail!" << endl; } } cout << "\n"; } /** * Pre-condition: * Post-condition: */ void sort(StudentData arr[]) { StudentData temp; for (int i = 0; i < (SIZE - 1); i++) { for (int j = i + 1; j < SIZE; j++) { if (arr[i].last_name > arr[j].last_name) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } }2015-10-12 aftabwhere the step two; 2014-10-18 charlespls give the answer pls i realy need it 2014-10-13 jackwhere is the step 2?2014-10-10