// Exercise 09 
// Final grade calculation for a class
============================================================================
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

const int MAX_NAME_SIZE = 25;

// Structure to hold StudentInfo
struct StudentInfo
{
       int    studentID;
       char   name[MAX_NAME_SIZE];       
       int    *progExercises;  // a pointer to an array of prog exercise scores   
       int    *quizzes;         // a pointer to an array of quiz score
       int    midTerm;         
       int    finalExam;
       int    attendanceParticipation;
       float  finalScore;      // Course grade calculated       
       char   finalGrade;      // Letter grade for the course
};

// function prototypes
void  getClassData (int &, int &, int &);
void  displayStudents(StudentInfo* , int, int, int); 
float calculateFinalScore(float, float, int, int, int);
char  assignLetterGrade(float);

int main(int argc, char *argv[])
{
    StudentInfo *ptrStudentInfoArray;    // a pointer to an array that 
                                         // will hold student structs

    int         numStudents;             // to hold # of students
    int         numQuizzes;              // to hold # of quizzes
    int         numProgExercises;        // to hold # of prog exercises



    // call getClassData function here to capture user input


    // Dynamically allocate an array of StudentInfo for the number
    // of students the user specified. This is an array of structures
    ptrStudentInfoArray = new StudentInfo[numStudents];

    // if something goes wrong with the dynamic allocation, notify the user
    // and terminate the program
    if (!ptrStudentInfoArray)
    {
        cout << "Fatal Error! Could not allocate memory for the array." << endl;                     
        cout << "Terminating the program." << endl;
        system("PAUSE");
        exit(0);
    }

    // Normally this would have been implemented in a function
     StudentInfo *ptrStudentInfo;  // pointer to the structure address 
     int progExSum;                // sum of programming exercises
     int quizSum;                  // sum of all quizzes
     float progExAvg, quizAvg;     // averages for programming exercises and
                                   // quizzes

     for (int i =0; i < numStudents; i++)
     {
         ptrStudentInfo = &ptrStudentInfoArray[i];

         cout << "Enter student#" << i+1 << " name: ";
         cin  >> ptrStudentInfo->name;

         cout << "Enter student#" << i+1 << " ID: ";
         cin  >> ptrStudentInfo->studentID;

         progExSum = 0;   // initialize the sum of programming exercises for
                          // each student
         progExAvg = 0;   // initialize the average of programming exercises for
                          // each student

         // now we will allocate an array to hold programming exercise scores
         ptrStudentInfo->progExercises = new int[numProgExercises];
         for (int j=0; j<numProgExercises; j++)
         {
             cout << "Enter programming exercise score #" << j+1 << ": ";
             cin >> ptrStudentInfo->progExercises[j];

             // 
             // Add a line of code here to keep a running total (sum) of 
             // programming exercises
             //
         }

         // call a function to drop the lowest programming exercise
         // Note: you will need to implement this function, as there is no
         //       prototype for it in the source code. This is one of the
         //       "extra credit" functions for this assignment

         // add a line of code to compute a programming exercise average


         quizSum = 0;  // initialize the sum of quizzes for each student
         quizAvg = 0;  // initialize the averages for each student

         // now allocate an array to hold quiz scores
         ptrStudentInfo->quizzes = new int[numQuizzes];
         for (int j=0; j<numQuizzes; j++)
         {
             cout << "Enter quiz score #" << j+1 << ": ";
             cin >> ptrStudentInfo->quizzes[j];

             // 
             // Add a line of code to keep a running total (sum)
             // programming exercises
         }

         // Add a line of code to compute quizAverage

         cout << "Enter MidTerm Score: " ;
         cin  >> ptrStudentInfo->midTerm;

         cout << "Enter Final Exam Score: " ;
         cin  >> ptrStudentInfo->finalExam;

         cout << "Enter Attendance and Participation Score: " ;
         cin  >> ptrStudentInfo->attendanceParticipation;

         // Call a function here to calculate the final score

         // Call a function here to assign letter grade based on the final score


     }  // end of for

    cout << "=======================================================" << endl;                                 
    displayStudents(ptrStudentInfoArray, numStudents, numProgExercises, numQuizzes);

    // delete dynamically allocated memory
    // Notice that I grab array elements through their address and then delete
    // individual dynamically allocated arrays in each structure.
     for (int i =0; i < numStudents; i++)
     {
         ptrStudentInfo = &ptrStudentInfoArray[i];
         delete ptrStudentInfo->progExercises;
         delete ptrStudentInfo->quizzes;      
     }

     // once I am done with each structure, I delete the array
     delete []ptrStudentInfoArray;

    system("PAUSE");
    return EXIT_SUCCESS;
}

char assignLetterGrade(float finalScore)
{
     char letterGrade;

     // implement the logic here to assign the letterGrade based on the finalScore
     // that was passed in.

     return letterGrade;
}

//*******************************************************************************
// Function   : calculateFinalScore
// Returns    : float
// Parameters : progExercisesAvg (input) average score of programming exercises
//              quizAvg (input) average of quizz scores
//              midTerm (input) midTerm exam grade
//              finalExam (input) final exam grade
//              attendanceParticipation (input) - attendance and class 
//              participation grade 
// Description: This function claculates the final score by using the following
//              formula:

//              (progExercisesAvg * 0.70) + 
//                  (quizAvg * 0.10) +
//                  (midTerm * 0.05) +
//                  (finalExam * 0.10) +
//                  (attendanceParticipation * 0.05)
//********************************************************************************
float calculateFinalScore(float progExercisesAvg, 
                            float quizAvg,
                            int    midTerm,
                            int    finalExam,
                            int    attendanceParticipation)
{
    float finalScore = 0.0;

    // implement the finalScore calculation here

    return finalScore;
}

//*******************************************************************************
// Function   : getClassData
// Returns    : void
// Parameters : numStuds (output) number of students
//              numPEs (output) number of programming exercises
//              numQs (output) number of quizzes
// Description: This function prompts the user for number of students, 
//              number of programming exercises, and number of quizzes and
//              returns these through reference parameters
//********************************************************************************
void getClassData(int &numStuds, int &numPEs, int &numQs)
{
    cout << "How many students are in your class?";
    cin >> numStuds;

    cout << "How many programming exercises did they have?";
    cin >> numPEs;

    cout << "How many quizzes did they have?";
    cin >> numQs;                                         
}                                         


//*******************************************************************************
// Function   : displayStudents
// Returns    : void
// Parameters : ptrStudentInfoArray - pointer to an array of student structures
//              numStuds (input) how many students in the array
//              numPEs (input) number of programming exercises
//              numQs (input) number of quizzes
// Description: This function displays the student array 
//********************************************************************************
void displayStudents(StudentInfo* ptrStudentInfoArray, 
                    int          numStuds, 
                    int          numPEs, 
                    int          numQs)
{
     StudentInfo *ptrStudentInfo;  // pointer to the structure address 

      cout << "===============================================================" << endl;
     cout << "SID \t\t Name\t\tFinal Score\tLetter Grade" << endl;
     cout << "===============================================================" << endl;
     for (int i =0; i < numStuds; i++)
     {
         ptrStudentInfo = &ptrStudentInfoArray[i];


         cout << setprecision(2) << fixed;
         cout << ptrStudentInfo->studentID << "\t\t"
              << ptrStudentInfo->name << "\t\t" 
              << ptrStudentInfo->finalScore << "\t\t"
              << ptrStudentInfo->finalGrade << endl;         
     }   
}

Recommended Answers

All 3 Replies

Help me to find the better solution for this code please

please help me

Can't. Can't read your post. Please re-post with a better description of problem and use code tags then maybe we can help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.