Hi,
I am trying to complete this application. just using a simple Array. I have most of the instructions down and included some additional stuff I thought was practical; it seems to work up to a point. I will continue to explain.
These are the instructions:
13.13 ( Cafeteria Survey Application) Twenty students were asked to rate, on a scale of 1-10, the quality of food in the school cafeteria, with 1 being "awful" and 10 being "excellent". Your application should present the user with a menu of options. Place the twenty responses into an int array and determine the frequency of each rating. Display the frequencies as a histogram. Recall that a histogram (also known as a bar chart) is a chart where numeric values are displayed as bars. In such a chart, longer bars represent larger numeric values. One simple way to display numeric data graphically is with a histogram that shows each value as a bar of asterisks. Figure 13.31 in your text demonstrates the completed application.
1. Opening the template source code file. Open the CafeteriaSurvey.cpp file in your text editor or IDE.
2. Creating an array of the possible ratings. After line 13, define int constant SIZE and initialize its value to 11. On the next line, define int array responses containing SIZE elements and initialize its values to 0.
3. Displaying an error message when the user input is out of range. In line 26, replace the condition of the if part of the if...else statement with an expression that evaluates to true if the value that the user enters at the keyboard is not a valid rating.
4. Completing the else part of the if....else statement. The body of the else part of the if...else statement executes when the user enters a valid rating. In lines 34-35, increment responseCounter to indicate that another valid response has been entered. Then increment the elements of the responses array at the index at the index corresponding to the value if the input. For example, if the user enters the value 3, the application should increment responses[3].
5. Displaying the histogram. You will now display the results in the form of a histogram. Line 44 begins a for statement. This statement loops once for each rating, displaying the rating in line 46 (followed by a tab character) and a newline character in line 48. You will add the number of stars to be displayed to the right of each rating. In line 49, add the header of a for statement that loops from 1 to the number of votes for the current rating (stored in responses). Use line 48 for a comment describing the statement. In line 50, add the left brace to begin the for statement's body. In line 51, add an asterisk to the output. Because this for statement will loop the same number of times as there are votes for the current rating, the proper number of asterisks will be displayed. In line 53, add the right brace to the end of the for statement's body. Follow the brace with a comment indicating the end of of the for statement.
This is my code:
// Date - 12/06/2011
// this program will calculate food ratings index
#include <iostream>
#include <iomanip>
using namespace std;
//void stars(int ratings)
void main()
{
int const SIZE = 5;
double responses[SIZE] = {0};
double ratingTotal = 0,
ratingAverage = 0,
ratingHigh = 0,
ratingLow = 0;
int i = 0;
system("color 1F");
cout<<"\n\t\t************************************"
<<"\n\t\t* Cafeteria Survey Application *"
<<"\n\t\t************************************"
<< endl
<<"\n\t\tEnter a rating 1= Awful & 10 = Excellent"
<<endl
<<endl;
for(int index = 0; index < SIZE; index++)
{
cout<<"\nHow do you rate the food!"
<<"\nEnter a number between 1-10 ( -1 to exit) ";
cin >> responses[index];
}
// Dispaly the contents of the array
cout <<"\nThe contents of the ratings index:\n" <<endl;
for(int index = 0; index <= SIZE; index++)
{
for(i = 0; i <= SIZE; i++)
cout<< responses[index] <<endl; [B]<------ getting to many responses and[/B]
}
// Calculate and display the average
for(int index = 0; index < SIZE; index++)
{
ratingTotal += responses[index];
}
ratingAverage = ratingTotal /SIZE;
cout<< "\nThe ratings average is: " << ratingAverage;
// Find and display the highest test grade
ratingHigh = responses[0];
for(int index = 0; index < SIZE; index++)
{
if(responses[index] > ratingHigh)
{
ratingHigh = responses[index];
}
}
cout << "\nThe highest rating is: " << ratingHigh;
// Find and display the lowest test grade for(int index = 0; index < SIZE; index++)
ratingLow = responses[0];
for(int index = 0; index < SIZE; index++)
{
if(responses[index] < ratingLow)
{
ratingLow = responses[index];
}
}
cout << "\nThe lowest rating is: " << ratingLow;
cout << endl << endl;
return;
}
//void stars(int number) [B]<-- I want to incorporate this function later as a histogram[/B]
//{
//for ( ; ratings > 0; ratings-- ) // first line of histogram
// {
//cout << '*';
// }
// cout << endl;
//return;
//}