How to mulitply all inputs together?

I don't know how to feed a variable all the inputs and multiply them together?
Also of all the input how do you grab the maximum and minimum value regardless of how many I have?
Say I input 4 integers like (4,4,4,4,) multiply them together and take the square root to get geometric_mean.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int main () {
    int age;
    int ageTotal = 0;
    int geometric_mean=1;
    
    int numberOfpeopleEntered = 0;
    
    cout << "Enter first persons age or -1 to quit "<<endl;
    cin>>age;
    
    while (age != -1){
        ageTotal = ageTotal + age;
        numberOfpeopleEntered++;
       
        cout<<"Enter next persons age or -1 to quit "<<endl;
        cin>>age;   
    }
    cout<<"Number of people entered: "<<numberOfpeopleEntered<<endl;
    cout<<"Average age:"<<ageTotal/ numberOfpeopleEntered<<endl;
    cout<<"Geometric mean: "<<pow(geometric_mean,.5)<<endl;
 return 0;
}  


http://ideone.com/8sqm80
Well first use a do/while for multiple inputs and second use an int array or vector to store the ages then just iterate through it and multiply for the total and sqrt is in the cmath header library
What s/he said. You aren't storing more than one value at the moment.
Can you give me an example and how to go about doing that ? I don't know how to go about doing that. Also based on all the input a make in the loop who do I get the max and or minimum value?
Do you know how to use if/else statements yet? And are you using the tutorial on this site? If so how far are you?
Yes I do know hwo do use if/else statements. No I'm not using the tutorial on the site. But I'm trying to do this with loops only.
I rewrote the code with a do-while loop, but I don't understand how to store the input into an array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>  //std::cout , std::cin , std::streamsize
#include <vector>    //std::vector<>
#include <limits>    //std::numeric_limits<>
#include <algorithm> //std::minmax_element
#include <cmath>     //pow()
int main( int argc , char **argv )
{
    auto count = 0 , temp = 0 , average = 0;
    long double total = 1; //could be a large total with multiplication and is a double because of sqrt
    std::vector<unsigned short> age( 0 );
    //unsigned since ages are postive.
    //short because ages are less than like 32k
    //or what ever the max on short is.
    //( 0 ) because it starts as an empty vector
    do
    {
        std::cout << "Please enter your age or <999> to quit.\n> " << std::flush;
        std::cin >> temp;
        while( std::cin.fail() )
        //continue getting inputs while it is a bad input
        {
            std::cin >> temp;
            //getting input
            std::cin.clear();
            std::cin.ignore( std::numeric_limits<std::streamsize>::max() , '\n' );
            //clearing buffer
        }

        if( temp != 999 )
        {
            age.push_back( temp );
        }
        //if the age is not the exit age then add it to the vector

        ++count;
    } while( temp != 999 );
    //count - 1 since I just incremented it.
    count = 0;
    for( const auto &it : age )
    {
        ++count;
        std::cout << "Person " << count << " is " << it << " year(s) old. " << std::endl;
        total *= it;
        average += it;
    }
    //iterate through the vector and output each person and their age.
    std::cout << "The total amount of people entered is " << count << std::endl;
    std::cout << "The average age is " << average / count << std::endl;
    total = pow( total , ( double )1/count );
    std::cout << "The Geometric mean is " << total << std::endl;

    auto result = std::minmax_element( age.begin() , age.end() );
    //getting the min and max value from the vector
    std::cout << "The youngest person is person " << result.first-age.begin() + 1 << " and is " << *result.first << " year(s) old." << std::endl;
    std::cout << "The oldest person is person " << result.second-age.begin() + 1 <<  " and is " << *result.second << " year(s) old." << std::endl;
    //outputting the min age and max age
}
Please enter your age or <999> to quit.
> 1
Please enter your age or <999> to quit.
> 2
Please enter your age or <999> to quit.
> 3
Please enter your age or <999> to quit.
> 4
Please enter your age or <999> to quit.
> 5
Please enter your age or <999> to quit.
> 6
Please enter your age or <999> to quit.
> 7
Please enter your age or <999> to quit.
> 8
Please enter your age or <999> to quit.
> 9
Please enter your age or <999> to quit.
> 999
Person 1 is 1 year(s) old.
Person 2 is 2 year(s) old.
Person 3 is 3 year(s) old.
Person 4 is 4 year(s) old.
Person 5 is 5 year(s) old.
Person 6 is 6 year(s) old.
Person 7 is 7 year(s) old.
Person 8 is 8 year(s) old.
Person 9 is 9 year(s) old.
The total amount of people entered is 9
The average age is 5
The Geometric mean is 4.14717
The youngest person is person 1 and is 1 year(s) old.
The oldest person is person 9 and is 9 year(s) old.

Process returned 0 (0x0)   execution time : 6.117 s
Press any key to continue.
Last edited on
So based on the tutorial on the site I am currently learning about arrays. Thanks a lot @giblit. But that is currently to advanced. I haven't learned about vectors or limits. Really appreciate it though.
vectors and arrays are almost identical and limits is just to ignore the buffer you could get rid of the ignore and clear if you want. It was just so if someone input like the letter a or something it wouldn't count

TheIdeasMan 5 seconds ago wrote:
arrays are when you know the size of it before compiling vectors are for when you do not

^ summed up

I just prefer vectors
Last edited on
Example based on what you should know already (these do not need additional headers):
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    int agehold[];
    while (age != -1){
        ageTotal = ageTotal + age;
        agehold[numberOfpeopleEntered] = age;
        numberOfpeopleEntered++;
        cout<<"Enter next persons age or -1 to quit "<<endl;
        cin>>age;   
    }
//...
void Find_MaxMin(int age[], int num_of_ppl, int& max, int& min){
      if(   //Get starting values for later comparison
          age[0] < age[1]
      ){
         max = age[1];
         min = age[0];
      }else{
         min = age[1];
         max = age[0];
      }
   for(int iter = 0; iter < num_of_ppl; iter++){
      if(
          age[iter] > max
      )   max = age[iter];
      else if(
         age[iter] < min
      )   min = age[iter];
   }
}

Only concept that might be new to you is the reference parameters. Reference variables are basically an alias name for the original variables, so you access the original variables directly and can change their values.

Edit:
In reply to Gilbit below. Yes, a reference is the physical address of a variable. But in this case, the reference variable is like a pointer as it holds the "reference" to a variable and is not a memory address itself.
http://www.codeproject.com/Articles/13363/An-Insight-to-References-in-C
Last edited on
well technically a reference is the actual physical address of the item AFAIK
mrju5t wrote:
Say I input 4 integers like (4,4,4,4,) multiply them together and take the square root to get geometric_mean.


That isn't the correct algorithm for geometric mean.
sqrt(4*4*4*4) = 16 that's the wrong answer, the result should be 4.

The correct algorithm is to take the nth root of after multiplying together n numbers.
How do we find the nth root?
square root = pow(x, 0.5)
cube root = pow(x, 0.33333333333)
nth root = pow(x, 1.0/n)
For example:
thus (4*4*4*4)^(1/4) = pow(256, 1.0/4) = 4
or with 5 numbers, pow(4*5*6*7*8, 1.0/5) = 5.82739

Last edited on
I was curious as to what he was trying to do that makes a bit more sense. Was wondering why he would want a value of like 608 when you input ages 1 - > 9
Topic archived. No new replies allowed.