[Help] Movie Ratings - 2d Arrays

I have completed all my source code, aside from being able to go through my 2d array and find the highest rating by a viewer and display the column number.
1
2
3
int Reviewers = 4;
int Movies = 6;
int reviews[Reviewers][Movies];


For example output should be something like this:

Viewer #100 #101 #102 #103 #104 #105
#0     5    4    3    3    5    5
#1     4    2    3    1    1    1
#2     2    3    5    2    1    4
#3     1    2    3    4    5    4

Enter a reviewer number (0-3), to find the Movie they rated the highest:1
Reviewer#1 rated Movie #100 as the highest


That is what I would like to achieve, but I have no idea how to get the column number of the highest number to display after user input.

Here is as far as I have come
1
2
3
4
5
6
7
8
9
  
for(int col = 0; col < Movies; col++)
{
    if(reviews[0][col] > reviews[0][col-1])
    {
        highest = reviews[0][col];
    }
}
cout << highest <<endl;


Thanks,

mccahon
Help would be appreciated....
To find the index of a maximum element you have to do the following:
1) make a variable that will contain the column index with maximum element
2) initialize it with zero (first column contains the maximum element)
3) loop through the rest of the columns
3a) compare the element in current column to the element in the maximum column so far
3b) if it is larger, replace the index of column with maximum element with current

By the end of the loop the variable will contain the index of column with maximum value.

Tell me if you have trouble translating this algorithm into actual code, but please try it yourself first.
And of course you can use the standard library algorithm max_element for that: http://www.cplusplus.com/reference/algorithm/max_element/
But I think it would be a good exercise for you to do it yourself - every programmer has to do it at least once in his life :)
Well here is my unfinished try at it, but this seems to return incorrect answers, for example it returns movie 104 as highest(Value = 4) when 103 was the highest(Value = 5)

GOT IT THANK YOU KRAkatau!!!!

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
void showReviewersHighestRating(int reviews[][Movies])
{
    int reviewerNum, highest = 0, movieNum = 0;
    cout <<endl<< "Enter a reviewer number #(0-3), to find the Movie they rated the highest:";
    cin >> reviewerNum;
    switch(reviewerNum)
    {
        case 0:
        highest = 0;
        movieNum = 100;
        for(int col = 0; col < Movies; col++)
        {
            if(reviews[0][col] > highest)
            {
                highest = reviews[0][col];
                if(highest == reviews[0][0])
                {
                    movieNum = 100;
                }
                if(highest == reviews[0][1])
                {
                    movieNum = 101;
                }
                if(highest == reviews[0][2])
                {
                    movieNum = 102;
                }
                if(highest == reviews[0][3])
                {
                    movieNum = 103;
                }
                if(highest == reviews[0][4])
                {
                    movieNum = 104;
                }
                if(highest == reviews[0][5])
                {
                    movieNum = 105;
                }
            }
        }
        cout << movieNum <<endl;
        break;
        case 1:
        break;
        case 2:
        break;
        case 3:
        break;
        default:
        break;
    }
}
Last edited on
Great! However I should say in your case you don't need the switch (just use reviewers[reviewerNum][col]). Plus, you can set movieNum = 100 + col; to get rid of those if's.
Topic archived. No new replies allowed.