# Finding the maximum value in a matrix

Page 1 of 1

## 4 Replies - 545 Views - Last Post: 01 December 2017 - 10:25 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407939&amp;s=5380b6ccf226cb3ee8ef4be0d7ad82f6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Bestfrog

Reputation: 0
• Posts: 6
• Joined: 12-October 17

# Finding the maximum value in a matrix

Posted 30 November 2017 - 10:28 AM

Hi everyone,
I have to find the maximum value in a matrix 4x5 and its position. I created a void function, but I don't understand why it gives me the first one element, that is not the maximum. Can you help me?
```void cercamax5(int m[][5]){
int r=4,c=5,max=m[0][0],rmax=1,cmax=1;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(m[i][j]>=max){
max=m[i][j];
i=i+1;
i=rmax;
j=j+1;
j=cmax;
}else{
continue;
}
}
}
cout<<"The maximum value is"<<max<<"."<<endl;
cout<<"It's in the row "<<rmax<<" and column "<<cmax<<endl;

return;
}
```

Is This A Good Question/Topic? 0

## Replies To: Finding the maximum value in a matrix

### #2 ndc85430

• I think you'll find it's "Dr"

Reputation: 778
• Posts: 3,173
• Joined: 13-June 14

## Re: Finding the maximum value in a matrix

Posted 30 November 2017 - 10:32 AM

Why do you need to update the values of i and j in your if statement?

Also, that else is completely unnecessary.

### #3 GazinAtCode

Reputation: 33
• Posts: 121
• Joined: 26-September 16

## Re: Finding the maximum value in a matrix

Posted 30 November 2017 - 02:03 PM

In addition to what ndc85430 has already said, I would replace if(m[i][j]>=max) with if (m[i][j] > max), as there's no real need to update the max variable if both values are equal.

Also, i=rmax; and j=cmax; should be reversed.

### #4 Bestfrog

Reputation: 0
• Posts: 6
• Joined: 12-October 17

## Re: Finding the maximum value in a matrix

Posted 01 December 2017 - 07:54 AM

Thanks, I solved by reversing i=rmax and j=cmax!

### #5 Skydiver

• Code herder

Reputation: 6110
• Posts: 21,027
• Joined: 05-May 12

## Re: Finding the maximum value in a matrix

Posted 01 December 2017 - 10:25 AM

Lines 7 and 9 will cause you to skip a potential maximum value.