This is a fairly simple problem (almost everything isif one’s basics are clear). The below program shows how you can do this. I have added some comments so that you can get an idea about what is happening.
#include <iostream>
#include <vector>
int main()
{
int numberOfInputs = 0;
std::cout<<"How many inputs?"<<std::endl;
std::cin>>numberOfInputs;//take input into numberOfInputs
std::vector<int> vectorOfInputs(numberOfInputs);//this vector(container) will contain all the inputs(both positive and negative)
//this will contain the sum of all positive numbers
int sum = 0;
//this will count how many numbers entered by user are positive
int countPositive = 0;
//use for loop or any other loop populate the vector by user input
for(int i = 0; i < numberOfInputs; ++i)
{
std::cout<<"input#"<<i+1<<":";
std::cin >> vectorOfInputs.at(i);
//if the number is positive increase sum
if(vectorOfInputs.at(i) >0)
{
sum += vectorOfInputs.at(i);
++countPositive;
}
}
//print all the positive numbers
for(int elem: vectorOfInputs)
{
if(elem>0)
{
std::cout<<elem<<std::endl;
}
}
//print the sum and average
std::cout<<"the sum of all the above positive numbers is: "<<sum<<std::endl;
std::cout<<"the average of all the above positive numbers is: "<<(static_cast<double>(sum))/countPositive<<std::endl;//static_cast is used to get the result as double instead of int
return 0;
}
0
solved How to add all positive integers and get their average [closed]