I have an unfinished program for homework, the main issue is I am not sure how to make a function that returns the average of an array of test scores and homework scores, so I can use those averages to use in my compute_grade function. I would also like some input on ways I can improve my program. I have also commented out what I am trying to do in the program to help others understand it.
Data File:
LAST NAME FIRST NAME BIRTH DATE ID CLASS MAJOR HOMEWORK SCORES EXAM SCORES
Jones Shelia 06 21 1992 3472 FR CS 87 89 65 90 91 93 79 91 94 90
Bain Robert 11 06 1991 1754 SO CS 77 88 99 95 93 86 78 83 95 87
Smith Pamela 07 18 1990 1888 SO CS 87 92 95 92 94 88 89 79 85 89
Hall Sherry 08 07 1993 7640 SR BIO 85 89 75 83 91 95 92 91 81 84
Stokes Jerry 06 25 1990 2311 JR CS 83 89 85 88 91 93 95 85 95 90
Clayton Randy 05 21 1992 9077 JR EE 80 86 95 89 97 83 96 78 83 90
Chancellor Janet 02 28 1991 2341 SO CHEM 87 99 95 88 97 90 85 82 88 94
Adams Mark 04 16 1989 1764 FR CS 96 97 99 91 93 89 98 95 97 96
Hooten Lamar 10 31 1988 8734 SO CS 91 89 77 90 91 90 84 91 85 86
Mintor Lara 10 31 1993 8734 FR MATH 88 85 79 93 92 81 86 95 82 89
|
Prompt:
Problem 2: Structures (struct) (35 Points)
Last Name First Name Birthdate Number Class Major Homework Scores (7) Exam Scores (3)
Define a structure for a student record. (Note that the Homework and Exams are arrays of numbers. There are 7 homework scores and 3 exam scores.)
Declare an array of student records to hold all the data from the file.
Read in the student data from the file to initialize the array of student records.
Write a function that will compute the grade for a given student. (Grade should be one of the members of the struct you define.)
Grade = (average of homework) * 0.40 + (average of exams) * 0.60
(Note: Your program will have to calculate the homework average and the exam average.)
Call the function to calculate the grades for all students.
Write a print function that will print out the following information for a student:
Last Name
First Name
Birthdate
Number
Major
Homework Average (Not each individual homework score)
Exam Average (Not each individual exam score)
Grade
In the remainder of your program, inside a loop, do the following:
Prompt the user to enter the firstname and lastname for a student.
Print out to the screen (you can call the print function) the data corresponding to the student.
TEST CASES: Pamela Smith and Mark Adams
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
struct date
{
unsigned int month;
unsigned int day;
unsigned int year;
};
// Define a structure for a student record. (Note that the Homework and Exams are arrays of numbers.
// There are 7 homework scores and 3 exam scores.)
struct student_record
{
string firstname;
string lastname;
date birthdate;
string year;
string major;
string ID;
double exam_grades[6];
double homework_grades[2];
float grade;
};
// Declare an array of student records to hold all the data from the file.
// Read in the student data from the file to initialize the array of student records.
const int RECORDS = 10;
void compute_grade(student_record &student);
void print_record(student_record student);
int main(void)
{
//Call the function to calculate the grades for all students.
ifstream infile;
student_record student;
string dataline;
int i = 0, k = 0, count = 0;
cout << "Student records" << endl << endl;
cout << "Open the data file." << endl << endl;
infile.open("/Users/adam/desktop/temp/student_records.txt");
if (!infile)
{
cout << "Unable to open the input file. " << endl;
return(1);
}
else
{
cout << "Data file opened." << endl;
}
cout << "Read line one: Column Headings " << endl;
getline(infile,dataline);
// Close the files
infile.close();
cout << endl << endl;
}
// Write a function that will compute the grade for a given student.
// (Grade should be one of the members of the struct you define.)
// Grade = (average of homework) * 0.40 + (average of exams) * 0.60
// Note: Your program will have to calculate the homework average and the exam average.)
void compute_grade(student_record student)
{
cout << "In function compute grade." << endl;
cout << "Exams Average= " << student.exam_grades << endl;
cout << "Homeworks Average= " << student.homework_grades << endl;
student.grade = student.homework_grades * 0.40 + student.exam_grades * 0.60;
cout << "Grade = " << student.grade << endl;
return;
}
// Write a print function that will print out the following information for a student:
void print_record(student_record student)
{
cout << student.lastname << " " << student.firstname << endl;
cout << "Date of birth "
<< setw(3) << student.birthdate.month
<< setw(3) << student.birthdate.day
<< setw(5)<< student.birthdate.year
<< endl;
cout << "ID: " << student.ID << endl;
cout << "Exam Average: " << student.exam_grades << endl;
cout << "Homework Average: " << student.homework_grades << endl;
cout << "Grade: " << student.grade << endl;
return;
}
//In the remainder of your program, inside a loop, do the following:
//Prompt the user to enter the firstname and lastname for a student.
//Print out to the screen (you can call the print function) the data corresponding to the student.
| |