Question

In: Computer Science

Need to create a program in C++ that can display/write into a file called marks.txt. I'm...

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

Solutions

Expert Solution

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

Related Solutions

Write a C++ program in a file called pop.cpp that opens a file called pop.dat which...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which has the following data (you’ll have to create pop.dat) AX013 1.0 BX123456 1234.56 ABNB9876152345 99999.99 The data are account numbers and balances. Account numbers are, at most 14 characters. Balances are, at most, 8 characters (no more than 99999.99). Using a loop until the end of file, read an account number and balance then write these to standard output in the following format shown...
Write a C++ program that will display the top internet stories from the stories.txt file The...
Write a C++ program that will display the top internet stories from the stories.txt file The program must display the stories that have a score which the statistical "mode". The "mode" of a set of values is the value that occurs most often (with the greatest frequency) The data about the stories is in a file that contains the following: storyTitle    (a sequence of numbers and/or letters, may contain spaces in it) storyURL    (a regular URL, like http://www.twitter.com/story1,without spaces) score        (an integer number,...
Write a C program that opens a file called "numbers.txt" in writing mode. The program should...
Write a C program that opens a file called "numbers.txt" in writing mode. The program should then read floating point numbers from the keyboard, and write these lines to the opened file one per line, stopping when the number 0 is entered. Your program should check to make sure that the file was opened successfully, and terminate if it was not.
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Write a C++ program to display toy name
Write a C++ program to display toy name
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT