Home > database >  Can I make this more efficient with loop?
Can I make this more efficient with loop?

Time:10-21

       if(diff != 0){

          if(diff >= 50){
            if(userGuess > toGuess){
              cout << "Very high" << endl;
            } else if (userGuess < toGuess){
              cout << "Very low" << endl;
            }
          }

          if(diff >= 30 && diff < 50){
            if(userGuess > toGuess){
              cout << "high" << endl;
            } else if (userGuess < toGuess){
              cout << "low" << endl;
            }
          }

          if(diff >= 15 && diff < 30){
            if(userGuess > toGuess){
              cout << "Moderately high" << endl;
            } else if (userGuess < toGuess){
              cout << "Moderately low" << endl;
            }
          }

          if(diff > 0  && diff < 15){
            if(userGuess > toGuess){
              cout << "Somewhat high" << endl;
            } else if (userGuess < toGuess){
              cout << "Somewhat low" << endl;
            }
          }

}

These if statement are in loops that run until the correct number is guessed, my question is, instead of having all these if statements can I make it better with a big loop.

CodePudding user response:

You can certainly make it more readable, the approach is to make more functions and to group things together. And then you can do all your tests in a loop.

(You might want to refine the logic for negative numbers, I didn't test the correctness of all corner cases for you)

For example :

#include <iostream>
#include <string>
#include <string_view>
#include <vector>
#include <limits>

// group all information to be able to give a hint in one struct
struct hint
{
    bool in_range(int v) const noexcept
    {
        return (v>= min) && (v< max);
    }

    int min;
    int max;
    std::string_view output;
};

// then make a function that will return the correct hint
// for a given difference value
std::string_view get_hint(const int dif)
{
    // instead of having a global variable I use a static local variable, this is initialized only once.
    // use initializer list to initialize all hint structures
    static std::vector<hint> hints
    {
        {50,std::numeric_limits<int>::max(),"very high"},
        {30,50,"high"},
        {15,30,"moderately high"},
        {0,15, "somewhat high" },
        {-15,0, "somewhat low" },
        {-30,-15, "moderately low"},
        {-50,-30, "low"},
        {std::numeric_limits<int>::min(), -50, "very low"}
    };

    // check which hint text to return
    // This is the part where a loop helps
    for (const auto& hint : hints)
    {
        if (hint.in_range(dif)) return hint.output;
    }

    return {};
};

int main()
{
    std::cout << get_hint(-1) << "\n";
    std::cout << get_hint(49) << "\n";
    std::cout << get_hint(50) << "\n";
    std::cout << get_hint(-17)<< "\n";
    return 0;
}

CodePudding user response:

The main lesson here is that the compiler will almost always outsmart you and do it the fastest way it knows how. The code you have posted seems extremely unlikely to have any meaningful impact on the speed of your program.

Instead, it is often better to choose an approach which makes your code easier to read and maintain. For example, you could try something like this:

// Add size prefix based on difference
if (diff > 50) {
    cout << "Very ";
} else if (diff >= 30) {
    // No prefix
} else if (diff >= 15) {
    cout << "Moderatly ";
} else {
    cout << "Somewhat ";
}

// State if code was low or high
if (userGuess > toGuess) {
    cout << "high" << endl;
} else {
    cout << "low" << endl;
}
  •  Tags:  
  • c
  • Related