In: Computer Science
C++ Programming
Develop and submit an original implementation of a menu-driven
program performing a
number of tasks relating to student marks scored in a number of
assessments including:
displaying all marks, adding new student marks, calculating the
mean score, median
score, finding the minimum and maximum scores as well as displaying
the average mark
of a given student.
The 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:
2
S1234567
55
70
4
S2222222
96
67
88
88
So, according to 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
the average mark for a given student.
Your program should be modular and menu-driven as described
below:
• The main() function handles all interactions with the user and
other functions:
Ø It displays an appropriate welcoming message introducing the
program
Ø Calls a function named readFile() which opens the text file
marks.txt
for reading and stores, all of the students marks from the file to
an array
named marksArray. The readFile()function has two parameters:
one
for receiving the file variable and one for the array, both
receiving arguments
passed by reference
Ø It (the main function) then repeatedly calls the menu() function
to display
user options, get the user selection returned by the menu()
function, use a
switch statement to process user request by calling the appropriate
function
Ø It displays the result with an appropriate message after
processing user
request
Ø It displays a goodbye message when the user selects the Quit
option (option
8) from the menu and terminates the program
• The menu() function has no parameters. When called, it displays a
menu of 8
options allowing the user to select one and returns this option to
the calling main()
function. The options displayed should be:
(1) Display marks
(2) Calculate mean
(3) Calculate the median
(4) Find the minimum
(5) Find maximum
(6) Find an average for student
(7) Add new student data
(8) Quit program
• Option (1) will use a function called displayMarks() called from
the main()to
display the contents of the marks array on the screen in an
appropriate format. The
displayMarks() function has two parameters: the array and the
number of
values in the array.
• Option (2) will use a function called calculateMean() which is
designed to
calculate the mean value of all scores in marksArray and return the
result to the
main() 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.
• Option (3) will use a function called calculateMedian() which is
designed to
calculate the median value of all scores in marksArray and return
the result to
the main() function which will then display it with an appropriate
message. The
median value is calculated by finding the middle value when the
values are sorted
in ascending (or descending) order. When there are two middle
values (as is the
case when the number of values is even), then the average of the
two middle
values is the median value. Parameters for this function are the
same as the
previous function.
• Option (4) will use a function called findMinimum()which is
designed to find the
smallest value of all scores in marksArray and return the result to
the main()
function which will then display it with an appropriate message.
Again, the
parameters for this function are the same as the previous
function.
• Option (5) will use a function called findMaximum()which is
designed to find the
largest value of all scores in marksArray and return the result to
the main()
function which will then display it with an appropriate message.
Again, the
parameters for this function are the same as the previous
function.
• Option (6) will use a function called
findAverageForStudent()which is
designed to get a student id as an argument as well as a reference
to the marks
data file from the main() function, open the marks.txt file, find
the marks in the
file for that particular student, calculate the average value and
return it to the main
function which will then display it with an appropriate message.
The main function
should be responsible for prompting the user for the student
id.
• Option (7) will first use a function called updateFile() which
will open the file in
append mode, prompt the user for new student’s id followed by a
varying number
of marks, and then write the new data at the end of the file using
the same format
as the original file. It will then call the readFile()function used
in the beginning of
the program again to read the contents of the updated file and
repopulate the
marksArray, so that the data consistency is preserved.
• Option (8) will terminate the program after displaying an
appropriate goodbye
message.
***Contents of the “marks.txt” file are below***
2
S1234567
55
70
4
S2222222
96
67
88
88
3
S1111111
45
37
61
5
S3333333
98
56
100
87
77
3
S444S4444
23
73
51
2
S5555555
89
88
4
S6666666
77
88
99
66
#include <iostream> //Input Ouput Stream
#include <fstream> //File Stream for reading and writing files
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
int readFile(string file_name,string marks[]);
int menu();
int getScores(string marks_arr[],int size,int marks[]);
void displayMarks(string marks_arr[],int size);
void calculateMean(string marks_arr[],int size);
void calculateMedian(string marks_arr[],int size);
void findMinimum(string marks_arr[],int size);
void findMaximum(string marks_arr[],int size);
void findAverageForStudent(string student_id,string marks_arr[],int size);
void updateFile(string file_name,string marks_arr[],int size);
int main()
{
cout << "****************WELCOME TO THE STUDENT'S ASSESMENT SCORES PORTAL****************\n";
string marks_arr[100];
int sizeOfFile=readFile("marks.txt",marks_arr);
while(true)
{
int ch=menu();
switch(ch){
case 1: {
displayMarks(marks_arr,sizeOfFile);
break;
}
case 2: {
calculateMean(marks_arr,sizeOfFile);
break;
}
case 3: {
calculateMedian(marks_arr,sizeOfFile);
break;
}
case 4: {
findMinimum(marks_arr,sizeOfFile);
break;
}
case 5: {
findMaximum(marks_arr,sizeOfFile);
break;
}
case 6: {
cout << "\n Enter the student ID: ";
string student_id;
cin >> student_id;
findAverageForStudent(student_id,marks_arr,sizeOfFile);
break;
}
case 7: {
updateFile("marks.txt",marks_arr,sizeOfFile);
break;
}
case 8: {
cout << "Thanks for using the portal!";
exit(0);
break;
}
default: {
cout << "Wrong Choice\n";
break;
}
}
}
return 0;
}
int readFile(string str,string marks_arr[])
{
//read file
fstream infile(str.c_str(),ios::in);
int i=0;
string data;
while(infile>>data)
{
marks_arr[i]=data;
i++;
}
int count=i;
infile.close();
return count;
}
int menu()
{
cout << "\n**************MENU***************\n";
cout << "(1) Display Marks "<< end1;
cout << "(2) Calculate Mean "<< end1;
cout << "(3) Calculate Median "<< end1;
cout << "(4) Find the minimum "<< end1;
cout << "(5) Find the maximum "<< end1;
cout << "(6) Find the average for student "<< end1;
cout << "(7) Add new student data "<< end1;
cout << "(8) Quit program "<< end 1;
cout << "\nEnter your Choice: ";
int ch;
cin >> ch;
return ch;
}
void displayMarks(string marks_arr[],int sizeOfFile){
int i=0;
while(i<sizeOfFile){
cout<< "\n student_id: "<< marks_arr[i+1] <<"Assessment scores: ";
stringstream conv(marks_arr[i]);
int scores;
conv >>scores;
for(int j=0;j<scores;j++){
cout<< marks_arr[i+2+j]<<" ";
}
cout << end1;
i=i+scores+2;
}
}
void calculateMean(string marks_arr[],int sizeOfFile){
int scores_arr[sizeOfFile];
int size=getScores(marks_arr,aizeOfFile,scores_arr);
int sum=0;
for(int i=0;i<size;i++){
sum+=scores_arr[i];
}
cout << "Mean Value of all scores is :" sum/size << end1;
}
int getScores(string marks_arr[],int sizeOfFile,int scores_arr[])
{
int i=0,k=0;
while(i< sizeOfFile)
{
stringstream conv(marks_arr[i]);
int count;
conv >> count;
for(int j=0;j<count;j++)
{
stringstream conv(marks_arr[i+2+j]);
int n;
conv >>n;
scores_arr[k]=n;
k++;
}
i=i+count+2;
}
return k;
}
void calculateMedian(string marks_arr[],int sizeOfFile)
{
int scores_arr[sizeOfFile];
int size=getScores(marks_arr,sizeOfFile,scores_arr);
sort(scores_arr,scores_arr+size);
int median=scores_arr[size/2];
cout << "Median Value of all scores is : "<< median <<end1;
}
void findMinimum(string marks_arr[],int sizeOfFile){
int scores_arr[sizeOfFile];
int size=getScores(marks_arr,sizeOfFile,scores_arr);
int min=INT_MAX;
for(int i=0;i<size;i++){
if(scores_arr[i]<min){
min=scores_arr[i];
}
}
cout<< "Smallest value of all scores: "min <<end1;
}
void findMaximum(string marks_arr[],int sizeOfFile){
int scores_arr[sizeOfFile];
int size=getScores(marks_arr,sizeOfFile,scores_arr);
int max=INT_MIN;
for(int i=0;i<size;i++){
if(scores_arr[i]>max){
max=scores_arr[i];
}
}
cout <<"Largest value of all scores : << max << end1;
}
void FindAverageForStudent(string student_id,string marks_arr[],int sizeOfFile){
int i=1;
bool f=false;
while(i<sizeOfFile){
if(marks_arr[i]==student_id){
f=true;
break;
}
i++;
}
if(f)
{
stringstream conv(marks_arr[i-1]);
int c;
conv>>c;
int sum=0;
for(int j=i+1;j<=i+c;j++){
stringstream conv(marks_arr[j]);
int n;
conv>>n;
sum+=n;
}
cout <<"Average score of the student " student_id <<" is: "sum/c << end1;
}
else{
cout << "Invalid student ID!" << end1;
}
}
void updateFile(string file_name,string marks_arr[],int sizeOfFile){
fstream outfile(file_name.c_str(),ios::app);
int sub;
string student_id;
cout << "Enterthe number of Assessments: ";
cin >> sub;
outfile << sub << end1;
cout << "Enter the new student ID: ";
cin >> student_id;
outfile << student_id << end1;
for(int i=0;i<sub;i++){
string str;
cout << "Enter scores for each assessments " << i+1 << ": ";
cin >> str;
outfile << str << end1;
}
cout << "Successfully added!" << end1;
outfile.close();
sizeOfFile=readFile(file_name,marks_arr);
}
Attached the code snippet for required question!