2012年2月20日 星期一

Exercise #2


Exercise #2

Write a complete C++ program that makes use of the student struct and main() shown below. The program should read in the student data from the keyboard, calculate the final grade and print the student data for two (or more) test cases. 

Your program should meet the following requirements:
  • Use the student struct with the indicated members.
  • You should write at least four functions, getStudentData(), checkStudentData(), calculateFinalGrade(), and printStudentData(). Each of these functions should take a student struct argument, passed by reference.
  • Your getStudentData() function should allocate memory dynamically for the last name. This memory should be released in main() before your program ends.
  • checkStudentData() should be called by getStudentData(). It should verify the accuracy of the entered data. The ssn should be 9 numeric digits, the labs grades  must have a value between 0 and 25, the midterm between 0 and ??1, and the final between 0 and ???1. If the entered data is incorrect, you may exit the program or ask the user to re-enter the data.
  • The calculateFinalGrade() function should reflect the policies used to determine points and the final grade for the course. Remember to discard the lowest lab grade, but not the last lab.
  • Your program should produce output similar to that shown on the next page. You must submit your output along with a program listing.
  • Do not include a disk or email your solution.

struct student
{
  char* lastname;
  char ssn[10];
  int  lab_grade[NumLabGrades1];
  int  midterm;
  int  final;
  int total_points;
  char final_grade;
};


int main(void)
{
    student Me, You;
    getStudentData(Me);
    calculateFinalGrade(Me);
   
    getStudentData(You);
    calculateFinalGrade(You);
    printStudentData(Me);
    printStudentData(You);

…        <- something goes here

    return 0;
}


**************************  Sample Run #1  *****************************

Enter the last name => Smith
Enter social security number => 123456789
Enter 9 lab grades (separated by a space) => 25 20 18 20 19 18 16 22 19
Enter midterm grade => 65
Enter final grade => 114
Thanks!

Enter the last name => Nguyen
Enter social security number => 987654321
Enter 9 lab grades (separated by a space) => 19 18 17 16 15 14 13 12 11
Enter midterm grade => 55
Enter final grade => 77
Thanks!

Name: Smith
SSN: 123456789
Lab grades: 25 20 18 20 19 18 16 22 19
Midterm: 65
Final: 114
Total Points: ???        - you figure this out
Final grade: ?         - you figure this out

Name: Nguyen
SSN: 987654321
Lab grades: 19 18 17 16 15 14 13 12 11
Midterm: 55
Final: 77
Total Points: ???     - you figure this out
Final grade: ?         - you figure this out

**************************  Sample Run #2  *****************************

Enter the last name => Doe
Enter social security number => 345678901
Enter #1 lab grades (separated by a space) => 11 12 11 12 11 12 …1
Enter midterm grade => 81
Enter final grade => 88
Thanks!

Invalid midterm 81.  Exiting...

沒有留言:

張貼留言