In: Computer Science
(c++)You will be given a data file containing data for 10 students. The format is as follows - grades are double precision numbers:
Line 1: Header Information
Line 2: Student Full name
Line 3: Student ID
Line 4: testgrade_1 testgrade_2 testgrade_3 testgrade_4 testgrade_5
Line 5: Student Full name
Line 6: Student ID
Line 7: testgrade_1 testgrade_2 testgrade_3 testgrade_4 testgrade_5
Line 8: Student Full name
Line 9: Student ID
Line 10: testgrade_1 testgrade_2 testgrade_3 testgrade_4 testgrade_5
Etc.
Read the data into appropriate arrays. You do not need to create a dynamic array.
Determine the average of all the grades for each test, and print the test number then the name, student id, and grade for all students who fall below that average.
Determine the average of all the grades for each student.
Determine which student has the highest average grades - print their name and their average
Determine which student has the lowest average grades - print their name and their average
Student Learning Outcomes:
Creating and using functions
Creating and using non-dynamically allocated arrays - 1-Dimensional and/or 2-Dimensional
File input
Style
Variable Naming
Proper variable and function naming (not uppercase to start, descriptive of purpose)
Proper constant naming (all capital letters)
Comments
Programmer name, date, purpose
Useful, descriptive comments about program procedures
Useful, descriptive comments about function purpose, inputs, and outputs
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <cstdlib>
#define MAX 100
using namespace std;
// Defines a class Student
class Student
{
// Data member to store student data
string branch;
string name;
int id;
double marks[3];
public:
// Default constructor to assign default values to data
member
Student()
{
branch = name = "";
id = 0;
marks[0] = marks[1] = marks[2] = 0.0;
}// End of default constructor
// Function to read file contents and stores it in array of
object of Student class
// Returns number of records by reference
void readRecord(Student stu[], int &len)
{
string ch;
// ifstream class object declared to read data from file
ifstream fileRead;
// Opens both the file for reading
fileRead.open("studentInfo.txt");
// Checks if the file unable to open for reading display's error
message and stop
if(!fileRead)
{
cout<<"\n ERROR: Unable to open the file for reading.";
exit(0);
}// End of if condition
// Loops till end of the file
while(!fileRead.eof())
{
// Reads a record
getline(fileRead, stu[len].branch, '\n');
getline(fileRead, stu[len].name, '\n');
fileRead>>stu[len].id;
// Reads the marks
for(int c = 0; c < 3; c++)
fileRead>>stu[len].marks[c];
// Reads new line character
getline(fileRead, ch, '\n');
// Increase the record counter
len++;
}// End of while loop
}// End of function
// Function to determine the average of all the grades for each
student and returns average
double average(Student stu)
{
// To store total
double total = 0;
// Loops 3 times for marks
for(int d = 0; d < 3; d++)
// Calculates total
total += stu.marks[d];
// Returns average
return total / 3.0;
}// End of function
// Function to display all students information
void showAll(Student stu[], int len)
{
cout<<"\n\n *********** All Student Information ***********
";
// Loops till number of students
for(int c = 0; c < len; c++)
{
// Displays student information
cout<<"\n Branch: "<<stu[c].branch<<"\n Name:
"<<stu[c].name<<"\n ID: "<<stu[c].id;
cout<<"\n Marks \n";
// Loop 3 times to display marks
for(int d = 0; d < 3; d++)
cout<<"\t Mark1: "<<stu[c].marks[d];
// Displays average
cout<<"\n Average: "<<average(stu[c]);
}// End of for loop
}// End of function
// Function to determine which student has the highest average
grades - print their name and their average
void highestAvg(Student stu[], int len)
{
cout<<"\n\n *********** Student Highest Average ***********
";
// Calls the function to get the average of first student
// and stores it in maxAvg as maximum average
double maxAvg = average(stu[0]);
// Stores the first position as highest average student
position
int pos = 0;
// Loops till number of students starting from 1
// Because 0th position student already considered above
for(int c = 1; c < len; c++)
{
// Calls the function to get average of each student
double currentAvg = average(stu[c]);
// Checks if current student average is greater then earlier
student average
if(currentAvg > maxAvg)
{
// Assigns the current average as maximum average
maxAvg = currentAvg;
// Stores the index position as highest student position
pos = c;
}// End of if condition
}// End of for loop
// Displays student name and average
cout<<"\n Name: "<<stu[pos].name<<"\n Average:
"<<maxAvg;
}// End of function
// Function to determine which student has the lowest average
grades - print their name and their average
void lowestAvg(Student stu[], int len)
{
cout<<"\n\n *********** Student Lowest Average ***********
";
// Calls the function to get the average of first student
// and stores it in minAvg as minimum average
double minAvg = average(stu[0]);
// Stores the first position as lowest average student
position
int pos = 0;
// Loops till number of students starting from 1
// Because 0th position student already considered above
for(int c = 1; c < len; c++)
{
// Calls the function to get average of each student
double currentAvg = average(stu[c]);
// Checks if current student average is less then earlier
student average
if(currentAvg < minAvg)
{
// Assigns the current average as minimum average
minAvg = currentAvg;
// Stores the index position as lowest student position
pos = c;
}// End of if condition
}// End of for loop
// Displays student name and average
cout<<"\n Name: "<<stu[pos].name<<"\n Average:
"<<minAvg;
}// End of function
};// End of class
// main function definition
int main()
{
// Creates an array of Student array of objects of size MAX
Student stu[MAX];
// Initializes the record counter to 0
int len = 0;
// Calls the function to read file contents
stu[0].readRecord(stu, len);
// Calls the function to display all student information
stu[0].showAll(stu, len);
// Calls the function to display highest average student
information
stu[0].highestAvg(stu, len);
// Calls the function to display lowest student information
stu[0].lowestAvg(stu, len);
return 0;
}// End of main function
Sample Output:
*********** All Student Information ***********
Branch: CSE
Name: Pyari Mohan Sahu
ID: 111
Marks
Mark1: 99.23 Mark1: 98.77 Mark1: 89.88
Average: 95.96
Branch: ECE
Name: Ram Mohan Panda
ID: 132
Marks
Mark1: 78.21 Mark1: 67.31 Mark1: 81.45
Average: 75.6567
Branch: ME
Name: Tanvi Mohan Sahu
ID: 222
Marks
Mark1: 97.53 Mark1: 89.67 Mark1: 80.84
Average: 89.3467
Branch: CE
Name: Manvi Sahu
ID: 333
Marks
Mark1: 98.83 Mark1: 91.79 Mark1: 90.81
Average: 93.81
*********** Student Highest Average ***********
Name: Pyari Mohan Sahu
Average: 95.96
*********** Student Lowest Average ***********
Name: Ram Mohan Panda
Average: 75.6567
StudentInfo.txt file contents
CSE
Pyari Mohan Sahu
111
99.23 98.77 89.88
ECE
Ram Mohan Panda
132
78.21 67.31 81.45
ME
Tanvi Mohan Sahu
222
97.53 89.67 80.84
CE
Manvi Sahu
333
98.83 91.79 90.81