Write a C++ program to read several positive integer numbers into an integer vec
ID: 3573747 • Letter: W
Question
Write a C++ program to read several positive integer numbers into an integer vector and then display to screen their sum and average value. The program should ask the user to enter numbers one at a time. The program should stop reading when the user enters a negative number. After reading all the numbers and storing them in the integer vector, the program should find the summation value of the numbers and then their average and then print the values to screen.
Please only write this program in C++.
Explanation / Answer
#include<iostream>
#include<vector>
using namespace std;
int main()
{ //declarations...
int n,sum=0,av=0;
vector<int> V;
cout<<"Enter numbers(negative number to stop):";
while((cin>>n)&&(n>=0))//reading...if negative number then loop breaks
{
V.push_back(n);//adding to vector int
sum=sum+n;//calculating sum
av=av+1;
}
av= sum/av;//calculating average
//printing values
cout<<"The summation : "<<sum<<endl;
cout<<"Average is :"<<av<<endl;
return 0;
}
ouput:-
Enter numbers(negative number to stop):1
2
3
4
5
6
7
-1
The summation : 28
Average is :4
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.