In: Computer Science
Need to create a program in C++ that can display/write into a file called marks.txt. I'm not too worried about the functions, but I don't know how to store the different marks into a arrays. Any help would be appreaciated. Here's some details about the assignment.
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 (for the complete data, refer to marks.txt file on : 2 S1234567 55 70 2 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 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 student 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 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 median (4) Find minimum (5) Find maximum (6) Find average for student (7) Add new student data (8) Quit program
marks.txt will look like this.
2
S1234567
55
70
4
S2222222
96
67
88
88
You asked about how to go about storing different marks into an array. Sample code below shows how:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int no_marks, marks_array [100], marks_ctr = 0, student_ctr = 0;
string student_ids [100]; // student ids for each students
int how_many_marks [100]; // how many marks for each student
ifstream f ("marks.txt");
while (f >> no_marks) {
how_many_marks [student_ctr] = no_marks;
f >> student_ids [student_ctr++];
for (int i = 0; i < no_marks; i++)
f >> marks_array [marks_ctr++];
}
f.close ();
for (int i = 0; i < student_ctr; i++)
cout << student_ids [i] << "\n";
for (int i = 0; i < student_ctr; i++)
cout << how_many_marks [i] << "\n";
for (int i = 0; i < marks_ctr; i++)
cout << marks_array [i] << "\n";
return 0;
}
Pseudo code for the question in the comment:
You can loop through student_ids and how_many_marks from 0 to student_ctr -1 in nested loop, pseudocode below
start_posn = 0
for (i = 0; i < student_ctr; i++)
print student_id [i]
for (j=0; j < how_many_marks[i]; j++)
// marks_array [start_posn + i] has the marks
// do whatever you want to do with each of the marks for this student
end for
start_posn = start_posn + j
end for