Two exact functions with varying results?? (2D array)

To summarize my situation, there's a 2D array that stores the results of a poll.
Each rows represent subject to vote on.
Each column is the rating from 1-10.

I'm trying to determine which subject got the highest rating and the lowest.

So I made this function for the maximum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Survey::getMaximum()
{
	int highestrowvalue = 0;
	for (size_t i = 0; i < ratingsrow; i++)
	{
		bool high = 0; //RESET AFTER ROW
		int sumofrow = 0;//RESET AFTER ROW
		for (size_t j = 0; j < ratingscolumn; j++)//CHECK COLUMNS
		{
			int multiplier = j + 1;
			sumofrow += multiplier* results[i][j]; //Gets the highest row
			if (sumofrow > highestrowvalue)
			{
				highestrowvalue = sumofrow;
				high = true;
			}
		}
		highestissue = high ? i : highestissue;
		highestissuevalue = highestrowvalue;//Set the highest value of row

	}
}


and it works like a charm. Boom sums up the highest row rating to 25 and display it.

However, I then input this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void Survey::getMinimum()
{
		int lowestrowvalue = 10000;
		for (size_t k = 0; k < ratingsrow; k++)
		{
			bool low = false;//RESET AFTER ROW
			int sumofrow = 0;//RESET AFTER ROW
			for (size_t l = 0; l < ratingscolumn; l++)//CHECKROWS
			{
				int multiplier = l + 1;
				sumofrow += multiplier* results[k][l];
				if (sumofrow < lowestrowvalue) //If lower than the lowest row values
				{
					lowestrowvalue = sumofrow;
					low = true;
				}
			}
			lowestissue = low ? k : lowestissue;
			lowestissuevalue = lowestrowvalue;
		}
}


and suddenly all I get as a value is 0.

THEY'RE EXACTLY THE SAME FUNCTIONS. It's just one is for a low value and one is for highest.
Last edited on
It's worth mentioning I use this output function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Survey::outputlimits()
{
	//outputMaximum
	getMinimum();
	cout << endl;
	cout << causesrow[lowestissue]
		<< " received the lowest point total with " << lowestissuevalue
		<< " points.\n";
	getMaximum();
	//output maximum
	cout << causesrow[highestissue]
		<< " received the highest point total with " << highestissuevalue
		<< " points.\n";

}


Say I rate 10, 8,7,6,5

output: highest is 10, lowest is 0



I've narrowed the issue down to be
the line
sumofrow += multiplier* results[k][l];//For some reason there seems to be a problem if I use the less operator
Last edited on
ARCHIVE Alright I figured out the problem! I just thought I'd keep this as archive in case someone runs into a similar problem as me.
This was assignment 7.35 in How to program C++ 9th edition.
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.


Basically, the minimum loop couldn't be similar to the maximum loop. Since the maximum increments bigger and will always fulfill the if. But not the minimum.


e.g. ratings for one issue is 1, 9,
max loop: 
j = 0
(sumofrow = 1) > (highestrowvalue = 0);
j = 10
(sumofrow  = 1+9) > (highestrowvalue =1);


 However,
this won't work for
min loop:
j = 0
(sumofrow = 1) < (lowestrowvalue = 0);
j=10
(sumofrow = 1+9) < (lowestrowvalue = 1);


The correct loop should look like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Survey::getMaximum()
{
	int highestrowvalue = 0;
	for (size_t i = 0; i < ratingsrow; i++)
	{
		bool high = 0; //RESET AFTER ROW
		int sumofrow = 0;//RESET AFTER ROW
		for (size_t j = 0; j < ratingscolumn; j++)//CHECK COLUMNS
		{
			int multiplier = j + 1;
			sumofrow += multiplier* results[i][j]; //Gets the highest row
	         }        //Basically this makes it end at the end of the row before checking the conditions.
                          if (sumofrow > highestrowvalue)                 
			{
				highestrowvalue = sumofrow;
				high = true;
			}
		
		highestissue = high ? i : highestissue;
		highestissuevalue = highestrowvalue;//Set the highest value of row

	}
}


All in all, the error stemmed from my maximum value function loop which was wrong; but under specific circumstance it worked.

This lead me to tunnel vision and not review my code correctly since I basically copy pasta'd my maximum function.


Last edited on
8
9
10
11
12
13
14
15
16
17
			for (size_t l = 0; l < ratingscolumn; l++)//CHECKROWS
			{
				int multiplier = l + 1;
				sumofrow += multiplier* results[k][l];
				if (sumofrow < lowestrowvalue) //If lower than the lowest row values
				{
					lowestrowvalue = sumofrow;
					low = true;
				}
			}


sumofrow is increased every time through the loop. It is compared to lowestrowvalue every time through the loop. Therefore, lowestrowvalue will never be larger than the first element of each row.

After running through the function, lowestrowvalue will be the smallest of all the elements in the first column.

EDIT: Guess I should have refreshed before posting.
Last edited on
Thanks a lot fg109!
You summarized it a lot better than I did.
I appreciate the effort it's nice to know people help each other a lot here.
Topic archived. No new replies allowed.