Pages

Tuesday, January 25, 2011

Finding Minimum And Maximum Value

/**
 * Finding maximum value over an array of integer.
 * 
 * @param count int
 *     size of elements of data
 * @param data array
 *     data in an array format
 *
 * @return data[index] int
 *     max value of the data
 */
int findMax( int count, int data[]){
    int maxIndex = 0;
    int i;
    for( i=0; i<count; i++){
        if(data[i] > data[maxIndex])
            maxIndex = i;
    }

    return data[maxIndex];
}


/**
 * Finding minimum value over an array of integer.
 * 
 * @param count int
 *     size of elements of data
 * @param data array
 *     data in an array format
 *
 * @return data[index] int
 *     min value of the data
 */
int findMin(int count, int data[]){
    int minIndex = 0;
    int i;
    for( i=0; i<count; i++){
        if(data[i]<data[minIndex])
            minIndex = i;
    }

    return data[minIndex];
}


No comments:

Post a Comment