C Program to Calculate Average Using Arrays

Calculating the average of a set of numbers is a common task in programming. In C, arrays provide a convenient way to store and process multiple values. This article will guide you through three different C Programs to Calculate Average Using Arrays. We will cover the prerequisites, provide detailed explanations for each example, and conclude with a summary of what we have learned.

Prerequisites

Before we delve into the examples, ensure you have the following prerequisites:

  • A C compiler (such as GCC)
  • A text editor or IDE for writing your C code
  • Basic understanding of C programming concepts, especially arrays and loops

1. Calculating Average Using Arrays in C

In this section, we will look at three different methods to calculate the average of an array of numbers in C.

1.1 Using a Simple Loop

Example 1: Calculating Average Using a Simple Loop in C

This method uses a simple for loop to iterate through the array, sum the elements, and then calculate the average.

Code

C
#include <stdio.h>

int main() {
    int n, i;
    float sum = 0.0, average;

    printf("Enter the number of elements: ");
    scanf("%d", &n);

    float arr[n];

    printf("Enter the elements:\n");
    for (i = 0; i < n; ++i) {
        scanf("%f", &arr[i]);
        sum += arr[i];
    }

    average = sum / n;
    printf("Average = %.2f\n", average);

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Input the number of elements: scanf reads the number of elements.
  • Declare an array: float arr[n] dynamically allocates an array of size n.
  • Input array elements and sum them: A for loop reads the elements and adds them to sum.
  • Calculate and print the average: The average is calculated by dividing sum by n.

Output

C
Enter the number of elements: 5
Enter the elements:
10
20
30
40
50
Average = 30.00

1.2 Using Function to Calculate Average in C

Example 2: Calculating Average Using a Function

This method encapsulates the average calculation logic in a separate function for better modularity.

Code

C
#include <stdio.h>

float calculateAverage(float arr[], int n);

int main() {
    int n, i;
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    float arr[n];
    printf("Enter the elements:\n");
    for (i = 0; i < n; ++i) {
        scanf("%f", &arr[i]);
    }

    float average = calculateAverage(arr, n);
    printf("Average = %.2f\n", average);

    return 0;
}

float calculateAverage(float arr[], int n) {
    float sum = 0.0;
    for (int i = 0; i < n; ++i) {
        sum += arr[i];
    }
    return sum / n;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Declare a function: float calculateAverage(float arr[], int n) to calculate the average.
  • Input the number of elements and array elements: Similar to the previous example.
  • Call the function: calculateAverage(arr, n) calculates the average and returns it.

Output

C
Enter the number of elements: 4
Enter the elements:
5
10
15
20
Average = 12.50

1.3 Using Dynamic Memory Allocation in C

Example 3: Calculating Average Using Dynamic Memory Allocation

This method uses dynamic memory allocation to handle the array, making it suitable for scenarios where the array size is not known at compile time.

Code

C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, i;
    float *arr, sum = 0.0, average;

    printf("Enter the number of elements: ");
    scanf("%d", &n);

    arr = (float *)malloc(n * sizeof(float));
    if (arr == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }

    printf("Enter the elements:\n");
    for (i = 0; i < n; ++i) {
        scanf("%f", &arr[i]);
        sum += arr[i];
    }

    average = sum / n;
    printf("Average = %.2f\n", average);

    free(arr);
    return 0;
}

Explanation

  • Include necessary headers: #include <stdio.h> for input/output functions and #include <stdlib.h> for dynamic memory allocation.
  • Dynamic memory allocation: malloc allocates memory for the array.
  • Check for allocation failure: Ensures memory allocation was successful.
  • Input array elements and sum them: Similar to previous examples.
  • Calculate and print the average: The average is calculated and printed.
  • Free the allocated memory: free releases the dynamically allocated memory.

Output

C
Enter the number of elements: 3
Enter the elements:
1.5
2.5
3.5
Average = 2.50

2. Conclusion

In this article, we explored three different methods to calculate the average of an array of numbers in C: using a simple loop, using a function, and using dynamic memory allocation. Each method demonstrates a different aspect of handling arrays and calculations in C. By understanding these methods, you can choose the one that best fits your specific needs and enhance your skills in array manipulation and function usage in C programming.

Using these examples as a guide, you can confidently calculate averages in various ways depending on your requirements, making your programs more flexible and modular.