In: Computer Science
//draft to work from for Assignment 7 where you compute a student's quiz average, dropping two lowest scores, assuming //they have 2 or more quizzes // a max of 12 quizzes can be taken #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> using namespace std; const int NUM_QUIZZES = 12; //assume 12 quizzes max can be taken int buildQuizArray( int [] ); //you need to write these 5 functions below void printQuizArray( string, int [], int ); double calcQuizAverage( int [], int ); void sortQuizArray( int [], int ); void copyArray( int [], int [], int ); int main() { //this is the driver code in main routine , calling your functions int quizArray[NUM_QUIZZES]; //declare array to hold 12 quizzes int numQuizzes; //will be filled in with number of quizzes from buildQuizArray numQuizzes = buildQuizArray( quizArray ); //call buildQuiz array, numQuizzes is returned from function printQuizArray( "Quiz Scores From QuizArray After My Build Function", quizArray, numQuizzes ); //print it out to make sure it is built correctly //take out when finished cout << fixed << setprecision(2); cout << endl << "Your quiz average is " << calcQuizAverage(quizArray, numQuizzes) << "%" << endl; //call function and get a double returned to print to user printQuizArray( "Quiz Scores From QuizArray after calling calcQuizAverage", quizArray, numQuizzes ); //print array again to make sure you did not change the original array return 0; } //******************************************************************************** int buildQuizArray( int array[] ) { //build the array of quizzes and return the count of entries of the array back to main routine } //************************************************************************* void printQuizArray( string reportTitle, int array[], int numValues ) { //print out the quiz array with appropriate title passed to you in reportTitle....add "/10" to end of each quiz score } //*************************************************************************** double calcQuizAverage( int array[], int numValues ) { //if 2 or less values in array use formula in assignment sheet for 2 or less values to compute average //if more than 2, define another "work" array in this function, call copyArray function below to copy elements to a "work array" from array passed to you, //and then call sortQuizArray function to sort your "work array". After work array has been sorted ascending order, use formula in sheet // to compute the quiz average (which will not factor in the two lowest quiz scores in positions 0 and 1 in array) //return the average computed to the main return } //*********************************************************** void copyArray( int destinationArr[], int sourceArr[], int numValues ) { //copy the data from source array into destination array with a loop // numValues has number of entries in the array //at the bottom of this function print out destinationArr to make sure it is correctly copied, simply call printQuizArray printQuizArray("Here's my copy/work array that is to be sorted: ", destinationArr ,numValues); } //*************************************************************** void sortQuizArray( int array[], int numValues ) { //code selection sort logic here to sort array passed in , BEGIN=0, END=numValues-1 //suggestion: document each line of code here to simulate the selection sort algorithm //at bottom of sort, to make sure table is sorted call print function and verify it is sorted printQuizArray( "Here is my sorted array", array, numValues ); //print it out to make sure it is built correctly,take out when finished }
This was the output screen.
here's the code:
/*draft to work from for Assignment 7 where you compute a
student's quiz average, dropping two lowest scores, assuming
//they have 2 or more quizzes
// a max of 12 quizzes can be taken*/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
const int NUM_QUIZZES = 12; //assume 12 quizzes max can be taken
int buildQuizArray( int [] ); //you need to write these 5
functions below
void printQuizArray( string, int [], int );
double calcQuizAverage( int [], int );
void sortQuizArray( int [], int );
void copyArray( int [], int [], int );
int main()
{ //this is the driver code in main routine , calling your
functions
int quizArray[NUM_QUIZZES]; //declare array to hold 12
quizzes
int numQuizzes; //will be filled in with number of quizzes from
buildQuizArray
numQuizzes = buildQuizArray( quizArray ); //call buildQuiz array, numQuizzes is returned from function
printQuizArray( "Quiz Scores From QuizArray After My Build
Function", quizArray, numQuizzes ); //print it out to make sure it
is built correctly
//take out when finished
cout << fixed << setprecision(2);
cout << endl << "Your quiz average is " <<
calcQuizAverage(quizArray, numQuizzes)
<< "%" << endl; //call function and get a double
returned to print to user
printQuizArray( "Quiz Scores From QuizArray after calling
calcQuizAverage", quizArray, numQuizzes );
//print array again to make sure you did not change the original
array
return 0;
}
//********************************************************************************
int buildQuizArray( int array[] )
{
array[NUM_QUIZZES]={-1};
int count=0;
cout<<"Either enter scores. Enter -1 when
done."<<endl;//build the array of quizzes and return the
count of entries of the array back to main routine
for(int i=0;i<NUM_QUIZZES;i++){
int n;
cin>>n;
array[i]=n;
if(n<0) break;
}
for(int i=0;i<NUM_QUIZZES;i++){
if(array[i]>=0) count++;
else break;
}
return count;
}
//*************************************************************************
void printQuizArray( string reportTitle, int array[], int numValues
)
{
cout<<reportTitle<<endl;
for(int i=0;i<numValues;i++){
cout<<"Score of quiz "<<i+1<<":
"<<array[i]<<"/10"<<endl;
}//print out the quiz array with appropriate title passed to you in
reportTitle....add "/10" to end of each quiz score
}
//***************************************************************************
double calcQuizAverage( int array[], int numValues )
{
double averageScore;
int sum=0;
if(numValues<=2){
for(int i=0;i<numValues;i++){
sum+=array[i];
}
averageScore=(sum/numValues)*10;
}//if 2 or less values in array use formula in assignment sheet for
2 or less values to compute average
else{
int work[numValues];
copyArray(work,array,numValues);
sortQuizArray(work,numValues);
for(int i=2;i<numValues;i++){
sum+=work[i];
}
averageScore=(sum/(numValues-2))*10;
}
//if more than 2, define another "work" array in this function,
call copyArray function below to copy elements to a "work array"
from array passed to you,
//and then call sortQuizArray function to sort your "work array".
After work array has been sorted ascending order, use formula in
sheet
// to compute the quiz average (which will not factor in the two
lowest quiz scores in positions 0 and 1 in array)
return averageScore;//return the average computed to the main
return
}
//***********************************************************
void copyArray( int destinationArr[], int sourceArr[], int
numValues )
{
for(int i=0;i<numValues;i++){
destinationArr[i]=sourceArr[i];
}//copy the data from source array into destination array with a
loop
// numValues has number of entries in the array
//at the bottom of this function print out destinationArr to
make sure it is correctly copied, simply call printQuizArray
printQuizArray("Here's my copy/work array that is to be sorted: ",
destinationArr ,numValues);
}
//***************************************************************
void sortQuizArray( int array[], int numValues )
{
int i, j, min_idx;
for (i = 0; i < numValues-1; i++)
{
min_idx = i;
for (j = i+1; j < numValues; j++) {
if (array[j] < array[min_idx])
min_idx = j;
}
int temp = array[min_idx];
array[min_idx] = array[i];
array[i] = temp;
} //code selection sort logic here to sort array passed in ,
BEGIN=0, END=numValues-1
//suggestion: document each line of code here to simulate the
selection sort algorithm
//at bottom of sort, to make sure table is sorted call print
function and verify it is sorted
printQuizArray( "Here is my sorted array", array, numValues );
//print it out to make sure it is built correctly,take out when
finished
}