Sum of Array Average in C++


This code computes the sum and average of the first 5 elements in an array named numbers. The array contains 6 integers: 7, 5, 6, 12, 35, and 27. The code initializes the variables sum and count to 0, and then uses a for loop to iterate through the first 5 elements of the numbers array. Inside the loop, it adds each element to sum and increments count. After the loop, it computes the average by dividing sum by count, and then outputs the sum and average to the console.

Source Code

#include <iostream>
using namespace std;
int main()
{
   int numbers[]={7,5,6,12,35,27};
   int sum=0;
   int count=0;
   int average;
   cout<<"The numbers are: ";
   for(int i=0;i<5;i++)
   {
    cout<<numbers[i]<<"  ";
    sum+=numbers[i];
    ++count;
   }
   cout<<"\nSum : "<<sum<<endl;
   average=sum/count;
   cout<<"Average : "<<average<<endl;
   return 0;
}
To download raw file Click Here

Output

The numbers are: 7  5  6  12  35  
Sum : 65
Average : 13

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples