YouTip LogoYouTip

C Examples Average Arrays

C Programming Example - Calculate Average of Array Elements |

-- Learning more than just technology, but dreams!

C Tutorial

C Tutorial C Introduction C Environment Setup C VScode C Program Structure C Basic Syntax C Data Types C Variables C Constants C Storage Classes C Operators C Decision Making C Loops C Functions C Scope Rules C Arrays C enum C Pointers C Function Pointers and Callback Functions C Strings C Structures C Unions C Bit Fields C typedef C Input & Output C File I/O C Preprocessors C Header Files C Type Casting C Error Handling C Recursion C Variable Arguments C Memory Management C Undefined Behavior C Command Line Arguments C Safe Functions C Sorting Algorithms C Project Structure C Examples C Classic 100 Examples C Quiz

C Standard Library

C Standard Library - Reference Manual <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library -

C Project Structure

C Classic 100 Examples

C Programming Example - Calculate Average of Array Elements

C Examples C Examples

Use a for loop to iterate through the output elements, sum all the elements to calculate the total, and then divide by the number of elements:

Example 1

#include<stdio.h>
int main()
{
    int array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    int sum, loop;
    float avg;

    sum = avg = 0;

    for(loop = 0; loop<10; loop++)
    {
        sum = sum + array;
    }

    avg = (float)sum / loop;

    printf("Average is %.2f", avg);

    return 0;
}

Output:

Average is 4.50

User-defined input:

Example 2

#include<stdio.h>
int main()
{
    int n, i;
    float num, sum = 0.0, average;

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

    while(n>100 || n<= 0)
    {
        printf("Error! Number should be in range of 1 to 100.n");
        printf("Enter again: ");
        scanf("%d", &n);
    }

    for(i = 0; i<n; ++i)
    {
        printf("%d. Enter number: ", i+1);
        scanf("%f", &num);

        sum += num;
    }

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

    return 0;
}

Output:

Enter number of elements: 4
1. Enter number: 1
2. Enter number: 2
3. Enter number: 4
4. Enter number: 8
Average = 3.75

C Examples C Examples

← C Examples Matrix TransposeC Examples Binary Decimal Conv β†’