Question

In this assignment, you will write a program in C++ which uses files and nested loops...

In this assignment, you will write a program in C++ which uses files and nested loops to create a file from the quiz grades entered by the user, then reads the grades from the file and calculates each student’s average grade and the average quiz grade for the class.

Each student takes 6 quizzes (unknown number of students). Use a nested loop to write each student’s quiz grades to a file. Then read the data from the file in order to display the student’s average score and the class average.

Use the data below when executing your program. Enter 7 items for each student. Column 1 is the student ID and Column 2-7 are the quiz grades.

Student id

Quiz 1

Quiz 2

Quiz 3

Quiz 4

Quiz 5

Quiz 6

1000

90

100

81

100

70

81

1001

85

95

86

100

87

96

1002

50

60

74

100

75

89

1008

100

93

87

100

91

88

Requirements:

Use a nested loop structure to input the data and write the data to a text file (have the user enter the text file name):

The outer loop will be a while loop; the inner loop will be a for loop (6 quizzes).

Validate whether the quiz scores are in range 0 – 100, inclusive.

Since you do not know how many students will be entered, determine a way to quit the loop.

Add spaces in between each item added to the text file, except add a new line after each complete set of student data.

The text file that your program creates should look like the format in the example below, but the actual data may be different:

Use a nested loop structure to read the data from the text file and calculate the student’s average grade (validate that the file is available):

The outer loop will be a while loop; the inner loop will be a for loop

To calculate each student’s average score, use a total variable initialized to 0 before the for loop, then calculate the student’s average after the loop.

To calculate the class average, initialize a classTotal variable to 0 before the while loop, add each student’s total into the classTotal following the for loop, then calculate the classAverage after the while loop.

Only display 1 decimal position for the averages.

BEWARE of integer division.

Use cout to display the values of the variables. Your cout statements MUST use the variables to display the values

0 0
Add a comment Improve this question Transcribed image text
Answer #1

here is your program : ------------->>>>>>>>>>>>

#include<iostream>
#include<fstream>

using namespace std;

void readAndWrite(string file){
ofstream ofs;
ofs.open(file.c_str());
if(ofs.is_open()){
  string ch;
  int id;
  int score;
  //first while loop
  while(true){
   cout<<"\nEnter Student ID : ";
   cin>>id;
   ofs<<id<<" ";
   //second loop
   for(int i = 0;i<6;i++){
    cout<<"\nEnter Quiz"<<(i+1)<<" Score : ";
    cin>>score;
    //for checking the score is between 0-100
    if(score < 0 || score > 100){
     i--;
     cout<<"\nScore Must in between 0-100 .";
     continue;
    }
    ofs<<score<<" ";
   }
   ofs<<"\n";
   cout<<"\nWant to enter more student data ? (y/n)";
   cin>>ch;
   if(ch == "n" || ch == "N"){
    break;
   }
  }
  ofs.close();
}else{
  cout<<"\nOutput File Creating error";
  exit(-1);
}
}

void readAndProcess(string file){
ifstream ifs;
ifs.open(file.c_str());
int id;
int score;
if(ifs.is_open()){
  ifs>>id;
  int classTotal = 0;
  int studentTotal = 0;
  int numStu = 0;
  //first loop
  while(!ifs.eof()){
   //second loop for reading score of quiz
   studentTotal = 0;
   for(int i = 0;i<6;i++){
    ifs>>score;
    studentTotal += score;
   }
   cout<<"\nID = "<<id<<" Avarage Score = "<<(double)(studentTotal/6.0);
   classTotal += studentTotal;
   numStu++;
   ifs>>id;
  }
  cout<<"\nClass Avarage = "<<(double)(classTotal/(double)numStu);
  cout<<"\nGOOD BYE";
  ifs.close();
}else{
  cout<<"\nFile opening error !!! ";
  exit(0);
}
}

int main(){
string file;
cout<<"\nEnter filename for process the Quiz data ";
cin>>file;
readAndWrite(file);
readAndProcess(file);

return 0;
}

Add a comment
Know the answer?
Add Answer to:
In this assignment, you will write a program in C++ which uses files and nested loops...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CIS 1111 Programming Files

    Description:In this assignment, you will write a program in C++ that uses files and nested loops to create a file for real estate agents home sales and then read the sales from the file and calculates the average sale for each agent. Each agent sold 4 homes.  Use a nested loop to write each agent’s sales to a file. Then read the data from the file in order to display the agent’s average sale and the average for all sales....

  • The following C++ program is a slightly modified version of Program 5.20 from Gary J. Bronson...

    The following C++ program is a slightly modified version of Program 5.20 from Gary J. Bronson textbook. In this program, you compute and display the average of three grades of each student for a class of four students. You need to modify the code to compute and display the class average as well as the standard deviation. Your code changes are as follows: 1. The variable “double grade” should be replaced by a two-dimensional array variable “double grade[NUMSTUDENTS][NUMGRADES].” Also replace...

  • C++ Write a program to calculate final grade in this class, for the scores given below...

    C++ Write a program to calculate final grade in this class, for the scores given below . Remember to exclude one lowest quiz score out of the 4 while initializing the array. Display final numeric score and letter grade. Use standard include <iostream> Implementation: Use arrays for quizzes, labs, projects and exams. Follow Sample Output for formatting. May use initialization lists for arrays. Weight distribution is as follows: Labs: 15% Projects: 20% Quizzes: 20% Exams: 25% Final Project: 20% Display...

  • Create a C++ program to calculate grades as well as descriptive statistics for a set of...

    Create a C++ program to calculate grades as well as descriptive statistics for a set of test scores. The test scores can be entered via a user prompt or through an external file. For each student, you need to provide a student name (string) and a test score (float). The program will do the followings: Assign a grade (A, B, C, D, or F) based on a student’s test score. Display the grade roster as shown below: Name      Test Score    ...

  • Write a program that uses nested loops to collect data and calculate the average rainfall over...

    Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should prompt the user for the number of years. Be sure to ask for each months average rainfall for each year. The outer loop will iterate once for each year while the inner loop will iterate 12 times(one time per month) prompting for the months average rainfall on each iteration. Display the number of months, the total inches...

  • Write them in python IDLE ***** 5. Average Rainfall Write a program that uses nested loops...

    Write them in python IDLE ***** 5. Average Rainfall Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations,...

  • Design a program that uses nested loops to collect data and calculate the average rainfall over...

    Design a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterte twelve times, onece for each month. Each itertion of the inner loop will ask the user for the inches of rainfall for tht month. After all iterations, the program should display the number of months, the...

  • Average Rainfall Design a program that uses nested loops to collect data and calculate the average...

    Average Rainfall Design a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number of...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • This is a C++ Program, I need the program broken up into Student.h, Student.cpp, GradeBook.h, GradeBook.cpp,...

    This is a C++ Program, I need the program broken up into Student.h, Student.cpp, GradeBook.h, GradeBook.cpp, and Main.cpp 1. The system must be able to manage multiple students (max of 5) and their grades for assignments from three different assignment groups: homework (total of 5), quizzes (total (total of 2) of 4), and exams 2. For each student record, the user should be able to change the grade for any assignment These grades should be accessible to the gradebook for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT