Assignments » Single Dimension Array » Set1 »Solution 1

Write a C++ program to find the sum and average of one dimensional integer array.

#include<iostream>
using namespace std;

int main()
{
	int Arr[100],n,i,sum=0;

	cout<<"Enter number of elements you want to insert ";
	cin>>n;

	for(i=0;i<n;i++)
	{

		cout<<"Enter element "<<i+1<<":";
		cin>>Arr[i];
	}

	for(i=0;i<n;i++)
		sum+=Arr[i];

	cout<<"\nThe sum of Array is :"<<sum;
	cout<<"\nThe average of Array is :"<<sum/i;

	return 0;
}

Enter number of elements you want to insert 5
Enter element in ascending order
Enter element 1: 8
Enter element 2: 1
Enter element 3: 10
Enter element 4: 5
Enter element 5: 6

The sum of Array is :30
The average of Array is :6