Question

In: Computer Science

Retrieve program Grades.cpp and the data file graderoll.dat from the Lab 12 folder. The code is...

Retrieve program Grades.cpp and the data file graderoll.dat from the Lab 12 folder. The code is as follows:

______________________________________________________________________________

#include // FILL IN DIRECTIVE FOR FILES

#include <iostream>

#include <iomanip>

using namespace std;

// This program reads records from a file. The file contains the

// following: student's name, two test grades and final exam grade.

// It then prints this information to the screen.

const int NAMESIZE = 15;

const int MAXRECORDS = 50;

struct Grades // declares a structure

{

​char name[NAMESIZE + 1];

​int test1;

​int test2;

​int final;

};

typedef Grades gradeType[MAXRECORDS];

// This makes gradeType a data type

// that holds MAXRECORDS

// Grades structures.

// FIll IN THE CODE FOR THE PROTOTYPE OF THE FUNCTION ReadIt

// WHERE THE FIRST ARGUMENT IS AN INPUT FILE, THE SECOND IS THE

// ARRAY OF RECORDS, AND THE THIRD WILL HOLD THE NUMBER OF RECORDS

// CURRENTLY IN THE ARRAY.

int main()

{

​ ifstream indata;

​ indata.open("graderoll.dat");

​ int numRecord; // number of records read in

​ gradeType studentRecord;

​if(!indata)

​{

​​cout << "Error opening file. \n";

​​cout << "It may not exist where indicated" << endl;

​​return 1;

​}

​// FILL IN THE CODE TO CALL THE FUNCTION ReadIt.

​// output the information

for (int count = 0; count < numRecord; count++)

​{

​ cout << studentRecord[count].name << setw(10)

​​ << studentRecord[count].test1

​​ << setw(10) << studentRecord[count].test2;

​ cout << setw(10) << studentRecord[count].final << endl;

​}

​return 0;

}

//**************************************************************

//​​​​​readIt

//

// task:​ This procedure reads records into an array of

// records from an input file and keeps track of the

//​​ total number of records

// data in: data file containing information to be placed in

// the array

// data out: an array of records and the number of records

//

//**************************************************************

void readIt(// FILL IN THE CODE FOR THE FORMAL PARAMETERS AND THEIR

// DATA TYPES.

​​ // inData, gradeRec and total are the formal parameters

​​ // total is passed by reference)

{

total = 0;

inData.get(gradeRec[total].name, NAMESIZE);

while (inData)

{

// FILL IN THE CODE TO READ test1

// FILL IN THE CODE TO READ test2

// FILL IN THE CODE TO READ final

​ ​

​ total++; // add one to total

// FILL IN THE CODE TO CONSUME THE END OF LINE

// FILL IN THE CODE TO READ name

}

}

______________________________________________________________________________

Exercise 1: Complete the program by filling in the code (areas in bold). This problem requires that you study very carefully the code and the data file already written to prepare you to complete the program. Notice that in the data file the names occupy no more than 15 characters. Why?

Sample Run:

Exercise 2: Add another field called letter to the record which is a character that holds the letter grade of the student. This is based on the average of the grades as follows: test1 and test2 are each worth 30% of the grade while final is worth 40% of the grade. The letter grade is based on a 10 point spread. The code will have to be expanded to find the average.

90 – 100 A

80 – 89 B

70 – 79 C

60 – 69 D

0 – 59 F

Sample Run:

Solutions

Expert Solution

Working code implemented in C++ and appropriate comments provided for better understanding:

Source code for Grades.cpp:

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

// This program reads records from a file. The file contains the
// following: student’s name, two test grades and final exam grade.
// It then prints this information to the screen.

// Michael Steele

const int NAMESIZE = 15;
const int MAXRECORDS = 50;

struct Grades   // declares a structure
{
   char name[NAMESIZE + 1];
   int test1;
   int test2;
   int final;
   char letter;
};

typedef Grades gradeType;
// This makes gradeType a data type
// that holds MAXRECORDS
// Grades structures.

// FIll IN THE CODE FOR THE PROTOTYPE OF THE FUNCTION ReadIt
// WHERE THE FIRST ARGUMENT IS AN INPUT FILE, THE SECOND IS THE
// ARRAY OF RECORDS, AND THE THIRD WILL HOLD THE NUMBER OF RECORDS
// CURRENTLY IN THE ARRAY.
void readIt(ifstream &,gradeType *, int &);
char letter(int test1,int test2, int final);


int main()
{
   ifstream indata;

   indata.open("graderoll.dat");

   int numRecord;   // number of records read in

   gradeType studentRecord[MAXRECORDS];

   if (!indata)
   {
       cout << "Error opening file. \n";
       cout << "It may not exist where indicated" << endl;

       return 1;
   }

   readIt(indata,studentRecord,numRecord);

   // output the information
   for (int count = 0; count < numRecord; count++)
   {
       cout << studentRecord[count].name << setw(10)
           << studentRecord[count].test1
           << setw(10) << studentRecord[count].test2;
       cout << setw(10) << studentRecord[count].final ;
       cout << " " << "Final grade " << studentRecord[count].letter << endl;
   }

   return 0;
}

//**************************************************************
//   readIt
//
//   task:   This procedure reads records into an array of
//   records from an input file and keeps track of the
//   total number of records
//   data in: data file containing information to be placed in
//   the array
//   data out: an array of records and the number of records
//
//**************************************************************

void readIt(ifstream &indata, gradeType *gradeRec ,int &total)// FILL IN THE CODE FOR THE FORMAL PARAMETERS AND THEIR
   // DATA   TYPES.
   // inData, gradeRec and total are the formal parameters
   // total is passed by reference)
{
   total = 0;

   indata.get(gradeRec[total].name, NAMESIZE);

   while (indata)
   {
       // FILL IN THE CODE TO READ test1
       indata >> gradeRec[total].test1;

       // FILL IN THE CODE TO READ test2
       indata >> gradeRec[total].test2;

       // FILL IN THE CODE TO READ final
       indata >> gradeRec[total].final;
gradeRec[total].letter = letter(gradeRec[total].test1,gradeRec[total].test2,gradeRec[total].final);
       total++;   // add one to total

       // FILL IN THE CODE TO CONSUME THE END OF LINE
char eat;
       indata.get(eat);

       // FILL IN THE CODE TO READ name
       indata.get(gradeRec[total].name, NAMESIZE);


   }

}
char letter(int test1,int test2, int final)
{
   int avg = 0.3 * test1 + 0.3 * test2 +0.4 * final;
   if(avg >= 90)
       return 'A';
   else if(avg >= 80)
       return 'B';
   else if(avg >= 70)
       return 'C';
   else if(avg >= 60)
       return 'D';
else
return 'F';
}

Sample Output Screenshots:

Hope it helps, if you like the answer give it a thumbs up. Thank you.


Related Solutions

Question 2: Download the Excel data file "Arlington_Homes" from the folder "Data" under "Chapter 12." a)...
Question 2: Download the Excel data file "Arlington_Homes" from the folder "Data" under "Chapter 12." a) read the data file in R. b) using R, answer question 65 (a, b, and c) on page 411 of your book. Run the regression, show the estimates and test. Write what you are testing using a comment in the R program. Question #65. link for page 411 #65 https://imgur.com/s0SgxP3 please show every step for R frmulas Price Sqft Beds Baths Col 840000 2768...
Using the code below from “LStack.h” file, write the code for a main program that takes...
Using the code below from “LStack.h” file, write the code for a main program that takes as input an arithmetic expression. The program outputs whether the expression contains matching grouping symbols. For example, the arithmetic expressions { 25 + ( 3 – 6 ) * 8 } and 7 + 8 * 2 contains matching grouping symbols. However, the expression 5 + { ( 13 + 7 ) / 8 - 2 * 9 does not contain matching grouping symbols....
11.1 Simple Arithmetic Program Using the instructions from Week 1 Lab, create a new folder named...
11.1 Simple Arithmetic Program Using the instructions from Week 1 Lab, create a new folder named Project01. In this folder create a new class named Project01. This class must be in the default package. Make sure that in the comments at the top of the Java program you put your name and today's date using the format for Java comments given in the Week 1 Lab. For this lab, you will write a Java program to prompt the user to...
IN JAVA Lab 10 This program reads times of runners in a race from a file...
IN JAVA Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you created and copy the Main class below into the file. There are 7 parts...
Retrieve Data
Go to www.nist.gov/pml/data/asd.cfm, click on Levels, enter C I in Spectrum, and click Retrieve Data to find the energy levels of the 2s22p3s configuration of C. Then use the selection rules given in Prob. 11.31 to find the wavenumbers and wavelengths of all allowed transitions between the levels of 2s22p3s and the levels of 2s22p2 given in Prob. 11.31. Check your wavelengths by clicking on Lines instead of Levels.In Problem 11.31For the carbon atom, the levels that arise from the...
Create a program that creates a sorted list from a data file. The program will prompt...
Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name ,last...
Go to the Files section and download the AFE_Test file from the Datasets folder. We are...
Go to the Files section and download the AFE_Test file from the Datasets folder. We are interested in a one­tail test described in the following fashion: Ho: u < or = to 200 CFM; H1: u > 200 CFM. At 5% significance level, we can reject the null hypothesis given the sample information in AFE_Test1. we can reject the null hypothesis given the sample information in AFE_Test2. we cannot reject the null hypothesis. we can reject the null hypothesis given...
a) A data is collected from Lab A. Sample mean is 12, SD is 2.4 and...
a) A data is collected from Lab A. Sample mean is 12, SD is 2.4 and sample size if 16. Another set of data from Lab B have a mean of 10 (assuming SDB is also 2.4 and nB=16). If we choose α = 0.05, do you think mean of Lab B is close enough to be considered the “same” as that of Lab A? Why? b) Same as problem 4), except SD of Lab B is SDB=4.4. If we...
Part 1 Ask for a file name. The file should exist in the same folder as...
Part 1 Ask for a file name. The file should exist in the same folder as the program. If using input() to receive the file name do not give it a path, just give it a name like “number1.txt” or something similar (without the quotes when you enter the name). Then ask for test scores (0-100) until the user enters a specific value, in this case, -1. Write each value to the file as it has been entered. Make sure...
The code in this question shows three ways that one can read data from a file...
The code in this question shows three ways that one can read data from a file in a Python program. Answer the following questions: Using the code as an example, explain why Python is a fixed format programming language. Describe what would be output for each of the print statements in the code listing. Given the difference in output, explain the different roles of readLine(), readLines(), and read() functions. with open("rainfall.txt","r") as inFile:    aLine = inFile.readLine() with open("rainfall.txt", "r") as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT