1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
/*
Name:
Copyright:
Author:
Date: 13/04/14 11:13
Description:
This program will allow 6 people to enter their ratings from 1 to 5
of a product. The program will read the scores and determine the average scores
as well as the highest and lowest scores. The program will implement functions
that will perform the following tasks:
1. Accept the data - function should prompt the user for score.
Error checking should validate that it is in the range of 1 to 5.
Function should be called for each of the ratings
2. Determine the highest rating - return the highest rating of the values passed
to the function
3. Determine the lowest rating - return the lowest rating of the values passed
to the function
4. Average the scores - display the average score of he values passed to the
function.
*/
#include <iostream>
#include <iomanip>
using namespace std;
const int ARR_SIZE = 6;
double score(double sum, double *arr, int size);
double highestRating(double *arr, int size, double maximum);
double lowestRating(double *arr, int size, double minimum);
double averageScores(double sum, int size);
int main()
{
double min = 6, max = 1, sum = 0;
double ratingSum, highest, lowest, average;
double numArray[ARR_SIZE] = {0};
ratingSum = score(sum, numArray, ARR_SIZE);
highest = highestRating(numArray, ARR_SIZE, max);
lowest = lowestRating(numArray, ARR_SIZE, min);
average = averageScores(ratingSum, ARR_SIZE);
cout << "Highest Rating is: " << highest << endl;
cout << "Lowest Rating is: " << lowest << endl;
cout << "Their average is: " << average << endl;
return 0;
}
double score(double sum, double *arr, int size)
{
for(int count = 1; count <= 6; count++)
{
cout << "Enter the Rating (1-5): " << count << " : ";
double rate;
cin >> rate;
if ((rate >= 1) && (rate <= 5))
{
sum += rate;
arr[count -1] = rate;
}
else
{
cout << "Not between 1 and 5. Try again!\n";
count -= 1;
}
}
return sum;
}
double highestRating(double *arr, int size, double maximum)
{
double max = 0;
for (int i = 0; i < size; i++)
{
if (arr[i] > maximum)
{
maximum = arr[i];
max = arr[i];
}
}
return max;
}
double lowestRating(double *arr, int size, double minimum)
{
double min = 0;
for (int i = 0; i < size; i++)
{
if (arr[i] < minimum)
{
minimum = arr[i];
min = arr[i];
}
}
return min;
}
double averageScores(double sum, int size)
{
double avg = sum / size;
return avg;
}
| |