Thread: min()function

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    5

    min()function

    Hi all.
    I have a problem in programming a function to select minimum values which are stored in an array. The parameter of the min function are dynamic values.
    For mow, I have an example on how to determine minimum value between 2 or 3 parameters. I need to create a function that can receive more than 3 parameters. I want to determine min value for each column in my two dimensional array as below.

    1.00 0.00 2.83 4.24
    5.00 4.24 1.41 0.00
    3.65 0.71 2.34 4.61
    4.30 0.00 0.50 3.20
    3.21 3.54 1.00 4.53

    The problem is to determine min value for each column.
    Can anyone here give me some ideas how to do this?

    Thanks in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First, write a program that will take a single dimension array, and find the minimum value in it. That should get you started.


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

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    5
    Hi all.

    I have this function like below to determine the min value for each column.
    But it doesnt work. The output seems right but not in the order of the array.

    I have an array euc_total[cluster][i] which stores data like below:
    1.0000 0.0000 2.8300 4.2400
    5.0000 4.2400 1.4142 0.0000

    My function:
    Code:
    void minimum_distance()
    {
    	double min = 0.0;
    	for ( i = 0; i < ROW; i ++ )
    	{
    		for (cluster = 0; cluster < cluster_num; cluster++)
    		{
    			if (euc_total[cluster][i] < euc_total[cluster + 1][i])
    				min = euc_total[cluster][i];
    			else
    				min = euc_total[cluster + 1][i];
    			
    		}
    		printf("\n %.4f",min);
    	}
    }//
    My output:
    1.4142
    0.0000
    1.0000
    0.0000

    The output should be like:
    column 1: 1.0000
    column 2: 0.0000
    column 3: 1.4142
    column 4: 0.0000

    Can anyone help?
    Last edited by rina; 09-28-2005 at 02:57 AM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Multi-dimensional arrays are indexed like so:
    Code:
    int array[ X ];
    int twodee[ Y ][ X ];
    int threedee[ Z ][ Y ][ X ];
    Thus:
    Code:
    int twodee[ ROWS ][ COLS ];
    int y, x;
    
    for( y = 0; y < ROWS; y++ )
        for( x = 0; x < COLS; x++ )
            printf("twodee[ %d ][ %d ] is %d\n", y, x, twodee[ y ][ x ] );
    Actually, since you want the information for each column, you'd do:
    Code:
    for( x = 0; x < COLS; x++ )
        for( y = 0; y < ROWS; y++ )
            if( minimum > twodee[ y ][ x ] )
                minimum = twodee[ y ][ x ];
    Or something akin to that.


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

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by rina
    Code:
    		if (euc_total[cluster][i] < euc_total[cluster + 1][i])
    				min = euc_total[cluster][i];
    			else
    				min = euc_total[cluster + 1][i];
    			
    		}
    You are accessing values that do not belong to the right cluster.
    BTW your algorythm doesn't work.
    try to set min to euc_total[cluster][0] before the loop and run the loop from 1 to nclusters-1 and if the value is lower than min set min to that value.
    Kurt

Popular pages Recent additions subscribe to a feed