Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 40

Menu driven program for smallest, largest, sum and average

CMiscellaneousJNTUH R22 · CS108ES

Aim

To write a menu driven C program that reads n numbers and lets the user choose to find the smallest, the largest, the sum or the average. The menu and each choice are separate functions, a switch statement picks the action, and an invalid choice shows an error message.

Algorithm

  1. Read n (1 to 100) and then n numbers into an array.
  2. Show the menu: 1. Smallest, 2. Largest, 3. Sum, 4. Average, 5. Exit, and read the choice.
  3. Use a switch on the choice. Case 1 calls the smallest function, case 2 the largest, case 3 the sum and case 4 the average, and prints the result.
  4. Case 5 prints a goodbye message.
  5. The default case prints an error message for any other choice.
  6. Repeat from step 2 until the choice is 5.

Program

C
#include <stdio.h>

#define MAX 100

/* Reads n and then n numbers into a[]. Returns n, or 0 if the input is not valid. */
int read_numbers(int a[]) {
    int n;
    printf("How many numbers (1 to %d)? ", MAX);
    if (scanf("%d", &n) != 1 || n < 1 || n > MAX) {
        return 0;
    }
    printf("Enter %d numbers: ", n);
    for (int i = 0; i < n; i++) {
        if (scanf("%d", &a[i]) != 1) {
            return 0;
        }
    }
    return n;
}

/* Shows the menu and returns the user's choice */
int menu(void) {
    int choice;
    printf("\n----- MENU -----\n");
    printf("1. Smallest\n2. Largest\n3. Sum\n4. Average\n5. Exit\n");
    printf("Enter your choice: ");
    if (scanf("%d", &choice) != 1) {
        return 5;   /* not a number or no more input: exit */
    }
    return choice;
}

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

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

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

double average(const int a[], int n) {
    return (double) sum(a, n) / n;
}

int main(void) {
    int a[MAX];
    int n = read_numbers(a);
    if (n == 0) {
        printf("Invalid input\n");
        return 1;
    }

    int choice;
    do {
        choice = menu();
        switch (choice) {
            case 1:
                printf("Smallest = %d\n", smallest(a, n));
                break;
            case 2:
                printf("Largest = %d\n", largest(a, n));
                break;
            case 3:
                printf("Sum = %d\n", sum(a, n));
                break;
            case 4:
                printf("Average = %.2f\n", average(a, n));
                break;
            case 5:
                printf("Exiting the program\n");
                break;
            default:
                printf("Invalid choice! Enter a number from 1 to 5.\n");
        }
    } while (choice != 5);

    return 0;
}

Output

Output
How many numbers (1 to 100)? 5
Enter 5 numbers: 12 7 25 3 18

----- MENU -----
1. Smallest
2. Largest
3. Sum
4. Average
5. Exit
Enter your choice: 1
Smallest = 3

----- MENU -----
1. Smallest
2. Largest
3. Sum
4. Average
5. Exit
Enter your choice: 2
Largest = 25

----- MENU -----
1. Smallest
2. Largest
3. Sum
4. Average
5. Exit
Enter your choice: 3
Sum = 65

----- MENU -----
1. Smallest
2. Largest
3. Sum
4. Average
5. Exit
Enter your choice: 4
Average = 13.00

----- MENU -----
1. Smallest
2. Largest
3. Sum
4. Average
5. Exit
Enter your choice: 7
Invalid choice! Enter a number from 1 to 5.

----- MENU -----
1. Smallest
2. Largest
3. Sum
4. Average
5. Exit
Enter your choice: 5
Exiting the program

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

In the run the numbers are 12, 7, 25, 3 and 18. The smallest is 3, the largest is 25, the sum is 65 and the average is 65 / 5 = 13.00. Choice 7 is not on the menu, so the default case prints the error and the menu is shown again.

average casts the sum to double before dividing. Without the cast, 65 / 5 is integer division, and a sum like 66 would give 13 instead of 13.20.

If something that is not a number is typed as a choice, menu returns 5 and the program ends, so it never gets stuck in a loop on bad input.

Common mistakes

  • Forgetting break at the end of a case. Execution then falls through into the next case, so choosing 1 also prints the largest, the sum and so on.

  • Starting min or max at 0 instead of a[0]. If all the numbers are positive, a minimum that starts at 0 is never replaced and the answer is wrong.

  • Dividing two ints for the average, as in sum(a, n) / n. The fraction is lost. Cast one of them to double first.

  • Leaving out the default case. An invalid choice then does nothing and the user gets no message.

Viva questions

Try to answer each one aloud before you open it.

  • What is the purpose of the default case in a switch?

    It runs when the value does not match any case. Here it shows the error message for an invalid choice.

  • What happens if break is missing after a case?

    Execution continues into the following cases (fall-through) until a break or the end of the switch is reached.

  • Why is a do-while loop used for the menu?

    The menu must be shown at least once before the choice can be checked. A do-while runs its body first and tests the condition afterwards.

  • Why is the array parameter written as const int a[]?

    const promises that the function only reads the array and does not change it. The compiler reports an error if the function tries to change it.

  • Can a switch be used with float values or strings in C?

    No. The switch expression and case labels must be integers (this includes char). For floats or strings, use if-else.

  • How is an array passed to a function in C?

    The array name passes the address of its first element, so the function works on the original array, not a copy. The size must be passed separately, as n is here.

Learn the concepts

All Programming for Problem Solving Lab experiments →