Some More Basic Operations

The get, set, min, max, sum and average operations.

Functions :

  • getElement()

  • setElement()

  • getMin()

  • getMax()

  • getSum()

  • getAverage()

getElement() function:

int getElement(int a[], int n, int index) {
    if (index >= 0 && index < n) {
        return a[index];
    } else {
        return -1;
    }
}

setElement() function:

void setElement(int a[], int n, int index, int value) {
    if (index >= 0 && index < n) {
        a[index] = value;
    } else {
        printf("Wrong index !");
    }
}

getMax() function:

int getMax(int a[], int n) {
    int max = a[0];
    for (int i = 0; i < n; i++) {
        if (a[i] > max) {
            max = a[i];
        }
    }
    return max;
}

getMin() function:

int getMin(int a[], int n) {
    int min = a[0];
    for (int i = 0; i < n; i++) {
        if (a[i] < min) {
            min = a[i];
        }
    }
    return min;
}

getSum() function:

int getSum(int a[], int n) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum = sum + a[i];
    }
    return sum;
}

getAverage() function:

float getAverage(int a[], int n) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum = sum + a[i];
    }
    float average = sum/n; 
    return average;
}

Contributed by Nitin Ranganath

Last updated