OBJECTIVES
- Read-in command line arguments 2. Read a file 3. Loop through an array 4. Create an array of struct types 5. Read and Write to a file
Write code to complete the problem below. To receive credit for your code, you will need to paste your solution into Moodles autograder (CodeRunner). Your code needs to compile and pass the given test cases in order to receive points. Note that the autograder submissions are divided into separate parts, so that you will be required to test the individual functions prior to testing the entire program. You must also zip your code and attach it to this weeks Dropbox via Moodle.
P roblem
Overview: In this question, you will write a program that:
- Reads a .csv file with up to 10 lines and 5 columns containing information on student grades.
- Stores the information in an array of structs.
- Writes the lines into the output .csv file where the overall student grade is an A.
- Prints the content of the entire array.
Specifics:
Create an array that holds the studentData struct objects. Use the following struct declaration:
struct studentData { string studentName; int homework; int recitation; int quiz; int exam; double average;
};
2A. Write a function named addStudent:
- The addStudentData function has the following declaration:
// length: Number of items currently stored in the array void addStudentData(studentData students[], string studentName, int homework, int recitation, int quiz, int exam, int length);
- Instantiate a struct and store the studentName, homework, recitation, quiz, and exam values in it.
- Assume equal weights amongst everything that needs to be graded. Take the average of the homework, recitation, quiz, and exam for each respective student and store it in the struct.
- student.average = (homework + recitation + quiz + exam) / 4;
- Add the struct to the students
2B. Write a function named calcLetter:
- The calcLetter function has the following declaration:
char calcLetter(double avg);
- Write IF-ELSE conditions to assign letter grades based on the following a. >90 = A
- 80-89.9 = B
- 70-79.9 = C
- 60-69.9 = D
- <60 = F
- Return the letter grade.
2C. Write a function named printList:
- The printList function has the following signature:
// length: Number of items in the array
void printList(const studentData students[], int length);
- Loop through the students
- Print out each element of the student array in the following format.
<student_name> earned <student_average> which is a(n) <letter_grade> cout << students.studentName << earned << students.average << which is a(n): << calcLetter(students[index].average) << endl; Example:
John Doe earned 95 which is an A
2D. Write a complete program which includes the following: I. The struct and functions listed above.
- A main() function defined as below:
- Your main() should handle four command line arguments
- the name of the input .csv file
- the name of the output .csv file
- a low bound letter grade
- an upper bound letter grade
- Input and output files need to be stored in the same directory as your program.
- Read from the input file, csv:
- Each line of the file can be read using getline
- Parse each line using stringstream and convert each entry into its appropriate data type. Keep in mind that you must use the exact data types that are specified in the struct. (Hint: If needed, use stoi, stof functions to convert from strings to numbers)
- Call addStudentData function to update the students
- Call the printList function after the array has been filled with data.
- Writing out to external files in C++ can be done very similarly as reading in. Instead of using an object of the ifstream class, as is done with input streams, use an object of the ofstream A csv stands for comma separated values. Write into output .csv file:
- Write the <student_name>, <student_average>, <letter_grade> of the students whose <letter_grade> is >= lower_bound and <letter_grade> <= upper_bound (read from command line) into the output .csv
- You should not sort them while writing to output file. The relative order will be same as input file. Only those students with overall letter grade within the bounds should be written to the output file.
- Make sure your program closes the output file when it is finished writing.
Check next page for sample input and output.
Sample Input and Output:
Test case 1:
File Contents: students.csv
Tim Thomas, 90, 92, 95, 100
Bob Brown, 80, 90, 88, 81
Jenny Jackson, 69, 79, 90, 73
Samantha Smith, 65, 57, 72, 59
Ralph Rogers, 55, 59, 62, 48
Your print Output:
Tim Thomas earned 94.25 which is an A
Bob Brown earned 84.75 which is an B
Jenny Jackson earned 77.75 which is an C
Samantha Smith earned 63.25 which is an D
Ralph Rogers earned 56 which is an F
Reviews
There are no reviews yet.