In: Computer Science
The C++ problem:
Student marks are kept in a text file as a single column. Each student may have a different number of assessments and therefore scores. The data recorded in the file for each student start with the number of scores for the student. This is followed by the student id and then several marks student scored in various assessments, one score per line. A small segment of the file might look like the following: (file name is marks.txt)
2
S1234567
55
70
4
S2222222
96
67
88
88
So, according data presented in this file segment, the first student has 2 scores, student id is S1234567, and the assessment scores are 55 and 70. The second students has 4 scores, student id is S2222222, and the assessment scores are 96, 67, 88 and 88.
It is anticipated (assumed) that the total number of scores in the marks.txt file will not exceed 100.
The program functionality:
Your program will utilise a number of user-defined functions to display data from the file, update data by adding marks for new students, as well as performing a number of other tasks including calculating the mean and median score of all marks and finding the minimum and maximum scores in the collection as well as calculating and displaying the average mark for a given student.
Your program should be modular and menu-driven as described below:
reading and stores all of the student marks from the file to an array (NOT A VECTOR!) namedmarksArray. The readFile()function has two parameters: one for receiving the file variable and one for the array, both receiving arguments passed by reference.
later
options, get the user selection returned by the menu() function, use a switch statement or a multi-alternative if statement to process user request by calling appropriate function
(1) Display marks
(2) Calculate mean
(3) Calculate median
(4) Find minimum
(5) Find maximum
(6) Find average for student
(7) Add new student data
(8) Quit program
function which will then display it with an appropriate message. This function also has two parameters: the array and the number of values in the array.
which will then display it with an appropriate message. Again, the parameters for this function are the same as the previous function.
Marks.cpp
#include <iostream>
#include<fstream>
#include <string>
using namespace std;
int sz = 0;
fstream file;
void readFile(fstream *filepointer, float *marks)
{
string line;
filepointer->open("marks.txt");
while (getline(file, line))
{
int i = stoi(line);
getline(*filepointer, line);
//cout<<"Reading
..."<<line<<endl;
for (int j = 0; j < i;
j++)
{
getline(*filepointer, line);
marks[sz] =
stof(line);
sz = sz +
1;
}
}
filepointer->close();
}
void displayMarks(float marks[], int sz)
{
cout << "MARKS" << endl;
for (int i = 0; i < sz; i++)
{
cout << marks[i] <<
"\t";
}
cout << endl;
}
float calculateMean(float marks[], int sz)
{
float mean = 0;
for (int i = 0; i < sz; i++)
{
mean += marks[i];
}
mean = mean / sz;
return mean;
}
float calculateMedian(float marks[], int sz)
{
float median = 0;
float temp[100];
for (int i = 0; i < sz; i++)
{
temp[i] = marks[i];
}
for (int i = 0; i < sz; i++)
{
for (int j = i; j < sz - 1;
j++)
{
if (temp[j] >
temp[j + 1])
{
float temp_val = temp[j];
temp[j] = temp[j + 1];
temp[j + 1] = temp_val;
}
}
}
if (sz % 2 == 1)
{
return (temp[(sz / 2)]);
}
return (((temp[(sz / 2)]) + (temp[(sz / 2) - 1])) /
2);
}
float findMinimum(float marks[], int sz)
{
float min = 101;
for (int i = 0; i < sz; i++)
{
if (marks[i] < min)
min =
marks[i];
}
return min;
}
float findMaximum(float marks[], int sz)
{
float max = 0;
for (int i = 0; i < sz; i++)
{
if (marks[i] > max)
max =
marks[i];
}
return max;
}
float findAverageForStudent(string id)
{
float avg = 0;
string line;
file.open("marks.txt");
while (getline(file, line))
{
int i = stoi(line);
getline(file, line);
//cout<<"Reading
..."<<line<<endl;
if (line.compare(id) == 0)
{
for (int j = 0;
j < i; j++)
{
getline(file, line);
avg += stof(line);
}
avg = avg /
i;
}
}
file.close();
return avg;
}
void updateFile(int num, string id, float *marks)
{
file.open("marks.txt", ios::app);
file << num << "\n";
file << id << "\n";
for (int j = 0; j < num; j++)
{
file << marks[j] <<
"\n";
}
file.close();
}
int main()
{
bool flag = true;
int choice = 0;
float marks[100];
readFile(&file, marks);
while (flag)
{
cout << "(1) Display
marks\n(2) Calculate mean\n(3) Calculate median\n(4) Find
minimum\n(5) Find maximum\n(6) Find average for student\n(7) Add
new student data\n(8) Quit program\n";
cin >> choice;
switch (choice)
{
case 1: {
displayMarks(marks, sz);
break;
}
case 2: {
cout <<
"MEAN: " << calculateMean(marks, sz) << endl;
break;
}
case 3: {
cout <<
"MEDIAN: " << calculateMedian(marks, sz) << endl;
break;
}
case 4: {
cout <<
"MIN: " << findMinimum(marks, sz) << endl;
break;
}
case 5: {
cout <<
"MAX: " << findMaximum(marks, sz) << endl;
break;
}
case 6: {
string id;
cout <<
"Enter student ID" << endl;
cin.ignore(1,
'\n');
getline(cin,
id);
cout <<
"Avg_of_Student: " << id << " --- " <<
findAverageForStudent(id) << endl;
break;
}
case 7: {
int num;
cout <<
"Enter num of entries for the student" << endl;
cin >>
num;
string id;
cout <<
"Enter student ID" << endl;
cin.ignore(1,
'\n');
getline(cin,
id);
cout <<
"Enter marks" << endl;
float *marks =
new float[num];
for (int i = 0;
i < num; i++)
{
cin >> marks[i];
}
updateFile(num,
id, marks);
break;
}
case 8: {
file.close();
flag =
false;
cout <<
"GOODBYE" << endl;
break;
}
default:cout << "Not a valid
option" << endl;
}
}
}
INPUT:
OUTPUT:
OUTPUT:
(1) Display marks
(2) Calculate mean
(3) Calculate median
(4) Find minimum
(5) Find maximum
(6) Find average for student
(7) Add new student data
(8) Quit program
1
MARKS
55 70 96 67 88 88
(1) Display marks
(2) Calculate mean
(3) Calculate median
(4) Find minimum
(5) Find maximum
(6) Find average for student
(7) Add new student data
(8) Quit program
2
MEAN: 77.3333
(1) Display marks
(2) Calculate mean
(3) Calculate median
(4) Find minimum
(5) Find maximum
(6) Find average for student
(7) Add new student data
(8) Quit program
3
MEDIAN: 79
(1) Display marks
(2) Calculate mean
(3) Calculate median
(4) Find minimum
(5) Find maximum
(6) Find average for student
(7) Add new student data
(8) Quit program
4
MIN: 55
(1) Display marks
(2) Calculate mean
(3) Calculate median
(4) Find minimum
(5) Find maximum
(6) Find average for student
(7) Add new student data
(8) Quit program
5
MAX: 96
(1) Display marks
(2) Calculate mean
(3) Calculate median
(4) Find minimum
(5) Find maximum
(6) Find average for student
(7) Add new student data
(8) Quit program
6
Enter student ID
S1234567
Avg_of_Student: S1234567 --- 62.5
(1) Display marks
(2) Calculate mean
(3) Calculate median
(4) Find minimum
(5) Find maximum
(6) Find average for student
(7) Add new student data
(8) Quit program
7
Enter num of entries for the student
2
Enter student ID
S23345354
Enter marks
45
67
(1) Display marks
(2) Calculate mean
(3) Calculate median
(4) Find minimum
(5) Find maximum
(6) Find average for student
(7) Add new student data
(8) Quit program
8
GOODBYE