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.
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.
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
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);
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.
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.