Question

In: Computer Science

The C++ problem: Student marks are kept in a text file as a single column. Each...

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:

  • 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 (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

  • 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 or a multi-alternative if 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

  • 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 received from the user, and then write the new data at the end of the file using the same format as the original file.
  • Option (8) will terminate the program after displaying an appropriate goodbye message.

Solutions

Expert Solution

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


Related Solutions

Write all your answers for this problem in a text file named aa.txt – for each...
Write all your answers for this problem in a text file named aa.txt – for each problem write the problem number and your answer. 1.1 What type of object can be contained in a list (write the letter for the best answer)? a. String b. Integer c. List d. String and Integer only e. String, Integer, and List can all be contained in a list 1.2 Which statement about the debugger is not correct? a. It is a powerful tool...
Selection sort on fstream data C++: Sort the given student information data inside a text file...
Selection sort on fstream data C++: Sort the given student information data inside a text file The format of the student data inside the text file is: Student Number, Surname, First Name, Middle Name, Birthday, Course, Year, School Year (The list can be updated over time so there is no limit for the size of the data) But I need to automatically sort the given the data in ascending alphabetical order based on their surnames when displaying the list. I...
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...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such as: "name,23" on every line where a comma is the delimiter, how would I separate the string "name" and integer "23" and set them to separate variables for every input of the text file? I am trying to set a name and age to an object and insert it into a vector. #include <iostream> #include <vector> #include <fstream> using namespace std; class Student{    ...
Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
in c++ (Sum, average and product of numbers in a file) Suppose that a text file...
in c++ (Sum, average and product of numbers in a file) Suppose that a text file Exercise13_3.txt contains six integers. Write a program that reads integers from the file and displays their sum, average and product. Integers are separated by blanks. Instead of displaying the results on the screen, send the results to an output named using your last name. Example:       Contents of Exercise13_3.txt: 100 95 88 97 71 67 80 81 82             Contents of YourLastName.txt: Your...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Assignment in C programming class: read the text file line by line, and place each word,...
Assignment in C programming class: read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a...
Complete each problem on a separate worksheet in a single Excel file. Rename the separate worksheets...
Complete each problem on a separate worksheet in a single Excel file. Rename the separate worksheets with the respective problem number. You may have to copy and paste the datasets into your homework file first. Name the file with your last name, first initial, and HW #2. Label each part of the question. When calculating statistics, label your outputs. Use the Solver add-in for these problems. For a telephone survey, a marketing research group needs to contact at least 600...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT