C The Internet and the web enable people to network join a c
C++: The Internet and the web enable people to network, join a cause, and so on. The presidential candidates in 2012 used the Internet to get out their messages and raise money. In this exercise, you’ll write a polling program that allows users to rate five social-consciousness issues from 1 to 10 (most important). Pick five causes (e.g., political issues, global environmental issues). Use a onedimensional string array topics to store the causes. To summarize 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.
Solution
First create a polling.h file which includes methods.
#include <string>
using namespace std;
class Polling {
public:
//constants
static const int issues = 5; //number of topics
static const int ratings = 10; //maximum rating scale
//constructor initializes array of topics and ratings
Polling( const array< string, issues > &, array< array< int, ratings >, issues > & );
int getHighestPoint(); //function to get highest rated topic
int getLowestPoint(); //function to get lowest rated topic
double getAverage( const array< int, ratings > & ) const; //function to get averate rating for each topic
void displayRating() const; //display the contents of the responses array
void processRating(); //perform various operations on ratings data
void setRating(); //function to rate different topics
void setTopics(); //function to set the topics
void enterRating(); //function to get the ratings, individually
int getTotalPoints( const array< int, ratings > & ) const; //function to get the total rating points of an array
private:
array< string, issues > topics; //array to store the topics
array< array< int, ratings >, issues > responses; //Multi-dimensional array to store ratings.
};
Implementation of methods :
#include <iostream>
#include <iomanip>
#include \"Polling.h\"
using namespace std;
//constructor initializes array of topics and ratings
Polling::Polling( const array< string, issues > &issuesArray, array< array< int, ratings >, issues > &ratingsArray )
: topics( issuesArray ), responses( ratingsArray ) {
}
//function to get averate rating for each topic
double Polling::getAverage( const array< int, ratings > &setOfRatings ) const {
int total = 0;
for ( int rating : setOfRatings )
total += rating;
return static_cast<double>(getTotalPoints( setOfRatings )) / total;
}
//function to get the ratings, individually
void Polling::enterRating() {
cout << \"Enter a rating (1 -\" << Polling::ratings << \") for the following topics: \" << endl;
for ( int issue = 0; issue < topics.size(); ++issue ) {
int rating;
cout << topics[ issue ] << \": \";
cin >> rating;
if ( rating >= 1 and rating <= 10 ) //error checking
++responses[ issue ][ rating - 1 ];
else {
cout << \"Invalid input. Enter values between 1 and \" << Polling::ratings << \" \" << endl;
enterRating();
}
}
}
//function to rate different topics
void Polling::setRating() {
string sentinel = \"y\";
while ( sentinel == \"y\" ) {
enterRating();
cout << \"Create another entry? (y/n) \";
cin >> sentinel;
}
}
//function to set the topics
void Polling::setTopics() {
int reply;
array< string, Polling::issues > defaultTopics = {\"Politics\", \"Hunger\", \"Security\", \"Crime\", \"Philantropy\"};
cout << \"Enter 1 or 2\ 1] Use Default Topics (\";
for ( string topic : defaultTopics )
cout << \" \" << topic << \" \";
cout << \")\ 2] Create your own Topics\ >> \";
cin >> reply;
if ( reply == 1 )
topics = defaultTopics;
else if ( reply == 2 ) {
cout << \"Enter your topics: \" << endl;
for ( int topic = 0; topic < Polling::issues; ++topic ) {
cout << \">> \";
cin >> topics[ topic ];
}
} else {
cout << \"Invalid Input. Enter 1 or 2.\" << endl;
setTopics();
}
}
//perform various operations on ratings data
void Polling::processRating() {
int highestTotal = getHighestPoint();
int lowestTotal = getLowestPoint();
cout << \"\ Topic with Higest Total Ratings: \" << topics[ highestTotal ] << \" (\" <<
getTotalPoints( responses[ highestTotal ] ) << \")\" << endl;
cout << \"Topic with Lowest Total Ratings: \" << topics[ lowestTotal ] << \" (\" <<
getTotalPoints( responses[ lowestTotal ] ) << \")\" << endl;
}
//display the contents of the responses array
void Polling::displayRating() const {
cout << \"\ The ratings are: \" << endl;
cout << setw( 14 );
for ( size_t i = 1; i <= ratings; ++i )
cout << i << \" \";
cout << \"Average\" << \" Total\" << endl;
for ( size_t issue = 0; issue < responses.size(); ++issue ) {
cout << setw( 12 ) << topics[ issue ] << \" \";
for ( size_t rating = 0; rating < responses[ issue ].size(); ++rating )
cout << responses[ issue ][ rating ] << \" \";
double average = getAverage( responses[ issue ] );
cout << setw( 8 ) << setprecision( 2 ) << fixed << average; //display to 2d.p.
int total = getTotalPoints( responses[ issue ] );
cout << setw( 6 ) << total << endl;
}
}
//function to get the total rating points of an array
int Polling::getTotalPoints( const array< int, ratings > &setOfRatings ) const {
int total = 0;
for ( int rating = 1; rating <= setOfRatings.size(); ++rating )
total += rating * setOfRatings[ rating - 1 ];
return total;
}
//function to get highest rated topic
int Polling::getHighestPoint() {
int fig = 0;
for ( int topic = 0; topic < topics.size(); ++topic )
if ( getTotalPoints( responses[ topic ] ) > getTotalPoints( responses[ fig ] ))
fig = topic;
return fig;
}
//function to get lowest rated topic
int Polling::getLowestPoint() {
int fig = 0;
for ( int topic = 0; topic < topics.size(); ++topic )
if ( getTotalPoints( responses[ topic ] ) < getTotalPoints( responses[ fig ] ))
fig = topic;
return fig;
}
main method :
#include <array>
#include \"Polling.h\" //Polling class definition
//function main begins program execution
int main() {
array< string, Polling::issues > topics = {};
array< array< int, Polling::ratings >, Polling::issues > responses = {}; //two-dimensional array of ratings
Polling polling( topics, responses );
polling.setTopics();
polling.setRating();
polling.displayRating();
polling.processRating();
} //end main



