Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help to write a C++ program to read several positive integer numbers and

ID: 3683206 • Letter: I

Question

I need help to write a C++ program

to read several positive integer numbers and then display to screen their sum and average value using integer vector. 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.

Remember that:

Vector Forms:

                vector<element_type> object_name;

               

where

                element_type is any known type;

                object_name is the name of the vector object being defined;

               

Vector member functions:

capacity()                              returns the number of values a vector can store

size()                                       returns the number of values a vector currently contains

empty()                                   returns true if the vector contains no values

reserve(n)                              grows the vector so that its capacity is n

push_back(value)                                append value at a vector's end

pop_back()                           erase a vector's last element

front()                                    return a reference to a vector's first element

back()                                     return a reference to a vector's last element

Explanation / Answer

#include <iostream>
#include <vector>
using namespace std;

int main(void)
{

cout<<"Enter list of positive integers (negative to quit) ";
vector<int> data;
int input;
int sum=0;
double average = 0.0;
int i;

while(true)
{
cin>>input;

if(input<0)
break;
else
data.push_back(input);
}

if(data.empty())
cout<<"You entered 0 positive numbers Sum = 0, Average = 0 ";
else
{
cout<<"You entered following numbers: ";
for(i=0;i<data.size();i++)
cout<<data[i]<<" ";

for(i=0;i<data.size();i++)
sum = sum + data[i];

average = sum*1.0/data.size();
cout<<" Sum = "<<sum<<", Average = "<<average<<endl;

}

return 1;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote