Pages

Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

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];
}


Sunday, December 26, 2010

Selection Algorithm In C

/**
 * This function is an implementation of the selection algorithm.
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 26, 2010
 * @version : 1.0
 */


/**
 * This function will do give an index of the minimal value.
 * @param data array of integer of the data
 * @param len int length of the data
 * @return minIndex int an index of the min value
 */
int getMinIndex(int data[], int len){
    int minIndex = 0;
    int i;
    for(i=1;i<len;i++){
        if(data[i]<data[minIndex]){
           minIndex = i;
        }
    }
    return minIndex;
}




/**
 * This function will do give an index of the mmaximal value.
 * @param data an array of integer of the data
 * @param len int length of the data
 * @return maxIndex int an index of the min value
 */
int getMaxIndex(int data[], int len){
    int maxIndex = 0;
    int i;
    for(i=1;i<len;i++){
        if(data[i]>data[maxIndex]){
            maxIndex = i;
        }
    }
    return maxIndex;
}


/**
 * This function will do give the max value
 *
 * @param data an array of integer
 * @param len int length of the data
 * @return minValue int min value of the data
 */
int getMinValue(int data[], int len){
    int minValue = data[0];
    int i;
    for(i=1;i<len;i++){
        if(data[i]<minValue){
            minValue = data[i];
        }
    }
    return minValue;
}



/**
 * This function will do give the max value of the data
 *
 * @param data an array of integer
 * @param len length of the data array
 * @return maxValue int max value of the data
 */
int getMaxValue(int data[], int len){
    int maxValue = data[0];
    int i;
    for(i=1;i<len;i++){
        if(data[i]>maxValue){
            maxValue = data[i];
        }
    }
    return maxValue;
}