How to add all positive integers and get their average [closed]

—————- Edit —————-

I completely missed that you need to output the positive integers as well.. In that case, take a look at std::vector where you would add these integers, and then you would iterate through them using a for loop again. Or, you could also print them out right away..

Since this is apparently your homework, I will try to avoid a code solution.

Number of inputs

This should be pretty straight-forward – using std::cin should be more than sufficient to ask for number of inputs (look at std::cout to printing the leading text ‘How many inputs?’) – https://en.cppreference.com/w/cpp/io/cin . This input will be n.

Getting inputs

You will still make the use if std::cin, and a loop. I would say a for loop will probably be the easiest – https://en.cppreference.com/w/cpp/language/for . The goal is to do n iterations, and in every iteration, you can check if the input is either positive, or negative. Handle each case – either disregard it, or count the value.

Average

That should be the last and easiest part – you already have sum of your positive numbers, you know how many positive numbers you have (which happens in step 2) – basic arithmetics.

Leave a Comment