๐Ÿ’ป
B.TECH I YEAR โ€ข I SEMESTER โ€ข R25

Programming for Problem Solving - Lab Manual

C Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R25 Regulation ๐ŸŽ“ I Year ๐Ÿ“š I Semester ๐Ÿ’ป C Programming ๐Ÿงช Lab Program
Selected Program

Write a C program to find the minimum, maximum and average in an array of integers.

โ† All Lab Programs

Write a C program to find the minimum, maximum and average in an array of integers.

Algorithm

start
    step1: read n
    step2: read array a[0...n-1]
    step3: min = a[0]
    step4: max = a[0]
    step5: sum = 0

    step6: for i = 0 to n-1 do
    step7:     if a[i] < min then
    step8:         min = a[i]
  
    step9:     if a[i] > max then
    step10:         max = a[i]

    step11:     sum = sum + a[i]

    step12: average = sum / n

    step13: print min
    step14: print max
    step15: print average
stop

Flowchart


C Programming
//to find the minimum, maximum and average in an array of integers
#include <stdio.h>
#include <math.h>

int main() {
    int n,arr[10],min,max,sum;
    float average;

    // Read the size of the array
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);

    // Read the elements of the array
    printf("Enter %d integers:\n", n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Initialize min, max and sum
    min = arr[0];
    max = arr[0];
    sum = arr[0]; // Using long long to avoid overflow

    // Iterate through the array to find min, max, and sum
    for (int i = 1; i < n; i++) {
        if (arr[i] < min) {
            min = arr[i];
        }
        if (arr[i] > max) {
            max = arr[i];
        }
        sum += arr[i];
    }

    // Calculate the average
    average = (float)sum / n;

    // Print the results
    printf("Minimum: %d\n", min);
    printf("Maximum: %d\n", max);
    printf("Average: %.2f\n", average);

    return 0;
}

OUTPUT
Enter the number of elements in the array: 5
Enter 5 integers:
5
15
25
10
20
Minimum: 5
Maximum: 25
Average: 15.00

๐Ÿ’ป Run This C Program

Write a C program to find the minimum, maximum and average in an array of integers.
The selected C program is loaded automatically. Edit the code, provide input through STDIN and click Run.

The compiler is embedded using OneCompiler. Your program runs in the embedded C environment; Study Glance does not execute C code directly on your web server.
โ† Back to PPS Lab Programs ๐Ÿ“š All Lab Manuals