#include stdafx.h
#include <iostream>
using namespace std;
#include <string>
int main(){
// Declaring variable for Positive integer
int p = 0;
cout << " Enter a positive integer value " ;
// Collecting Positive Integer variable
cin >> p ;
// Declaring integer for average calculation
int z = 0;
// Specifying umbers of loop
for(int cnt = 0; cnt < p; ++cnt)
{
cout << cnt << " You entered : " ;
// Collecting value of integer for average calculation
cin >> z ;
// Adding Variable Values
int x = z;
int c = x + z;
// Computing Average Value
if (cnt == p - 1){
int r = x/p;
cout <<" Your Average is: " << c << std::endl;
}
}
}
Need help with this arithmetic problem, I got this assignment from a c++ book i bought.
Write an averaging program. The program must prompt the user to enter a positive integer that specifies
the number of values (n) the user wishes to average. The program must then ask the user to input these n
values. The program should then compute the average of the values inputted and output it to the user—
use the following average formula: average= a0............an/n
Your program’s output should look like the following:
Enter the number of values to average: 5
[0] = 1
[1] = 2
[2] = 3
[3] = 4
[4] = 5
Average = 3
Press any key to continue
#include <iostream>
usingnamespace std;
int main()
{
cout<<"How many numbers would you enter?"<<endl;
int no=0; int total=0; int avg=0;
cin>>no;
int *array = newint[no];//dynamically allocate size of array
for(int i=0; i<no; i++)//input the numbers
{
cout<<"Enter number"<<i+1<<endl;
cin>>array[i];
total=total+array[i];//the total of all numbers entered
}
avg=total/no;//average=(sum of all/total number of all)
cout<<"Average is: "<<avg<<endl;
return 0;
}