Calculations

Cpp Program to calculate average of array

Cpp Program to calculate average of an array

In this tutorial, we will discuss a simple concept of Cpp Program to calculate average of an array

In this topic, we will learn about how to calculate the average of n number of elements (integer or floating point)using the array in C++ programming language.

Average calculates using the operator in Cpp programming language

Calculate average

find the average of integer values

Calculate the average of numbers using an array

In this program, we can see step by step procedure for completion of the program.

  • array declaration and initialization
  • Essential variable declaration
  • loop for calculating total through each value
  • Calculating average
  • Display result on the screen

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int arr[5]={45,34,65,43,23};
//Array declaration and inilization
    int total=0,i;//variable declaration
    float avg=0;
    for(i=0; i<5; i++){//loop for calculating total
        total=total+arr[i];//calculate total
    }
     avg=total/i;//calculate average
   cout<<"The average is: "<<avg;//Display result on the screen
    getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

The average is: 42

 

Program 2

Calculate the average of numbers using an array-entered by user

In this program, we can see what should be step by step procedure for completion of the program.

  • Array declaration
  • Essential variable declaration
  • loop for taking input from the user for every index
  • loop for calculating total through each value
  • Calculating average
  • Display result on the screen
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int arr[20];//Array declaration
    int num,i, sum=0;//Variable declaration
    double avg=0;
cout<<"Enter the numbers of array elements: ";
    cin>>num;//get input from user for Array size

    for(i=0; i<num; i++){
        cout<<"Enter the numbers: "<<i+1<<" :";
        cin>>arr[i];//get input from user for Array elements
    }
    for(i=0; i<num; i++){//loop for calculate sum and average
        sum=sum+arr[i];//calculate sum
        avg=sum/num;//calculate average
    }
    cout<<"Average of entered numbers are: "<<avg;
    getch();//Display result on the screen
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter the numbers of array elements: 5
Enter the number 1: 34
Enter the number 2: 43
Enter the number 3: 65
Enter the number 4: 45
Enter the number 5: 76
Average of entered numbers are: 52

 

find the average of floating point values

Program 3

In this program, we can see what should be step by step procedure for completion of the program.

  • array declaration and initialization
  • Essential variable declaration
  • loop for calculating total through each value
  • Calculating average
  • Display result on the screen

Calculate the average of numbers using an array

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    float arr[5]={56.45,12.4,65.99,45.6,12.5};
//Array declaration and initialization
    float total=0.0;//variable declaration
    float avg=0.0;
    int i;
    for(i=0; i<=4; i++){//loop for calculating total
        total=total+arr[i];//Calculating total
    }
     avg=total/5;//calculating average
    cout<<"The average is: "<<avg;//display result on the screen
    getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

The average is: 38.588

 

Program 4

In this program, we can see what should be step by step procedure for completion of the program.

  • Array declaration
  • Essential variable declaration
  • loop for taking input from the user for every index
  • loop for calculating total through each value
  • Calculating average
  • Display result on the screen

Calculate the average of numbers using an array- entered by the user

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
   float arr[20];//Array declaration
    int i,num;//variable declaration
    double avg=0.0,sum=0.0;
cout<<"Enter the number for calculating the average: ";
   cin>>num;//get input from user for array size

    for(i=0; i<num; i++){
        cout<<"Enter the numbers: "<<i+1<<": ";
        cin>>arr[i];//get input from user for array elements
    }
    for(i=0; i<num; i++){
        sum=sum+arr[i];//loop for calculating sum
        avg=sum/num;//calculate average
    }
    cout<<"Average of entered numbers are: "<<avg;
    getch();//display result on the screen
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter the number for calculating the average: 6
Enter the number 1: 12.23
Enter the number 1: 23.34
Enter the number 1: 34.45
Enter the number 1: 45.56
Enter the number 1: 67.78
Average of entered numbers are: 40.005

 

Explanation

This program gets input number of elements in the array and store in the variable num.

Then the for loop takes the elements from the user one by one and stores in the array.

Then for loop calculates the sum of elements using entered elements

Finally, the average calculated by the dividing sum of values by the number of elements

 

 

Suggested for you

Operator in C++ language

For loop in C++ language

Single dimensional array in C++ language

The data type in C++ language

C Program to calculate the average of an array
C program to calculate sum in array elements
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago