Ok I have a little problem.I have a pointer in the struct which I don't know how to use as or move the information into an array on the bottom function. Need an explanation. Anyone please help...
// This is the minimum functionality for this exercise
// Notice that I did not perform any user input checking as the emphasis was
// on programming with pointers.
//============================================================================
#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);
double getLowest (StudentInfo* , int);
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
double lowestScore;
//-
// call getClassData function here to capture user input
//
getClassData(numStudents, numQuizzes, numProgExercises);
// 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);
}
// Everything went fine with dynamic memory allocation, proceed with the
// program. Since we did not learn in detail how to pass pointers into
// functions or have functions return pointers, I will implement the logic
// here. 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
//
progExSum += ptrStudentInfo->progExercises[j];
}
// 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
lowestScore = getLowest (ptrStudentInfoArray, numProgExercises);
// 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;
}
//*******************************************************************************
// Function : assignLetterGrade
// Returns : char - representing letter grade for a given score
// Parameters : finalScore (input) student's final score (numeric)
// Description: This function receives the final score as input and then
// assigns the appropriate letter grade to the grade.
//********************************************************************************
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
//********************************************************************************
float calculateFinalScore(float progExercisesAvg,
float quizAvg,
int midTerm,
int finalExam,
int attendanceParticipation)
{
float finalScore = 0.0;
// implement the finalScore calculation here
finalScore = (progExercisesAvg * 0.70) +(quizAvg * 0.10) + (midTerm * 0.05)
+ (finalExam * 0.10) + (attendanceParticipation * 0.05);
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;
}
}
double getLowest (StudentInfo* ptrStudentInfoArray, int numProg)
{
StudentInfo *ptrStudentInfo;
double lowest;
int score[0];
for (int i =0; i < numProg; i++)
{
ptrStudentInfo = &ptrStudentInfoArray[i];
score = ptrStudentInfo->progExercises;
}
lowest = score[0];
for (int count = 1; count < numProg; count++)
{
if (score[count] < lowest)
lowest = score[count];
}
return lowest;
}