Question

In: Computer Science

C++ Programming Develop and submit an original implementation of a menu-driven program performing a number of...

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

Solutions

Expert Solution

#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!


Related Solutions

Write a menu driven C++ program that prints the day number of the year , given...
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java program that will solve the following problem. The program uses one and two-dimensional arrays to accomplish the tasks specified below. The menu is shown below. Please build a control panel as follows: (Note: the first letter is shown as bold for emphasis and you do not have to make them bold in your program.) Help SetParams FillArray DisplayResults Quit Upon program execution, the screen...
In C++ Language English/Spanish Translation Program. Create a menu driven program that translates English to Spanish...
In C++ Language English/Spanish Translation Program. Create a menu driven program that translates English to Spanish and Spanish to English. Your translation program should use arrays for this program. You will need to populate the arrays with the contents of the English and Spanish data files provided. The two files are ordered such that each word in the one file corresponds to the respective translation in the other (i.e.: the first word in the ENG.txt file corresponds to the first...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file has unknown number of records of inventory items, but no more than 100; one record per line in the following order: item ID, item name (one word), quantity on hand , and a price All fields in the input file are separated by a tab (‘\t’) or a blank ( up to you) No error checking of the data required Create a menu which...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End Program.The user can only input a positive integer (0 does not count) and if the input is not a positive integer the program must state "Invalid Input." The program must determine whether the input is even or odd. The program must use functions and can only use the iostream and iomanip libraries. Note: The program must loop until the 2nd menu option is chosen.
This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program should allow the user to select one of the options according to whether lengths, weights or volume are to be converted, read the volue to be converted and the units, and then call the appropriate function to carry out the conversion In unit out unit I C (inch to centimeter 1 in = 2.4 cm) F C (feet to centimeter 1 ft = 30.4...
Write a menu-driven program to handle the flow of widgets into and out of a warehouse....
Write a menu-driven program to handle the flow of widgets into and out of a warehouse.     The warehouse will have numerous deliveries of new widgets and orders for widgets     The widgets in a filled order are billed at a profit of 50 percent over their cost     Each delivery of new widgets may have a different cost associated with it     The accountants for the firm have instituted a last-in, first-out system for filling orders         the newest...
Thread Programming (C Programming) Objective Develop threads: thread and main. Functionality of Threads: The main program...
Thread Programming (C Programming) Objective Develop threads: thread and main. Functionality of Threads: The main program accepts inputs from the user from the console. The user can provide the following two types of input: add num1 num2 mult num1 num2 Here, num1 and num2 are two integer numbers. E.g., the user may input add 1 2. The threads receive the command along with the number, and performs the appropriate arithmetic operation and returns the results to main program. The main...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT