Vedast
Vedast

Reputation: 21

Right way of using max function and/or alternatives

So, here is my homework problem. It states "Enter five numbers five times. Every time, the program chooses the biggest number you enter, and it returns the arithmetic mean of those five largest numbers." Now, what I've done is use max function, however, I learned that it isn't useable in this way. Here is what I've tried:

#include <iostream>
using namespace std;

int main() {
  int zbir = 0;
  for (int i = 1; i < 6; i++) {
    int a, b, c, d, e;
    for (int j = 1; j < 6; j++) {
      cin >> a >> b >> c >> d >> e;
    }
    int maks = max(a, b, c, d, e);
    zbir = zbir + maks;
  }
  cout << zbir / 5;
}

Upvotes: 1

Views: 1048

Answers (3)

bhristov
bhristov

Reputation: 3187

Within your code you do not need to have an inner for-loop because you are already collecting all 5 of the user numbers with the cin >> a >> b >> c >> d >> e; statement. Having a second for loop around it will cause you to collect 5 numbers from the user 25 times total.

This is an example of the alternative to using the max function in which case a single number is collected 5 times making use of an inner for-loop:

int main()
{
    int sumOfMaxNums = 0;
    int userNum = 0;
    int maxNum = 0;
    // it is a good practice to have a const that will bound the loop
    // this way you can change it from here
    // in other cases you can have two different const bounds, one for the inner loop and 
    // one for the outer loop because they may differ
    const int NUM_ITERS = 5;

    // This will handle asking the user to enter the numbers 5 times
    for(int i = 0; i < NUM_ITERS; ++i)
    {
      // this loop will asks the user to enter a number 5 times and keep track of the max
      for(int j = 0; j < NUM_ITERS; ++j)
      {
        cin >> userNum;
        // here we want to set the maxNum to be the very first number that the user enters
        // or we could set it to smallest negative number
        if(j == 0)
        {
          maxNum = userNum;
        }
        else if(userNum > maxNum)
        {
          maxNum = userNum;
        }
      } // end of inner for-loop
      sumOfMaxNums += maxNum;
    } // end of outer for-loop

    cout << sumOfMaxNums / static_cast<float>(NUM_ITERS) << "\n";
} // end of main

Upvotes: 2

Allan Cameron
Allan Cameron

Reputation: 173803

You probably want to calculate the mean as a float rather than an int, otherwise the program will round the final answer down to the nearest whole number. Also, you really don't need to use five variables to store each cycle's inputs, since you can ignore any inputs that are less than the running maximum for that cycle. This means you don't need to use std::max at all.

#include<iostream>

int main()
{
  float running_total = 0;
  for (int cycle = 1; cycle < 6; ++cycle)
  {
    float cycle_max;
    for (int entry = 1; entry < 6; ++entry)
    {
      float input = 0;
      std::cin >> input;
      if (entry == 1 || input > cycle_max) cycle_max = input;
    }
    running_total += cycle_max;
  }
  std::cout << running_total / 5 << std::endl;
}

Upvotes: 1

L. F.
L. F.

Reputation: 20579

There are two versions of max: the two-argument version, and the initializer list version. In this case, you have five arguments, so use the initializer list version:

std::max({a, b, c, d, e})

(You need to #include <algorithm> to use std::max.)

Upvotes: 4

Related Questions