Sociopaten
Sociopaten

Reputation: 144

C++ functions that takes multiple numbers and perform different jobs

I have an exercise where I should input multiple numbers which finish with zero and then perform different calculations with them. So far I have created a do..while-loop for storing the numbers. I´ve been told that this is possible to do without an array, if that´s wrong please tell me right away. The functions I need to do is to add all the numbers together, locate the highest number and the second highest number, and also the mid (mean) value of all the numbers. I think there are different libraries I should use and there may be different options also. Please help me to understand the different libraries and how to use them. The search results I´ve found on the the web does not give me the answers I´m looking for because I could not find a similar problem to mine. This is my code so far:

#include<iostream>
#include<string>

using namespace std;

int sumCalc (int);
int midValCalc (int);
int secHighCalc (int);
int highestCalc (int);
void printCalc ();


int main()  {

    int nums;
    cout << "Input numbers, quit with 0: ";

    do {
        cin >> nums;
        cout << nums << " ";    // This is just to see the result    
    }
    while (nums != 0);

    return 0;
}

What is wrong with this add function?

int sumCalc (int total)   {

total = 0;
while (nums != 0) {
    total += nums;
}

return nums;

}

Upvotes: 2

Views: 994

Answers (2)

If you need to get the mean and highest and second-highest numbers you don't need an array(you don't need to store the numbers)

Essentially, you can keep track of the highest and second highest numbers that the user has entered, and if the user enters a number higher than those, you can adjust accordingly.

As for the mean average, you can also keep a running sum and count(# of numbers entered) which you can use to calculate the mean with whenever you need to.

Upvotes: 1

Jon Kiparsky
Jon Kiparsky

Reputation: 7753

I don't think you need any unusual libraries for this.

Think about how you'd calculate these things mentally. For example, if you want to sum a list of numbers that I read off to you, you'd just keep track of the running total, and forget each number as you added it - so that only needs one variable. If you want to keep track of the greatest number entered, again, you simply remember the biggest one you've seen so far and compare new numbers as you get them.

You can probably solve these.

Upvotes: 5

Related Questions