Home > OS >  My code is not showing an output when I am trying to find the largest number among three numbers in
My code is not showing an output when I am trying to find the largest number among three numbers in

Time:09-28

This Is My Code I Am A Beginner Whenever I run my code it takes the input for the three numbers but sometimes mostly when the given numbers are small is doesn't give back an output.

#include <iostream>
using namespace std;

int main()
{
   int num1, num2, num3;
   cout << "Enter First Number" << endl;
   cin >> num1;
   cout << "Enter Second Number" << endl;
   cin >> num2;
   cout << "Enter Third Number" << endl;
   cin >> num3;
   if (num1 > num2)
   {
      if (num1 > num3)
      {
         cout << "First Number Is The Largest";
      }
   }
   else if (num2 > num1)
   {
      if (num2 > num3)
      {
         cout << "Second Number Is The Largest";
      }
   }
   else
   {
      cout << "Third Number Is The Largest";
   }

   return 0;
}

CodePudding user response:

My thought is you're trying to get more complicated then necessary:

   if (num1 >= num2 && num1 >= num3)
   {    
      cout << "First Number Is The Largest";
   }
   else if (num2 >= num1 && num2 >= num3)
   {
      cout << "Second Number Is The Largest";
   }
   else
   {
      cout << "Third Number Is The Largest";
   }

CodePudding user response:

In this case, you are not testing equal numbers, e.g: num1 >= num2. Also:

 if (num1 > num2)
   {
      if (num1 > num3)
      {
         cout << "First Number Is The Largest";
      }
   }

You check if num1 > num2, but if num3 == num 1 or num 3 > num1 it will not appear a message. Try to put a message in the first if to show if it doesn't enter in second if, and then you can edit all the conditions.

CodePudding user response:

I don't know if it's a requirement that you say "First", "Second", or "Third", but if it's not, your code can be simplified down to something like this (this assumes you haven't learned about loops yet):

#include <iostream>

int main() {
  int biggest;
  int input;

  std::cout << "First number: ";
  std::cin >> biggest;

  std::cout << "Second number: ";
  std::cin >> input;
  if (input > biggest) biggest = input;

  std::cout << "Third number: ";
  std::cin >> input;
  if (input > biggest) biggest = input;

  std::cout << "Largest number was " << biggest << '\n';
}

Output:

~/tmp 
❯ ./a.out 
First number: 25
Second number: 64
Third number: 45
Largest number was 64

The first number is de-facto the biggest when it's read because it's the only number we know. After that, we read into a different variable, input so that we can compare against biggest.

There's some repetition though, and this method falls apart pretty quickly as the number of numbers to check grows.

If you know about loops and arrays, your code can be made smaller:

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

int main() {
  const std::vector<std::string> ordinals{"First", "Second", "Third"};
  int biggest = std::numeric_limits<int>::min();

  for (const auto& i : ordinals) {
    int input;
    std::cout << i << " number: ";
    std::cin >> input;

    if (input > biggest) biggest = input;
  }

  std::cout << "Largest number was " << biggest << '\n';
}

We now have to initialize biggest, so we give the lowest possible value an int can hold. Our loop reads into input and compares against biggest. We have the same effect, however, in that biggest is always replaced by the first value we enter.

If you are required to actually say "First", etc., it's now trivial to do as well.

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

int main() {
  const std::vector<std::string> ordinals{"First", "Second", "Third"};
  std::string ordinalWord;
  int biggest = std::numeric_limits<int>::min();

  for (const auto& i : ordinals) {
    int input;
    std::cout << i << " number: ";
    std::cin >> input;

    if (input > biggest) {
      biggest = input;
      ordinalWord = i;
    }
  }

  std::cout << ordinalWord << " number was largest. Value: " << biggest << '\n';
}

Output:

~/tmp 
❯ ./a.out 
First number: 25
Second number: 64
Third number: 45
Second number was largest. Value: 64

Again, this would fall apart if we had much more than three numbers to compare. So instead of words like "First" and so on, we could modify the program to print ordinal numbers.

#include <iostream>
#include <limits>
#include <random>  // For simulating larger sets
#include <string>
#include <vector>

std::string ordinal_suffix(int num) {
  num %= 10;

  if (num == 1) return "st";
  if (num == 2) return "nd";
  if (num == 3) return "rd";

  return "th";
}

int main() {
  // *** Automation setup and execution
  constexpr int setSize = 10'000;

  std::mt19937 prng(std::random_device{}());
  std::uniform_int_distribution<int> range(1, 100'000);
  std::vector<int> inputs;

  for (int i = 0; i < setSize;   i) {
    inputs.push_back(range(prng));
  }
  // *** End of automation setup and execution

  int biggest = std::numeric_limits<int>::min();
  int location = -1;

  for (std::size_t i = 0; i < inputs.size();   i) {
    if (inputs[i] > biggest) {
      biggest = inputs[i];
      location = i;
    }
  }

  std::cout << location   1 << ordinal_suffix(location   1)
            << " number was largest. Value: " << biggest << '\n';
}

Outputs:

~/tmp 
❯ ./a.out 
8725th number was largest. Value: 99944

~/tmp 
❯ ./a.out 
433rd number was largest. Value: 100000

~/tmp 
❯ ./a.out 
7674th number was largest. Value: 99987

~/tmp 
❯ ./a.out 
891st number was largest. Value: 99995
  •  Tags:  
  • c
  • Related