I have been working on my last homework assignment and its due tonight.
I have a few issue i need to work out with it. Fist off the output is returning in reverse order in my ending output loop. Such as values entered as 78 87 98 return as 98 87 78.
Secondly i am having trouble figuring out the last two function i need.
I need a function to determine the min value in an array (array "Avgs")and return it as well as a function to return the max value of the same array.
I am still working to figure it out but time is running out so any help is appreciated!
/* Beauzel
Assignment - Program 3 - C++
Due date - 10/17/14 */
#include "stdafx.h"
#include <iostream> // Allows program to perform input and output.
#include <iomanip> // Used to manipulate data values using setprecision to 2 decimals
usingnamespace std; // allows using cin and cout rather then std::cin and std::cout.
/* Function and template preloading*/
char printLetterGrade(double AvgGrade);
template <class Avg>
Avg Average(Avg Grade1,Avg Grade2,Avg Grade3);
int main()
{
/*Variable declaration*/
int Students;
double LtrGrd;
double Grade1;
double Grade2;
double Grade3;
double AvgValue = 0;
int StudentNumber = 1;
int x = 0;
int y = 0;
/*Requests User data of how many students using an If statement to request new data if input is over the limit of 30*/
cout << "Enter the number of students in class: ";
cin >> Students;
if (Students > 30)
{
cout << "Maximum number of students allowed is 30.\n";
cout << "Enter the number of students in class: ";
cin >> Students;
}
/*Initializing maximum array sizes based on parameters of maximum 30 students*/
int Grades[90];
double Avgs[30];
/*Input Loop - Loop used to gather requested data from user and stores values in array "Grade".
Loop uses input variables to calculate values for array "Avgs"*/
for (int n = 1; n <= Students; n++){
cout << "Please enter three numeric grades for student number " << StudentNumber++ << ". ";
cin >> Grade1;
Grades[x++] = Grade1;
cin >> Grade2;
Grades[x++] = Grade2;
cin >> Grade3;
Grades[x++] = Grade3;
Avgs[y++] = Average(Grade1, Grade2, Grade3);
cout << endl;
}
/*output Look - Used to return data from arrays in structured forum that is readable by user.
Loop uses if statements to create Visual structure of output.*/
x = 0 ;
y = 0 ;
for (int n = 1; n <= Students; n++){
if (n <= 1){
cout << "StudentNumber\t""Grade1\t""Grade2\t""Grade3\t""Average\t""LetterGrade\n";
cout << "===========================================================" << endl;
}
cout << "\t" << n << "\t" << Grades[x++] << "\t" << Grades[x++] << "\t" << Grades[x++] << "\t" << setprecision(2) << fixed << (LtrGrd = Avgs[y++]) << '\t';
cout << printLetterGrade(LtrGrd) << '\t' << endl;
if (n == Students)
cout << "===========================================================" << endl;
}
/*Calculates Class average using array "Avgs"*/
y = 0;
cout << "Processed: " << Students << " Students." << endl;
for (int n = 0; n < Students; n++){
AvgValue += Avgs[y++];
}
cout << "Class Average: " << setprecision(2) << fixed << (AvgValue / 3) << endl ; //output total class average with precision value of 2 decimals
cout << "Class highest average: " << endl; // Space held for max function.
cout << "Class least average: " << endl; // Space held for min function.
system("PAUSE");
return 0;
exit(1);
}
/*Function template to take input grades per student from input loop and average the values.
Value is returned and stored in "Avgs" array for use in min/max function and output loop*/
template <class Avg>
Avg Average(Avg Grade1 ,Avg Grade2 ,Avg Grade3){
Avg GradeAvg = ((Grade1 + Grade2 + Grade3) / 3);
return GradeAvg;
}
/*Function to take average of grade input per student from input loop and determines
the corresponding letter grade. Function returned in output loop*/
char printLetterGrade(double AvgGrade){
int LetterGrade;
if (AvgGrade >= 90) // Grades 90 and above as "A"
LetterGrade = 'A';
elseif (AvgGrade >= 80) // Grades 80-89 as "B"
LetterGrade = 'B';
elseif (AvgGrade >= 70) // Grades 70-79 as "C"
LetterGrade = 'C';
elseif (AvgGrade >= 60) // Grades 60-69 as "D"
LetterGrade = 'D';
else // Grades 59 and less as "F"
LetterGrade = 'F';
return LetterGrade;
}
float min(float* ary){
float min = 100;//Init to max value
for (int i = 0; i < SIZE; i++)
if (ary[i] < min)
min = ary[i];
return min;
}
float max(float* ary){
float max = 0;//Init to min value
for (int i = 0; i < SIZE; i++)
if (ary[i] > max)
max = ary[i];
return max;
}
float avg(float* ary){
float avg = 0;
for (int i = 0; i < SIZE; i++)
avg += ary[i];
avg = avg / SIZE;
return avg;
}
Thank you very much! I was able to make the needed adjustments to get everything working and i learned a lot from your "template there".
Much appreciated!