(Polling) The Internet and the web are enabling more people to network, join a cause, voice opinions, and so on. The presidential candidates in 2008 used the Internet intensively to get out their messages and raise money for their campaigns. In this exercise, you’ll write a simple polling program that allows users to rate five social-consciousness issues from 1 (least important) to 10 (most
important). Pick five causes that are important to you (e.g., political issues, global environ- mental issues). Use a one-dimensional array topics (of type string) to store the five causes. To sum- marize the survey responses, use a 5-row, 10-column two-dimensional array responses (of type int), each row corresponding to an element in the topics array. When the program runs, it should ask the user to rate each issue. Have your friends and family respond to the survey. Then have the program display a summary of the results, including:
a) A tabular report with the five topics down the left side and the 10 ratings across the top, listing in each column the number of ratings received for each topic.
b) To the right of each row, show the average of the ratings for that issue.
c) Which issue received the highest point total? Display both the issue and the point total.
d) Which issue received the lowest point total? Display both the issue and the point total.
```
// Analyze polling data on various issues.
#include
#include
#include
using namespace std;
// constants
const int topicCount = 5;
const int maxRating = 10;
const string topics[ topicCount ] =
{ "global warming", "the economy", "war", "health care", "education" };
void pollUser( int responses[][ maxRating ] ); // function prototype
void displayResults( int responses[][ maxRating ] ); // function prototype
double calculateAverage( int votes[] ); // function prototype
void displayHighest( int responses[][ maxRating ] ); // function prototype
void displayLowest( int responses[][ maxRating ] ); // function prototype
int countPoints( int votes[] ); // function prototype
int main()
{
// empty initialization list initializes array to zero
int responses[ topicCount ][ maxRating ] = {};
int choice = 1; // sentinel to decide when to exit loop
while ( choice != 0 )
{
cout << endl; // add extra blank line
pollUser( responses );
// see if we should stop getting user input
cout << "Enter more data? (1=yes, 0=no): ";
cin >> choice;
} // end while
displayResults( responses );
} // end main
// get ratings on topics from one user
void pollUser( int responses[][ maxRating ] )
{
for ( int i = 0; i < topicCount; ++i )
{
cout << "On a scale of 1-" << maxRating << ", how important is "
<< topics[ i ] << "?\n"; // ask question
int rating; // rating user gave for this
do
{
cout << "> "; // display prompt
cin >> rating; // read rating
} // end do
while ( rating < 1 || rating > maxRating );
++responses[ i ][ rating - 1 ]; // store rating
} // end for
} // end method pollUser
// display polling results in tabular format
void displayResults( int responses[][ maxRating ] )
{
// display table header
cout << "\n" << setw( 15 ) << left << "Topic";
for ( int i = 1; i <= maxRating; ++i )
cout << setw( 4 ) << i;
cout << setw( 10 ) << "Average" << endl;
// display rating counts and averages for each topic
for ( int i = 0; i < topicCount; ++i )
{
cout << setw( 15 ) << left << topics[ i ]; // display topic
// display number of times topic was given this score
for ( int j = 0; j < maxRating; ++j )
cout << setw( 4 ) << responses[ i ][ j ];
// display average rating for this topic
cout << setprecision( 1 ) << fixed << setw( 10 )
<< calculateAverage( responses[ i ] ) << endl;
} // end for
cout << endl; // add blank line
displayHighest( responses ); // display highest-rated issue
displayLowest( responses ); // display lowest-rated issue
} // end method displayResults
// calculate average number of points
double calculateAverage( int votes[] )
{
int count = 0; // total number of votes
for ( int i = 0; i < maxRating; ++i )
count += votes[ i ]; // add number of responses
return ( double ) countPoints( votes ) / count; // return average
} // end method calculateAverage
// display topic with most points
void displayHighest( int responses[][ maxRating ] )
{
int max = countPoints( responses[ 0 ] ); // maximum number of points
int maxIndex = 0; // index of issue with maximum number of points
for ( int i = 1; i < topicCount; ++i )
{
if ( countPoints( responses[ i ] ) > max )
{
// larger point count found, update maximum value and index
max = countPoints( responses[ i ] );
maxIndex = i;
} // end if
} // end for
cout << "Highest points: " << topics[ maxIndex ]
<< " (" << max << ")\n";
} // end method displayHighest
// display topic with fewest points
void displayLowest( int responses[][ maxRating ] )
{
int min = countPoints( responses[ 0 ] ); // minimum number of points
int minIndex = 0; // index of issue with minimum number of points
for ( int i = 1; i < topicCount; ++i )
{
if ( countPoints( responses[ i ] ) < min )
{
// smaller point count found, update minimum value and index
min = countPoints( responses[ i ] );
minIndex = i;
} // end if
} // end for
cout << "Lowest points: " << topics[ minIndex ]
<< " (" << min << ")\n";
} // end method displayLowest
// calculate total number of points for a given topic
int countPoints( int votes[] )
{
int sum = 0; // total number of votes
for ( int i = 0; i < maxRating; ++i )
sum += votes[ i ] * ( i + 1 ); // add weighted count
return sum;
} // end method countPoints
```
You might also like to view...
In Lync, ________ are similar to status updates on social media sites
Fill in the blank(s) with correct word
Which of the following is NOT identified in the textbook as a stage in the writing process?
A) Revising B) Planning C) Writing D) Collaborating