4 Replies - 545 Views - Last Post: 01 December 2017 - 10:25 AM Rate Topic: -----

#1 Bestfrog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

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

Reputation: 778
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#4 Bestfrog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6110
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1