5 Replies - 345 Views - Last Post: 27 October 2013 - 01:43 AM Rate Topic: -----

#1 TychoKaine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-October 13

Average Rating Does Not Display Correctly

Posted 25 October 2013 - 03:42 PM

The program works fine with no errors, and the output looks the way that I want it, except for the average output. It seems that I am simply getting the sum of the times a particular topic was rated, but not the average of the ratings themselves. I was hoping a second set of eyes could offer some advice to steer me in the right direction.

#include < iostream >
#include < iomanip >
#include < array >
#include < string >

using namespace std;

int main()
{
	string topics[5] = {"Economy      ", "Health Care  ", "Education    ", "Gun Control  ",										"Environment  "};

	int responses[5][10];

    char choice = 'y';
	
	for (int i=0; i<5; i++)
    {
		for (int j=0; j<10; j++)
		{
          responses[i][j]=0;
        }
    }

	while (choice != 'n')
	{
		system("cls");
		cout << "Please rate the following political issues from 1 to 10.\n";
		cout << "1 being of least importance to you, and 10 being of greatest importance to you.\n\n"; 
		system("pause");
		int input = 0;

		for (int i=0; i<5; i++)
			{
				system("cls");   
				cout << topics[i] <<": ";
				cin >> input;

				while (input == 0)
                {
					cout << "That is an invalid response. Please enter a number between 1 and 10!\n";
					cin >> input;
				}
				while (input > 10 || input < 1)
                {
                 cout << "That is an invalid response. Please enter a number between 1 and 10!\n";
                 cin >> input;
                }
                
				responses[i][input-1]++;
			}  

		system("cls");
		float avg = 0;
		cout << "Topics        1  2  3  4  5  6  7  8  9  10   Avg\n\n";
		
		for (int i=0; i<5; i++)
		{
           cout << topics[i];
           
		   for (int j=0; j<10; j++)
           {
                avg += responses[i][j];
                cout << " " << responses[i][j] << " ";
           }
           cout << "    " << avg;
           avg = 0;
           cout << "\n";
		}

		cout << "\n\n Would you like to take this poll again? y/n: ";
		cin >> choice;
      } 
	system("pause");
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Average Rating Does Not Display Correctly

#2 Vaypur  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 52
  • Joined: 09-October 13

Re: Average Rating Does Not Display Correctly

Posted 25 October 2013 - 05:53 PM

On line 62 it would appear that you are adding the elements to average, but I don't see where you are dividing by the number of items added.
Was This Post Helpful? 0
  • +
  • -

#3 TychoKaine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-October 13

Re: Average Rating Does Not Display Correctly

Posted 26 October 2013 - 04:24 AM

Yeah, line 62 does add the elements to average, but just how many iterations of votes that have taken place, and not the actual value of the votes. I need to figure out a way to get the sum of the value of each topic vote, then I will be able to set avg to equal this total, divided by ten. This is where I am stuck unfortunately. I continue to make copies of this code so I can try different methods, but I cannot seem to get the average right yet. Any help with what code I need to obtain the value of the total number of votes for each topic instead of just the number of votes would be greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Average Rating Does Not Display Correctly

Posted 26 October 2013 - 04:43 AM

You have cataloged the responses, but using input-1 as your second dimension, means everything will be skewed if you aren't careful to fix that, as you work toward the average.

I'd suggest changing line 49 to just use input, instead. If someone enters 1 for instance, that should increment responses[i][1], not responses[i][0]. You can compensate for that, but it's awkward, and I don't see that it has been done so far.

Then, if someone has two inputs of 8, let's say, you are simply adding 8 to your avg in line 62. You need to multiply responses[i][j] * j, to get 2 * 8 or 16.

Make sense? If not, print out the array responses, on just a few responses, and you'll see what I mean here.

The number you need to divide by to get your average, is the number of responses > 0, in responses[i][j] - but only if you get rid of the -1 part mentioned above.

Your catalog of responses has both the values of each response, and by counting them, the number of responses, as well. That works ONLY if you have set the values of responses (all elements), to zero, before you start.

This post has been edited by Adak: 26 October 2013 - 04:46 AM

Was This Post Helpful? 0
  • +
  • -

#5 TychoKaine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-October 13

Re: Average Rating Does Not Display Correctly

Posted 26 October 2013 - 08:36 PM

I tried getting a total and then dividing by 10, but my average is still coming out wrong no matter what I input for each topic, or how many iterations I go through it. Adak, I tried some of what you recommended in your reply, but being so new to this, I couldn't get my head wrapped around it and just ended up adding errors to the code.

#include < iostream >
#include < iomanip >
#include < array >
#include < string >

using namespace std;

int main()
{
	string topics[5] = {"Economy      ", "Health Care  ", "Education    ", "Gun Control  ",										"Environment  "};

	int responses[5][10];

	char choice = 'y';
	int input = 0;

	for (int i=0; i<5; i++)
    {
		for (int j=0; j<10; j++)
		{
          responses[i][j]=0;
        }
    }

	while (choice != 'n')
	{
		system("cls");
		cout << "Please rate the following political issues from 1 to 10.\n";
		cout << "1 being of least importance to you, and 10 being of greatest importance to you.\n\n"; 
		system("pause");

		for (int i=0; i<5; i++)
		{
			cout << topics[i] <<": ";
			cin >> input;

			while (input == 0)
                {
					cout << "That is an invalid response. Please enter a number between 1 and 10!\n";
					cin >> input;
				}

				while (input > 10 || input < 1)
                {
                 cout << "That is an invalid response. Please enter a number between 1 and 10!\n";
                 cin >> input;
                }

				responses[i][input-1]++;

		}

		system("cls");
		float avg;
		int total = 0;
		cout << "Topics        1  2  3  4  5  6  7  8  9  10    Avg\n\n";

		for (int i=0; i<5; i++)
		{
			cout << topics[i];

			for (int j=0; j<10; j++)
			{
				cout << " " << responses[i][j] << " ";
				total += responses[i][j];
			}

			avg = (float)total / 10;
		    cout << "   " << avg;
			avg = 0;
            cout << "\n";
		}

		cout << "\n\n Would you like to take this poll again? y/n: ";
		cin >> choice;
	} 
	
	system("pause");
    return 0;

}


Was This Post Helpful? 0
  • +
  • -

#6 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Average Rating Does Not Display Correctly

Posted 27 October 2013 - 01:43 AM

You have responses[][] cataloging the answers. You can't just add those up, and get the right total response number. So your average will not be right.

Say you have 2 people's responses, with answers of
category number rating number
===============================
First person:
0 5
1 7

Second person:
0 5
1 7

responses[0][5] would be 2
responses[1][7] would be 2

If you just add up responses > 0 (2 + 2) you will total only 4, so the average will be wrong at 1 (2 if your math is off however).

You need to do different math:
responses[0][5] (2) * [j] (which is 5) is 10
responses[1][7] (2) * [j] (which is 7) is 14
=============================================
Total is 24. 24 divided by 4 responses (24/4) equals an average of 6
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1