0

I wrote this piece of code, which has t test cases, and for every test cases it would input 3 digits. I want this program to then identify the minimum element among those three inputs, and then print the minimum element. What should I do?

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
       for(int i=0;i<3;i++)
       {
           int n;
           cin>>n;
       }
    }
    return 0;
}
5
  • 2
    As you get the inputs, compare the current one with the previous one and keep the minimum value. Commented Aug 4, 2021 at 18:36
  • IMHO, you should read the digits as char. You can still determine the minimum of the three. Commented Aug 4, 2021 at 18:36
  • so if we input 10,2,3 then 2 must be printed right? Commented Aug 4, 2021 at 19:00
  • @RohithV yes but i figured it out. i needed to do like this if there are three variables a,b,c then minimum=min(a,min(b,c)). and then cout<<minimum; Commented Aug 4, 2021 at 19:49
  • I provided an answer Commented Aug 4, 2021 at 19:56

2 Answers 2

2

Here's a example of finding the minimum of three digits, using a running minimum:

#include <iostream>

int main()
{
    unsigned int test_case_quantity = 0U;
    std::cin >> test_case_quantity;
    while(test_case_quantity--)
    {
        char minimum;
        std::cin >> minimum;
        char digit;
        std::cin >> digit;
        if (digit < minimum) minimum = digit;
        std::cin >> digit;
        if (digit < minimum) minimum = digit;
        std::cout << "    minimum: " << minimum << "\n";
        std::cin.ignore(100000, '\n'); // Synchronize to next line.
    }
    return 0;
}

For a more robust program, you can add validation that the character read is actually numeric.

Sign up to request clarification or add additional context in comments.

5 Comments

Can you elaborate on the benefit of reading a char as opposed to an int? As you mentioned, doing so can have consequences, in particular when a user enters a non-numeric number. So why use a char if it complicates the solution?
When reading as a number, you have to isolate the digits by division and remainder. Division and remainder take up a lot of execution time. Besides, the char type is a numeric data type and the operator< can be applied to them.
Ah I see. It's a bit of a runtime optimization. Since the lexicographical ordering of numeric characters is the same as their integer counterparts, the "find minimum" logic works the same.
Then doesn't this solution only work with single digit inputs? I apologize for continuing the conversation but if so I think it is useful to state this limitation.
As I stated, this is for the minimum of 3 digits, not 3 numbers. I got the idea from the OP's question: for every test cases it would input 3 digits. If you want numbers, that's a different implementation..
1

We can make use of an extra variable say small and when we are inputting the very first number, we can assign that first number to the small and in all other cases we can compare whether the current number is less than small, if yes then update small. Finally outside the loop we can print the value of small and this will be our answer.

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int small;
        for(int i=0;i<3;i++)
        {
            int n;
            cin>>n;
            if (i == 0)
                small = n;
            else {
                if (n < small)
                    small = n;
            }
        }
        cout<<"Smallest number is :" << small;
    }
    return 0;
}

input :

1
10 2 3

output :

Smallest number is :2

Flow : At first the value 10 will be assigned as small, then for the next element 2, we compare whether 2 is less than small which is 10 in that case, and reassign small to 2. Then this small is checked with 3, but 2 < 3, so small value remains unchanged.

I assume there will be only integer values as input.

2 Comments

This is the right idea, and the next logical enhancement to the OP's code without complicating the solution. For a runtime optimization, and modern C++ style, look to Thomas Matthew's answer.
This violates the OP's request of for every test cases it would input 3 digits. The numbers presented for input don't contain 3 digits. At least that's my understanding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.