Average rating does not display correctly.

My program for school works properly, but I cannot seem get the average to compute properly. I was hoping a second set of eyes could give me some insight as to my mistake. Thank you.
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
#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;
}
Last edited on
Your average just looks like it's the sum. All you ever do is add individual numbers to it... you never actually divide to get the average.
It is just the sum at the moment, but the sum of how many iterations that the each topic was voted on and not the sum of the actual value of the votes. If I could just figure out how to get the total value of the votes per topic, then I could easily set the average to equal total divided by 10 (the number of options). This is the part that has me stumped at the moment. Any further insights would be greatly appreciated.
You should store the count of votes too, there's no other way around.
O.K. I got the sum and set the average to equal the total divided by ten, but I am getting the wrong average for each row now matter what rating, or how many iterations, I give each of the 5 topics during user input.

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
#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;

}
Topic archived. No new replies allowed.