[c++] Program to find largest and smallest among 5 numbers without using array

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 the program using array.

int largestNumber;
int smallestNumber;
int numbers[n];

largestNumber=numbers[0];
smallestNumber=numbers[0];
for (i=0 ; i<n; i++)
{
if (numbers[i] > largestNumber) 
{
largest = numbers[i];
}
if (numbers[i] < smallestNumber) 
{
smallestNumber= numbers[i];
}
}

But how to create it without using array. Any help??

This question is related to c++ c++11

The answer is


void main()
{
int a,b,c,d,e,max;
    max=a;
    if(b/max)
        max=b;
    if(c/max)
        max=c;
    if(d/max)
        max=d;
    if(e/max)
        max=e;
    cout<<"Maximum is"<<max;
}

Heres what I did, without using an array. This was a method to return the highest number of 5 scores.

double findHighest(double score1, double score2, double score3, double score4, double score5)
 {
   double highest = score1;
   if (score2 > score1 && score2 > score3 && score2 > score4 && score2 > score5)
      highest = score2;
   if(score3 > score1 && score3 > score2 && score3 > score4 && score3 > score5)
      highest = score3;
   if(score4 > score1 && score4 > score2 && score4 > score3 && score4 > score5)
      highest = score4;
   if (score5 > score1 && score5 > score2 && score5 > score3 && score5 > score4)
      highest = score5;
   return highest;
 }

An array is going to be far more efficient, but I had to do it for homework without using an array.


Here's my implementation: Simple and short

#include <iostream>
#include <cstdio>
using namespace std;


int max_of_five(int a, int b, int c, int d,int e){
    int large= max(max(a, b), max(c,d));
    return max(large,e);

}

int min_of_five(int a,int b,int c, int d,int e){
    int small=min(min(a,b),min(c,d));
    return min(small,e);
}


int main() {
    int a, b, c, d,e;

    scanf("%d %d %d %d %d", &a, &b, &c, &d,&e);
    int ans = max_of_five(a, b, c, d, e);
    int ans1=min_of_five(a,b,c,d,e);
    printf("Max:\t%d\n", ans);
    printf("Min:\t%d", ans1);

    return 0;
}

If you like to keep things simple, then here is my solution.

It works for any number of integers taken from standard input. It also works for negative integers. Enter end when you are done.

#include <iostream>

int main()
{
int max,min,input;
std::cout<<"Enter the number: ";
std::cin>>input;
min=max=input;

while(std::cin>>input){
    if(input>max) max=input;
    if(input<min) min=input;
    std::cout<<"Enter the number: ";
}
std::cout<<"\nMax: "<<max<<"\nMin: "<<min;
}

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;
}

int findMin(int t1, int t2, int t3, int t4, int t5)
{
    int min1, min2, min3;

    min1 = std::min(t1, t2);
    min2 = std::min(t3, t4);
    min3 = std::min(min1, min2);
    return std::min(min3, t5);
}

int findMax(int t1, int t2, int t3, int t4, int t5)
{
    int max1, max2, max3;

    max1 = std::max(t1, t2);
    max2 = std::max(t3, t4);
    max3 = std::max(max1, max2);
    return std::max(max3, t5);
}

These functions are very messy but easy to follow and thus easy to remember and it only uses the simple min and max methods which work best for 2 values.


Let max will hold the maximum of 5 numbers. Assign the first number to max. Take the 2nd number and compare it with max if the the 2nd number is greater than max then assign it to max else do nothing. Next take the 3rd number and compare it with max , if the 3rd number is greater than max assign it to max else do nothing. Do the same for 4th and 5th number. Finally max will hold the maximum of 5 number.


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. :)


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;
   }
}

#include <algorithm>
#include <iostream>

template <typename T>
inline const T&
max_of(const T& a, const T& b) {
    return std::max(a, b);
}

template <typename T, typename ...Args>
inline const T&
max_of(const T& a, const T& b, const Args& ...args) {
    return max_of(std::max(a, b), args...);
}

int main() {
    std::cout << max_of(1, 2, 3, 4, 5) << std::endl;
    // Or just use the std library:
    std::cout << std::max({1, 2, 3, 4, 5}) << std::endl;
    return 0;
}

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);

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;
}

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


You can do something like this:

int min_num = INT_MAX;  //  2^31-1
int max_num = INT_MIN;  // -2^31
int input;
while (!std::cin.eof()) {
    std::cin >> input;
    min_num = min(input, min_num);
    max_num = max(input, max_num);
}
cout << "min: " << min_num; 
cout << "max: " << max_num;

This reads numbers from standard input until eof (it does not care how many you have - 5 or 1,000,000).


Use a sorting network!

#include <iostream>
#include <utility>

int main()
{
    int a, b, c, d, e;
    std::cin >> a >> b >> c >> d >> e;

    if (a < b) std::swap(a, b);
    if (d < e) std::swap(d, e);
    if (c < e) std::swap(c, e);
    if (c < d) std::swap(c, d);
    if (b < e) std::swap(b, e);
    if (a < d) std::swap(a, d);
    if (a < c) std::swap(a, c);
    if (b < d) std::swap(b, d);
    if (b < c) std::swap(b, c);

    std::cout << "largest = " << a << '\n';
    std::cout << "smallest = " << e << '\n';
}