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

Write the following code (C++): Create a struct data type called Student that co

ID: 3677613 • Letter: W

Question

Write the following code (C++):

Create a struct data type called Student that contains:

name – a string that may contain blank spaces
id – an integer
percent – a double, current percent of course points
grade – a char representing a letter grade

Main program:
Declare an array of 20 Student records
Ask the user for the name of an input file and open that file for input
Read the Student information from the file into the array (the names, ids, and percents)
Calculate the grade field for each Student in the array (based on a 90/80/70/60/less scale for A/B/C/D/F grades.)
Sort the array of Students into decreasing order, comparing percent values.
Output all of the information for all of the Students (line the fields up in columns to make it easily readable)
Sort the array of Students into increasing order, comparing names.
Output all of the information for all of the Students (line the fields up in columns to make it easily readable)

Use functions to do all of the major tasks in the program (open file, input, calculate, sort, output)

Explanation / Answer

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

struct StudentData
{
    int studentID;
    string first_name;
    int total;
    int exam1;
    int exam2;
    int exam3;
    int total;
    char per;
    char per;
};

const int SIZE = 20;
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 calculate(StudentData[], int &, int &, int &, int &, int[]);
void getAverage(StudentData[], int, 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;
    int highest1, highest2, highest3, highest4;
    double average1 = 0, average2 = 0, average3 = 0, average4 = 0;
    double std1 = 0, std2 = 0, std3 = 0, std4 = 0;
    int lowest[INFO] = {};
    int highest[INFO] = {};
    double average[INFO] = {};
    double standardDeviation[INFO] = {};

    ifstream inFile;
    string inFileName = "C:marks.txt";


    openInputFile(inFile, inFileName);


    for(int count = 0; count < SIZE; count++)
    {
        inFile >> arr[count].studentID >> arr[count].first_name >> arr[count].exam1 >> arr[count].exam2 >> arr[count].exam3;
    }


    inFile.close();


    getTotal(arr);


    getGrade(arr);


    calcLowest(arr, lowest1, lowest2, lowest3, lowest4, lowest);

  
    calculate(arr, highest1, highest2, highest3, highest4, highest);


    getAverage(arr, SIZE, average1, average2, average3, average4, average);

    cout << " ";

    sort(arr);
    system("PAUSE");

    return 0;
}

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;
    }
}


void getTotal(StudentData arr[])
{
    for(int i = 0; i < SIZE; i++)
    {
        arr[i].total = arr[i].exam1 + arr[i].exam2 + arr[i].exam3;
    }
}


void getGrade(StudentData arr[])
{
    for(int i = 0; i < SIZE; i++)
    {
        if(arr[i].total >= 90)
            arr[i].ch = 'A';
        else if(arr[i].total >= 80)
            arr[i].ch = 'B';
        else if(arr[i].total >= 70)
            arr[i].ch = 'C';
        else if(arr[i].total >= 60)
            arr[i].ch = 'D';
        else
            arr[i].ch = 'F';
    }
}

void calcLowest(StudentData arr[], int &lowest1, int &lowest2, int &lowest3, int &lowest4, int lowest[])
{
    int smallest = 0;

    lowest1 = arr[0].exam1;
    lowest2 = arr[0].exam2;
    lowest3 = arr[0].exam3;
    lowest4 = arr[0].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;
        }
    }

    for(int index = 0; index < INFO; index++)
    {
        if(index == 0)
            lowest[0] = lowest1;
        else if(index == 1)
            lowest[1] = lowest2;
        else if(index == 2)
            lowest[2] = lowest3;
        else if(index == 3)
            lowest[3] = lowest4;
        else
            cout << "Fail!" << endl;
    }
}


void calculate(StudentData arr[], int &highest1, int &highest2, int &highest3, int &highest4, int highest[])
{
    int biggest = 0;

    highest1 = arr[0].exam1;
    highest2 = arr[0].exam2;
    highest3 = arr[0].exam3;
    highest4 = arr[0].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;
        }
    }

    for(int index = 0; index < INFO; index++)
    {
        if(index == 0)
            highest[0] = highest1;
        else if(index == 1)
            highest[1] = highest2;
        else if(index == 2)
            highest[2] = highest3;
        else if(index == 3)
            highest[3] = highest4;
        else
            cout << "Fail!" << endl;
    }
}


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;
    }

    average1 += static_cast<double>(sum1)/size;

    average2 += static_cast<double>(sum2)/size;

    average3 += static_cast<double>(sum3)/size;

    average4 += static_cast<double>(sum4)/size;

    for(int index = 0; index < INFO; index++)
    {
        if(index == 0)
            average[0] = average1;
        else if(index == 1)
            average[1] = average2;
        else if(index == 2)
            average[2] = average3;
        else if(index == 3)
            average[3] = average4;
        else
            cout << "Fail!" << endl;
    }
}

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].First_name > arr[j].First_name)
            {
                temp = arr[i];  
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}