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

 

Similar post

Java Program to calculate average of array

C Program to calculate average of array

Java Program to calculate average of odd and even numbers in an array

Java Program to calculate average of odd and even numbers

C Program to calculate average of odd and even numbers in an array

C Program to calculate average of odd and even numbers

C++ Program to calculate average of odd and even numbers in an array

C++ Program to calculate average of odd and even numbers

 

 

 

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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

1 day ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago