Thread: Finding maximum and minimum number in 2D array

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    18

    Lightbulb Finding maximum and minimum number in 2D array

    Code:
    void findMaxnMin(double x[][COLS])
    {
    	int max=x[0][0], min= x[0][0];
            int i,j;
    	
    	for (i=0; i<ROWS; i++)
    		for (j=0; j<COLS; j++)
    		{	
    			if(x[i][j]> max)
    			max= x[i][j];
    			
    			if(x[i][j]<min)
    			min= x[i][j];
    		
    		}
    }
    This is my code to find the maximum and minimum number for the arrays. It works but I am wondering is there any other way to improve the code? or other way to find minimum and maximum.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Well right now that function doesn't do anything, since max and min are never read. Also, x is a bad name for a variable.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Does C care that he is assigning a double to an integer variable?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by camel-man View Post
    Does C care that he is assigning a double to an integer variable?
    If he doesn't care that he is turning it into an int, C won't either. It'll just truncate it as needed.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by wonderwall View Post
    Code:
    void findMaxnMin(double x[][COLS])
    {
    	int max=x[0][0], min= x[0][0];
            int i,j;
    	
    	for (i=0; i<ROWS; i++)
    		for (j=0; j<COLS; j++)
    		{	
    			if(x[i][j]> max)
    			max= x[i][j];
    			
    			if(x[i][j]<min)
    			min= x[i][j];
    		
    		}
    }
    This is my code to find the maximum and minimum number for the arrays. It works but I am wondering is there any other way to improve the code? or other way to find minimum and maximum.
    When you want more than just the min and max, say just the Nth highest or the first N lowest etc then there are better alternatives. But for just finding the min and max, there is no better algorithm to do it. Note that a better implementation would not rely on global constants and would use the corrrect the data types for min and max.

    You should also actually do something with the results like pass them out by writing through pointers that are passed in.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Minimum/maximum number problem. Please help!
    By PYROMANIAC702 in forum C Programming
    Replies: 43
    Last Post: 07-15-2011, 12:28 AM
  2. finding mean minimum and maximum values
    By begginer in forum C Programming
    Replies: 1
    Last Post: 03-25-2011, 11:55 PM
  3. Invalid Output of Minimum and Maximum Value in Array
    By georgio777 in forum C Programming
    Replies: 10
    Last Post: 09-19-2009, 03:17 AM
  4. Replies: 2
    Last Post: 05-28-2009, 09:58 PM
  5. Replies: 10
    Last Post: 04-26-2009, 02:46 PM