Question

we will be taking data in as a file. you cannot use the data values in...

we will be taking data in as a file. you cannot use the data values in your source file but you must read in everything into variables and use it from there.

First we need to include <fstream> at the top of our file.

We will need to create an input file stream to work and ifstream. ifstream is a datatype. Create a variable with the datatype being ifstream. Variable is drfine by using the member accessor operator to call the open function and passing in “testGrades.txt”, which is where our data will be.

Example: [whatever the variable name is].open(“testGrades.txt”);

Your program must be setup to open “testGrades.txt” as the file. For testing you can add your own testGrades.txt file into visual studios as a resource (right click resource in solution explorer -> go to add new -> utility -> text file).

The file you will be imputing to your program is a record of the 3 test grade from a class of students. You will need to read in the information for the student number first. This data will be used later to calculate some of the output. “Students:” in the file is only a decoration/label but you will still need to read it in (or use the stream ignore function) so that you can get to the student number. The maximum amount of students these classes will ever have is 8. To read in the first line your code file could look something like this:

       ifstream file;

       file.open("testGrades.txt");

       int students;

       string studentLabel;

       file >> studentLabel;

       file >> students;

We need to read in the total number of attempts on this test. This number will tell us how many grades will follow on the line below and thus how many numbers we will need to read in to compute the test average.

Example input:

Students: 7

Quiz/Homework Average: 66.85

test 1

Attempts: 7

81 90 55 67 101 77 91

test 2

Attempts: 5

24 66 61 98 71

test 3

Attempts: 6

54 87 100 79 67 93

Your output will be the average for each test as well as whether the student did better or worse than the quizzes and homework average. Additionally, output the number of attempts above and below the quizzes and homework average per test.

For the example file above the output should be:

test 1:

Attempts: 7

Average: 80.2857

test 1 average above quiz/homework average

Number above quiz average: 6

Number below quiz average: 1

test 2:

Attempts: 5

Average: 64

test 2 average below quiz/homework average

Number above quiz average: 2

Number below quiz average: 3

test 3:

Attempts: 6

Average: 80

test 3 average above quiz/homework average

Number above quiz average: 5

Number below quiz average: 1

Homework Answers

Answer #1

#include<iostream>
#include<sstream>
#include<fstream>
using namespace std;
int main()
{
   ifstream file;
   //open the input file
    file.open("testGrades.txt");
   string line;
   //read the first line
   getline(file, line);
   stringstream s(line);
   int students;
   string studentLabel;
   s >> studentLabel;
   //read number of students
   s >> students;
   //read the second line
   getline(file, line);
   stringstream ss(line);
   string avglabel1;
   string avglabel2;
   double avg;
   ss >> avglabel1;
   ss >> avglabel2;
   //read average
   ss >> avg;
   string line1, line2, line3;
   //read three lines at a time from file
   while(getline(file, line1))
   {  
       getline(file, line2);
       getline(file, line3);
       //create string stream object for each line
       stringstream ss1(line1);
       stringstream ss2(line2);
       stringstream ss3(line3);
       string label;
       ss1>>label;
       int num;
       //read the test number
       ss1>>num;
       ss2>>label;
       int attempts;
       //read the number of attempts
       ss2>>attempts;
       //create an array to store the marks
       double arr[attempts];
       double average = 0;
       for(int i = 0;i < attempts;i++)
       {
           int marks;
           //read the marks
           ss3 >> marks;
           //store the marks into array
           arr[i] = marks;
           //add the marks to average
           average += marks;
       }
       //calculate the average
       average /= attempts;
       //initialize high average and low average to 0
       int high_avg = 0, low_avg = 0;
       for(int i = 0;i < attempts;i++)
       {
           //check and increment the respective variable
           if(arr[i] > avg)
           {
               high_avg += 1;
           }
           else
           {
               low_avg += 1;
           }
          
       }
       //display the output
       cout<<"Test "<<num<<":"<<endl;
       cout<<"Attempts: "<< attempts<<endl;
       cout<<"Average: "<< average<<endl;
       cout<<"test "<<num<<" average above quiz/homework average"<<endl;
       cout<<"Number above quiz average: "<<high_avg<<endl;
       cout<<"Number below quiz average: "<< low_avg<<endl;
   }
  
}

If you have any doubts please comment and please don't dislike.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
JAVA Question a) Read a file named testScoreIn.txt representing test scores for a class of students....
JAVA Question a) Read a file named testScoreIn.txt representing test scores for a class of students. Each record contains an id number and three test scores (Note: the test scores are integers). For example, a student record might look like: BK1234 87 72 91 b) Without using an array, read each student’s information, one record at a time and compute the student’s average (as a double). c) Write to an output file, testScoreOut.txt, as a neatly formatted table with column...
The following code to run as the described program on python. The extra test file isn't...
The following code to run as the described program on python. The extra test file isn't included here, assume it is a text file named "TXT" with a series of numbers for this purpose. In this lab you will need to take a test file Your program must include: Write a python program to open the test file provided. You will read in the numbers contained in the file and provide a total for the numbers as well as the...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Create a program that filters the data in a CSV file of product data based on...
Create a program that filters the data in a CSV file of product data based on some search word and prints the resulting output to a new file. Additionally, the program will print the number of items filtered to stdout. • Your program should take three arguments: an input file to process, an output file to save the results, and a search word. • If the output file already exists or cannot be opened, warn the user by printing "CANNOT...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain account numbers (10 digits) along with the account type (L:Loan and S:Savings) and their current balance. The second file (transactions.txt) will contain transactions on the accounts. It will specifically include the account number, an indication if it is a withdrawal/deposit and an amount. Both files will be pipe delimited (|) and their format will be the following: accounts.txt : File with bank account info...
Create a program that copies the data from one file to another while converting all lowercase...
Create a program that copies the data from one file to another while converting all lowercase vowels to uppercase vowels. Your program should accept two arguments: an input file to read from and an output file to copy to. • Your program should check to make sure the output file does not already exist. If it does, print "DESTINATION FILE EXISTS." to stdout. • Print the number of characters changed to stdout using the format string "%d characters changed." •...
Run a regression in Excel/MegaStaat. After you have the data in your Excel file, from the...
Run a regression in Excel/MegaStaat. After you have the data in your Excel file, from the MegaStat menu select Correlation / Regression, open the Regression applet and fill in the appropriate information. Select OK to run. Output will be in the Output tab created by MegaStat. The data are as follows:   Tree height Tree Diameter 35 8 49 9 27 7 33 6 60 13 21 7 45 11 51 12
Create a program to calculate and print basic stats on a set of a given input...
Create a program to calculate and print basic stats on a set of a given input values representing students' scores on an quiz. 1) Ask the user for the total number of quiz scores to be input (assume a positive integer will be given). 2) Create an array to hold all the quiz scores and then get all the scores from input to put into the array. For example, if the user input 10 in step (1) then you should...
The following 4 parts relate to this scenario and data. A professor wishes to develop a...
The following 4 parts relate to this scenario and data. A professor wishes to develop a numerical method for giving grades. He intends to base the on homework, two tests, a project, and a final. He wishes the final to have the largest influence on the grade. He wants the project to have 20%, each test to have 10%, and the homework to have 20% of the influence on the semester grade. 1. Determine the weights the professor should use...
The following 4 parts relate to this scenario and data. A professor wishes to develop a...
The following 4 parts relate to this scenario and data. A professor wishes to develop a numerical method for giving grades. He intends to base the on homework, two tests, a project, and a final. He wishes the final to have the largest influence on the grade. He wants the project to have 20%, each test to have 10%, and the homework to have 20% of the influence on the semester grade. 1. Determine the weights the professor should use...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT