Dear All,

I got difficulties here related with extracting max and min value from every column in array.
So far i just can extract max-min value at a time by changing column index, stupid approach..eeeh, but i do it just for testing that code could extract right max and min value
for every column. But when i want to automatically got max and min values for every
different column, i got stuck..

Could you help me..please...

#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

main() 
{
     float M[3][3]=
     {
           {0.01, 1.02, 2.05},
           {2.01, 0.00, 5.00},
           {3.01, 4.00, 0.40}
     };
    
    float mx, mn;
    
    int i,j;    
    
     
    
    for (i=0;i<3;i++)
    {
     
     for (j=0; j<1; j++) // Here i changed the column for testing only.
                         // with j=0 and j<1 i just want to make sure that max = 3.01
                         // and min = 0.01.
                         // so when i changed j = 1 and j<2 i got max = 4.00, min 0.00
                {
                if(i==0)
                {mn = M[0][j];} 
                if (mx < M[i][j])
                {mx = M[i][j];}
                if (M[i][j] < mn)
                {mn= M[i][j];}
               
               }
                
     }    
    printf("min=%.2f + max=%.2f\n",mn,mx);
    system ("pause");	    
     return 0;
}

regards,

roy

Recommended Answers

All 7 Replies

Incorrect mx/mn assignment logic (think about why). Right solution:

// don't use float type, use double.
double m[3][3] = // dont't use capital letters as var names
...;                      // it's not a good style
double minval, maxval;
for (int i = 0; i < 3; ++i)
{
    minval = maxval = m[i][0]; // hic!
    for (int j = 1; j < 3; ++j)
    {
        if (m[i][j] < minval)
            minval = m[i][j];
        else if (m[i][j] > maxval)
            maxval = m[i][j];
    }
    // print here or what else...
}

Is this what you're looking to do?

colMin = array[0][0];
colMax = array[0][0];

for (col = 0; col < MaxCol; col++)  {
   for(row = 0; row < MaxRow; row++)  {
      if(colMin) > array[row][col]
         colMin = array[row][col];
      //and repeat above if statement for colMax

   }
}

Hi, you need array for maximum and minimum in each column

float M[3][3]=
    {
		{0.01, 1.02, 2.05},
		{2.01, 0.00, 5.00},
		{3.01, 4.00, 0.40}
	};
    //You will have three minimum and three maximum so array needed
	float mx[3], mn[3];

	int i,j,k;    
    
	for ( i =0; i<3 ; i++){	//This is for column
		mn[i] = M[0][i]; //Assume first element in i th Column is Minimum
		mx[i] = M[0][i]; //Assume first element in i th Column is Maximum
		
		for ( j = 1; j<3; j++){ // To fine min max in each column 

			if (mx[i] < M[j][i])
				{mx[i] = M[j][i];}
			
			if (M[j][i] < mn[i])
				{mn[i] = M[j][i];}
		}
	}
	//Print the final result of min and max
	for ( i =0; i<3 ; i++){
		printf("Column = %d min=%.2f in max=%.2f\n",i, mn[i],mx[i]);
	}

Of course, you don't need an array for min and max, if you just print the column's min and max, at the end of each column's check.

Simple. :)

Wow so many responds, i should try it one by one...thanks Guys..

Regards from novice and newbie in C

me_roy

Here is your code

int main ()
float M[3][3]=
{
   {0.01, 1.02, 2.05},
   {2.01, 0.00, 5.00},
   {3.01, 4.00, 0.40}
};

float mn, mx;
for (int i = 0; i < 3; i++)  // change the column for testing only HERE
// for (i=0; i<1; i++) to get max = 3.01 and min = 0.01
// for (i=2; i<3; i++) to get max = 5.00 and min = 0.40
{    
   mn = mx = M[i][0]; 
   for (int j = 1; j < 3; j++)     // DO NOT change the column here
   {        
      if (M[i][j] < mn)            
         mn = M[i][j];        
      if (M[i][j] > mx)            
         mx = M[i][j];    
   }    
}
printf("min=%.2f + max=%.2f\n",mn,mx);
system ("pause");	    
return 0;
}

WOW! 4 pieces of code to choose from! Which one works best? Which to choose to turn in as the assignment answer?

C'mon guys, we're here to help, not do everyone's assignments for them!

commented: word. +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.