Program to find largest and smallest among 5 numbers without using array

前端 未结 15 2152
遇见更好的自我
遇见更好的自我 2021-01-06 07:25

Yesterday I went for an interview where I have been asked to create a program to find largest and smallest among 5 numbers without using array.

I know how to create

相关标签:
15条回答
  • 2021-01-06 07:54

    The > and < are transitive properties, so if a > b and b > c, then a > c. So you can

    int a=10, b=6, c=4, d=21, e=4;
    
    int maxNum = a;
    int maxNum = max(b, maxNum);
    int maxNum = max(c, maxNum);
    int maxNum = max(d, maxNum);
    int maxNum = max(e, maxNum);
    
    0 讨论(0)
  • 2021-01-06 07:59

    For example 5 consecutive numbers

    int largestNumber;
    int smallestNumber;
    int number;
    std::cin>>number;
    largestNumber = number;
    smallestNumber = number;
    for (i=0 ; i<5; i++)
    {
       std::cin>>number;
       if (number > largestNumber) 
       {
         largest = number;
       }
       if (numbers < smallestNumber) 
       {
         smallestNumber= number;
       }
    }
    
    0 讨论(0)
  • 2021-01-06 07:59

    You could use list (or vector), which is not an array:

    #include<list>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    int main()
    {
        list<int> l;
        l.push_back(3); 
        l.push_back(9); 
        l.push_back(30);    
        l.push_back(0); 
        l.push_back(5); 
    
        list<int>::iterator it_max = max_element(l.begin(), l.end());
        list<int>::iterator it_min = min_element(l.begin(), l.end());
    
        cout << "Max: " << *it_max << endl;
        cout << "Min: " << *it_min << endl;
    }
    
    0 讨论(0)
  • 2021-01-06 08:00
    int findMin(int t1, int t2, int t3, int t4, int t5)
    {
        int min;
    
        min = t1;
        if (t2 < min)
            min = t2;
        if (t3 < min)
            min = t3;
        if (t4 < min)
            min = t4;
        if (t5 < min)
            min = t5;
    
        return min;
    }
    
    int findMax(int t1, int t2, int t3, int t4, int t5)
    {
        int max;
    
        max = t1;
        if (t2 > max)
            max = t2;
        if (t3 > max)
            max = t3;
        if (t4 > max)
            max = t4;
        if (t5 > max)
            max = t5;
    
        return max;
    }
    
    0 讨论(0)
  • 2021-01-06 08:01

    Works for any number of numbers taken from standard input:

    #include <algorithm>
    #include <iterator>
    #include <iostream>
    
    int main()
    {
        std::istream_iterator<int> it_begin(std::cin), it_end;
        auto p = std::minmax_element(it_begin, it_end);
        if (p.first != it_end)
            std::cout << "min: " << *p.first << " max: " << *p.second;
    }
    

    Disclaimer:
    Technicaly, this isn't required to work by C++ standard. The minimum iterator category required for minmax_element is ForwardIterator which stream iterators are not. Once an input iterator is dereferenced or incremented, its copies are no longer guaranteed to be dereferenceable or comparable to other iterators. It Works On My MachineTM. :)

    0 讨论(0)
  • 2021-01-06 08:04

    This is not an efficient answer but it still works

    int a,b,c,d,e,largest;
    if ((a>b) and (a>c) and (a>d) and (a>e))
    {    
        largest=a;
    }
    else if ((b>a) and (b>c) and (b>d) and (b>e))
    {    
        largest=b;
    }
    else if ((c>a) and (c>a) and (c>d) and (c>e))
    {    
        largest=c;
    }
    else if ((d>a) and (d>c) and (d>a) and (d>e))
    {    
        largest=d;
    }
    else 
    {
    largest=e;
    }
    

    you can use similar logic to fid the smallest value

    0 讨论(0)
提交回复
热议问题