In: Computer Science
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
# include <iostream>
# include <climits>
# include <string>
# include <iomanip>
# define MAX_SIZE 12
using namespace std;
/***************************************************************
Function: int buildQuizArray ()
Use: This function takes the input from the user and fill it
in the quizArray()
Argument: an integer array : the array holds the scores of
the quizzes.
Returns: integer : denoting the size of the integer array
quizArray()
Note: We have used a while loop which will break when the user
inputs -1.
***************************************************************/
int buildQuizArray (int quizArray[]) {
// initialize the subscript to the beginning of the array
int subscript = 0;
// this variable will hold the quizScore
int quizScore = 0;
cout << "\n";
// take input the quizScore from keyboard
// do this (indefinitely) until -1 is encountered
while (true) {
// prompt the user for the quiz score
// as subscript indicates the index
// for quiz number, we will print (subscript + 1)
cout << "Enter your score for quiz " << subscript + 1 << " (-1 to quit): ";
// take the input from keyboard
cin >> quizScore;
// if -1 is entered, we will return
// break out of the loop
if (quizScore == -1) {
break;
}
// fill the score into the array
quizArray[subscript] = quizScore;
// increment the subscript
subscript += 1;
}
// as the subscript variable contains the
// index of the next block, its value will be equal
// to the size of the array
return subscript;
}
/***************************************************************
Function: void printQuizArray()
Use: Prints the array of scores the student got in quizzes.
Argument: string : a title which says - "Quiz Scores"
integer array : containing the scores of the quizzes
integer : containing the number of elements in the array
Returns: Nothing.
Note: None.
***************************************************************/
void printQuizArray (string title, int quizArray[], int numberOfQuizzes) {
cout << "\n" << title;
cout << "\n------------------";
for (int i = 0; i < numberOfQuizzes; ++i) {
// as the subscript starts from 0,
// while printing, we will add 1 to it
cout << "\nQuiz " << i + 1 << "\t" << quizArray[i] << "/10";
}
cout << "\n\n";
}
/***************************************************************
Function: void copyArray()
Use: Copies the elements of the source array into destination
array.
Argument: integer array : destination array in which copying has
to be done
integer array : source array from which copying has
to be done
integer : containing the number of elements in the array
Returns: Nothing.
Note: None.
***************************************************************/
void copyArray (int destination[], int source[], int numberOfValues) {
// copying the elements of source in destination
for (int i = 0; i < numberOfValues; ++i) {
destination[i] = source[i];
}
}
/***************************************************************
Function: void sortArray()
Use: Sorts the input array given as an argument.
Argument: integer array : whose elements need to be sorted in
ascending order.
integer : containing the number of elements in the array
Returns: Nothing.
Note: We have employed Selection Sort to sort the elements.
***************************************************************/
void sortArray (int array[], int numberOfQuizzes) {
// selection sort
// 1. Pick an element from the start
// 2. find the position of the minimum element
// 3. replace the picked element with the minimum element
// 4. Do this for all the elements
// we will run our loop for numberOfQuizzes - 1 elements
// because, after this, the last element will automatically
// be on its correct position
for (int i = 0; i < numberOfQuizzes - 1; ++i) {
int minIndex = i;
// find the minimum element in the array right to index i
for (int j = i + 1; j < numberOfQuizzes; ++j) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
// swap the minimum element found and the ith element
swap (array[i], array[minIndex]);
}
}
/***************************************************************
Function: double calcQuizAverage()
Use: Calculates the average of the integers given according
to certain conditions(specified in problem statement).
Argument: integer array : whose elements need to be sorted in
ascending order.
integer : containing the number of elements in the array
Returns: double : denoting the average of integers of the array.
Note: The formula used depends on the number of elements in the
array:
1. If the elements are greater than 2, then use:
average = (sumOfScores - sumOfTwoLowestQuizzes) / (10 * (numberOfQuizzes - 2)) * 100
2. If the elements are smaller than or equal to 2, then use:
average = sumOfScores / (10 * numberOfQuizzes) * 100
***************************************************************/
double calcQuizAverage (int quizArray[], int numberOfQuizzes) {
// sum up the scores of all the quizzes
int sumOfScores = 0;
for (int i = 0; i < numberOfQuizzes; ++i) {
sumOfScores += quizArray[i];
}
double average = 0.00;
// check the number of quizzes
// if the numberOfQuizzes are greater than 2
if (numberOfQuizzes > 2) {
// allocate the memory for copy array
int * newArray = new int [12];
// copy the contents to the newArray
copyArray (newArray, quizArray, numberOfQuizzes);
// sort the new array
sortArray (newArray, numberOfQuizzes);
int sumOfTwoLowestQuizzes = newArray[0] + newArray[1];
// using the formula as mentioned in the problem statement
average = (double) (sumOfScores - sumOfTwoLowestQuizzes) / (10 * (numberOfQuizzes - 2));
average *= 100;
} else {
average = (double) sumOfScores / (10 * numberOfQuizzes);
average *= 100;
}
return average;
}
/***************************************************************
Function: int main()
Use: Contains the driver code for testing the program.
Argument: None.
Returns: integer : as a status code.
Note: None.
***************************************************************/
int main (void) {
// declaring an array of integers with size 12
int * quizArray = new int [MAX_SIZE];
// declaring the integer varible
// to hold the number of quizzes completed by student
int numberOfQuizzes = 0;
// calling the buildQuizArray() to get the input
// this function returns the number of quizzes completed by student
numberOfQuizzes = buildQuizArray (quizArray);
// calling the calcQuizAverage() to get the quiz average
double quizAverage = calcQuizAverage (quizArray, numberOfQuizzes);
// we are using fixed and setprecision from #include <iomanip>
// for displaying the double variable upto 2 decimal places
cout << "\nYour quiz average is " << fixed << setprecision(2) << quizAverage << "%\n";
// calling the printQuizArray function to print the
// students' quiz score
printQuizArray ("Quiz Scores", quizArray, numberOfQuizzes);
return 0;
}
Here is the code for version 2 of the code:
# include <iostream>
# include <climits>
# include <string>
# include <iomanip>
# include <cstdlib>
# include <fstream>
# define MAX_SIZE 12
using namespace std;
/***************************************************************
Function: int buildQuizArray ()
Use: This function takes the input from a file named
"quizscores.txt" and fill it in the quizArray()
Argument: an integer array : the array holds the scores of
the quizzes.
Returns: integer : denoting the size of the integer array
quizArray()
Note: We have used a while loop which will run until the data
in the file is not completely read.
***************************************************************/
int buildQuizArray (int quizArray[]) {
// declaration for the input file stream
ifstream infile;
// we need to open the file before we can access its contents
// we will use open () function
// according to the problem statement, opening the file is to be
// done differently for different operating systems
// you may choose accordingly
// the code we are showing here will work in the scenario where
// the text file is present in the same directory as that of the
// cpp file.
infile.open ("quizscores.txt");
// testing the file, that if it has opened correctly
// according to the problem statement, we will use fail () function for that
if (infile.fail ()) {
cout << "the quizscores.txt input file did not open";
exit (-1);
}
// as in this case, we are reading from the file(instead of keyboard)
// we can't use cin object as it is connected to the standard input
// in here, we need to replace cin with "infile" ifstream object
// initialize the subscript to the beginning of the array
int subscript = 0;
// this variable will hold the quizScore
int quizScore = 0;
cout << "\n";
// accoding to the problem statement
// here, we cannot use the -1 input condition to break out of the loop
// because the input file doesn't contain -1
// we need to read the file until there is data in the file
// we can use: infile >> quizScore
// as the condition in the for loop
// until there is data in the file, the input file stream object
// will be valid (or true)
// when there will be no data left, it will become invalid(or false)
while (infile >> quizScore) {
// Note that we have removed the prompt
// and the file reading is put as the condition in the while loop
// fill the score into the array
quizArray[subscript] = quizScore;
// increment the subscript
subscript += 1;
}
// after all the data has been read, we will close the file
infile.close();
// as the subscript variable contains the
// index of the next block, its value will be equal
// to the size of the array
return subscript;
}
/***************************************************************
Function: void printQuizArray()
Use: Prints the array of scores the student got in quizzes.
Argument: string : a title which says - "Quiz Scores"
integer array : containing the scores of the quizzes
integer : containing the number of elements in the array
Returns: Nothing.
Note: None.
***************************************************************/
void printQuizArray (string title, int quizArray[], int numberOfQuizzes) {
cout << "\n" << title;
cout << "\n------------------";
for (int i = 0; i < numberOfQuizzes; ++i) {
// as the subscript starts from 0,
// while printing, we will add 1 to it
cout << "\nQuiz " << i + 1 << "\t" << quizArray[i] << "/10";
}
cout << "\n\n";
}
/***************************************************************
Function: void copyArray()
Use: Copies the elements of the source array into destination
array.
Argument: integer array : destination array in which copying has
to be done
integer array : source array from which copying has
to be done
integer : containing the number of elements in the array
Returns: Nothing.
Note: None.
***************************************************************/
void copyArray (int destination[], int source[], int numberOfValues) {
// copying the elements of source in destination
for (int i = 0; i < numberOfValues; ++i) {
destination[i] = source[i];
}
}
/***************************************************************
Function: void sortArray()
Use: Sorts the input array given as an argument.
Argument: integer array : whose elements need to be sorted in
ascending order.
integer : containing the number of elements in the array
Returns: Nothing.
Note: We have employed Selection Sort to sort the elements.
***************************************************************/
void sortArray (int array[], int numberOfQuizzes) {
// selection sort
// 1. Pick an element from the start
// 2. find the position of the minimum element
// 3. replace the picked element with the minimum element
// 4. Do this for all the elements
// we will run our loop for numberOfQuizzes - 1 elements
// because, after this, the last element will automatically
// be on its correct position
for (int i = 0; i < numberOfQuizzes - 1; ++i) {
int minIndex = i;
// find the minimum element in the array right to index i
for (int j = i + 1; j < numberOfQuizzes; ++j) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
// swap the minimum element found and the ith element
swap (array[i], array[minIndex]);
}
}
/***************************************************************
Function: double calcQuizAverage()
Use: Calculates the average of the integers given according
to certain conditions(specified in problem statement).
Argument: integer array : whose elements need to be sorted in
ascending order.
integer : containing the number of elements in the array
Returns: double : denoting the average of integers of the array.
Note: The formula used depends on the number of elements in the
array:
1. If the elements are greater than 2, then use:
average = (sumOfScores - sumOfTwoLowestQuizzes) / (10 * (numberOfQuizzes - 2)) * 100
2. If the elements are smaller than or equal to 2, then use:
average = sumOfScores / (10 * numberOfQuizzes) * 100
***************************************************************/
double calcQuizAverage (int quizArray[], int numberOfQuizzes) {
// sum up the scores of all the quizzes
int sumOfScores = 0;
for (int i = 0; i < numberOfQuizzes; ++i) {
sumOfScores += quizArray[i];
}
double average = 0.00;
// check the number of quizzes
// if the numberOfQuizzes are greater than 2
if (numberOfQuizzes > 2) {
// allocate the memory for copy array
int * newArray = new int [12];
// copy the contents to the newArray
copyArray (newArray, quizArray, numberOfQuizzes);
// sort the new array
sortArray (newArray, numberOfQuizzes);
int sumOfTwoLowestQuizzes = newArray[0] + newArray[1];
// using the formula as mentioned in the problem statement
average = (double) (sumOfScores - sumOfTwoLowestQuizzes) / (10 * (numberOfQuizzes - 2));
average *= 100;
} else {
average = (double) sumOfScores / (10 * numberOfQuizzes);
average *= 100;
}
return average;
}
/***************************************************************
Function: int main()
Use: Contains the driver code for testing the program.
Argument: None.
Returns: integer : as a status code.
Note: None.
***************************************************************/
int main (void) {
// declaring an array of integers with size 12
int * quizArray = new int [MAX_SIZE];
// declaring the integer varible
// to hold the number of quizzes completed by student
int numberOfQuizzes = 0;
// calling the buildQuizArray() to get the input
// this function returns the number of quizzes completed by student
numberOfQuizzes = buildQuizArray (quizArray);
// calling the calcQuizAverage() to get the quiz average
double quizAverage = calcQuizAverage (quizArray, numberOfQuizzes);
// we are using fixed and setprecision from #include <iomanip>
// for displaying the double variable upto 2 decimal places
cout << "\nYour quiz average is " << fixed << setprecision(2) << quizAverage << "%\n";
// calling the printQuizArray function to print the
// students' quiz score
printQuizArray ("Quiz Scores", quizArray, numberOfQuizzes);
return 0;
}