here is what i tried before:
//finding maximum
for (int i = 0; i < n; i)
{
for (int j = 0; j < n; j)
{
if (arr[i][j] > max)
{
max = arr[i][j];
imax = i;
jmax = j;
}
}
}
//finding a sum
for (int i = 0; i < n; i)
{
for (int j = 0; j < n; j)
{
if (arr[i][j] > 0)
{
if (i <= imax && j < jmax)
sum = arr[i][j];
}
}
}
cout << "sum = " << sum << endl;
}
but that algorithm doesn't count it right, how should i do to make it work? looks like that my code is "limitng" the range of search, because of wrong condition and i duuno how to make it right?
CodePudding user response:
Let's think step by step.
Assuming, imax
is the row number and jmax
is the column number of the maximum elements present in the matrix.
Row selection procedure:
So, to accomplish our object, we will traverse row
which is <= imax
. That means, we'll consider the value of the current row as our answer only if current row <= imax
. If the current row becomes larger than row
, then we can stop traversing .
//finding a sum
for (int i = 0; i < n; i)
{
for (int j = 0; j < n; j)
{
if (arr[i][j] > 0)
{
if(i <= imax)
{
// do something
}
else
{
break;
}
}
}
}
We can also do it in below way:
//finding a sum
for (int i = 0; i < n && i <= imax; i)
{
for (int j = 0; j < n; j)
{
if (arr[i][j] > 0)
{
}
}
}
Column selection procedure:
It's a little bit different from the row selection procedure.
We can consider every column unless current row is equal to imax
. That means when
current row < imax
we will consider values of every column, but when current row == imax
we'll only consider smaller column's value as our answer.
//finding a sum
for (int i = 0; i < n && i <= imax; i)
{
for (int j = 0; j < n; j)
{
if(i < imax && arr[i][j] > 0)
{
// consider the value for answer
}
else
{
// here i == imax
// so we'll only consider smaller column's value as our answer
if(j < jmax && arr[i][j] > 0)
{
// consider the value for answer
}
else if(j >= jmax) // we've come out of the boundary. No matter the value is positive or negative, we don't need to check any further
{
break;
}
}
}
}
Overall code of finding sum portion will look like this:
//finding a sum
for (int i = 0; i < n && i <= imax; i)
{
for (int j = 0; j < n; j)
{
if(i < imax && arr[i][j] > 0)
{
// consider the value for answer
sum = arr[i][j];
}
else
{
// here i == imax
// so we'll only consider smaller column's value as our answer
if(j < jmax && arr[i][j] > 0)
{
// consider the value for answer
sum = arr[i][j];
}
else if(j >= jmax) // we've come out of the boundary. No matter the value is positive or negative, we don't need to check any further
{
break;
}
}
}
}
Note: Don't forget to initialize the value of max
, imax
, jmax
and sum
properly.
CodePudding user response:
So this doesn't work the way you've set it up.
Suppose you have a matrix like this:
0 1 5 10 8
4 22 8 18 11
1 2 6 14 9
1 27 6 14 9
1 2 6 14 9
Your max would find that imax = 3, jmax = 1
What happens when you try to count the element at [0, 5]? You miss it because jmax = 1, means all elements to the right of the j =1 column aren't counter by your conditions.
CodePudding user response:
It is not clear what you mean with "located before", but I am almost certain that this is the issue in your code. Supposed the max element is x
then you add all elements marked with o
and exclude elements marked with .
:
ooo...
ooo...
oox...
......
......
But I'd rather expect either this
oooooo
oooooo
oox...
......
......
or this:
ooo...
ooo...
oox...
oo....
oo....