Simple task to compare 4 inputs and find the largest

Hi all,

First time posting online for any help so please be gentle if I mess up the formatting etc.

I've been given homework to compare two inputs and output the highest integer value using functions. Once I've done that (and I have) I must change it to allow the user to input 4 values and determine the highest. Previously I used an if statement to find the largest however now I am unsure. I did find information online regarding an array which I kind of understand however I don't know how I would use them with a function. (I'm very very new to programming in general).

This is what I've got so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

int main()
{
	int higher(int x, int y);

	int first = 0, second = 0, largest = 0;

	cout << "Please enter the first score: " << endl;
	cin >> first;

	cout << "Please enter the second score: " << endl;
	cin >> second;
	
	largest = higher(first, second);
	cout << "The highest score is" << largest << endl;


	return 0;
}

int higher(int x, int y)
{
	if (x > y)
	{
		return x;
	}
	else
	{
		return y;
	}
}


I'll be checking back regularly so any help is appreciated
You can do so by using AND operator and if-else condition. You can take one variable and compare it with other three variables. For example if you have four integer variables var1, var2, var3 and var4. You can find highest number by using a technique like this:

if( var1>=var2 && var1>=var3 && var1>=var4)
{
return var1;
}

You can compare all four numbers one by one.
Sorry brother I can't give you complete code, because I am replying from mobile phone.
But I can suggest you a link where you can find 2 method to find greatest number http://www.cppbeginner.com/numbers/find-greatest-number-among-3-numbers-in-cpp
Last edited on
Hey,

Thanks for the quick response.

I had actually come back to say I have just used the && operator to do this however I was expecting a tidier(?) way to do this. Problem being I now have to use 10 user inputs and I feel like the code gets a little bit muddy with so many if statements.

This is what I've added:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int higher(int w, int x, int y, int z)
{
	if (x > y && x > w && x > z)
	{
		return x;
	}
	else if (y > w && y > x && y > z)
	{
		return y;
	}
	else if (w > x && w > y && w > z)
	{
		return w;
	}
	else
	{
		return z;
	}
}



Is there a more efficient way to write this? If you could point me in the right direction I would appreciate it.

Again, thanks for taking time to help me.
Wecome to the forum!

Ok, first of all, this code should not compile--on line 7 you are declaring a function. This should not be done in main(), but in the global scope. You should get a warning when you try to compile the code.
OK, try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<int> vec(4);
    for(int i = 0; i < 4; i++)
	{
	    cout << "Enter score #" << i + 1 << endl;
	    int score = 0;
	    cin >> score;
	    vec[i] = score;
	}
    std::sort(vec.begin(), vec.end());
    cout << "Largest value was " << vec[3] << endl;

    return 0;
}



Good luck with your future coding!
Last edited on
@polklk
Use your 'higher' function over and over again in a loop.

You don't need arrays or vectors to store your numbers if you're only interested in what the largest number is. Every time you go through the loop, compare your new input to your current largest number using the 'higher' function. Stick the result back into the variable keeping track of your largest number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

int higher(int x, int y);

int main()
{
    const int HOW_MANY_NUMBERS = 10;

    int input = 0, largest = 0;

    for(int i = 1; i <= HOW_MANY_NUMBERS; ++i)
    {
        cout << "Please enter score #" << i << ": ";
        cin >> input;
        largest = higher(input, largest);
    }

    cout << "The highest score is" << largest << endl;
    return 0;
}

int higher(int x, int y)
{
    if (x > y)
    {
        return x;
    }
    else
    {
        return y;
    }
}
closed account (E0p9LyTq)
There are several ways to code this, using C arrays or a C++ container class:

1. declare your array and needed variables at global scope, calling a custom function. Not recommended.

2. declare your array and needed variables at main() scope, passing the array and its size into the function. Return the found largest value.

3. declare a vector or C++ array at main() scope and pass that into your function. Return the largest value. Using a vector lets you change the number of integers to compare at runtime.

global scope:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

const int array_size = 4;
int my_array[array_size];
int largest;
size_t loop;

void find_largest();

int main()
{
   std::cout << "Enter four integers to find the largest one: ";
   
   for (loop = 0; loop < array_size; loop++)
   {
      std::cin >> my_array[loop];
   }
   
   find_largest();
   
   std::cout << "\nThe largest is: " << largest << "\n";
}

void find_largest()
{
   largest = my_array[0];

   for (loop = 1; loop < array_size; loop++)
   {
      if (largest < my_array[loop])
      {
         largest = my_array[loop];
      }
   }
}


main() scope:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

int find_largest(int array[], const int size);

int main()
{
   const int array_size = 4;
   int my_array[array_size];

   std::cout << "Enter four integers to find the largest one: ";

   for (size_t loop = 0; loop < array_size; loop++)
   {
      std::cin >> my_array[loop];
   }

   int largest = find_largest(my_array, array_size);

   std::cout << "\nThe largest is: " << largest << "\n";
}

int find_largest(int array[], const int size)
{
   int largest = array[0];

   for (size_t loop = 1; loop < size; loop++)
   {
      if (largest < array[loop])
      {
         largest = array[loop];
      }
   }
   
   return largest;
}


C++ vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>

int find_largest(std::vector<int> vect);

int main()
{
   std::vector<int> vect;

   int num_ints;
   std::cout << "How many integers to enter? ";
   std::cin >> num_ints;
   
   std::cout << "Enter " << num_ints << " integers to find the largest one: ";
   for (size_t loop = 0; loop < num_ints; loop++)
   {
      int number;
      std::cin >> number;
      vect.push_back(number);
   }

   int largest = find_largest(vect);

   std::cout << "\nThe largest is: " << largest << "\n";
}

int find_largest(std::vector<int> vect)
{
   int largest = vect[0];

   for (size_t loop = 1; loop < vect.size(); loop++)
   {
      if (largest < vect[loop])
      {
         largest = vect[loop];
      }
   }
   
   return largest;
}
booradley60's idea is solid, but for one tiny detail: the initial value of 'largest' must be less-or-equal to input. The 0 is perfect as long as all the input is non-negative integers. If the user can type negative values, then the largest must start smaller than 0.

std::numeric_limits<int>::min() is a fool-proof starting value.



There are many ways to write:
1
2
3
4
5
largest = higher(input, largest);

largest = ( largest < input ) ? input : largest;

if ( largest < input ) { largest = input; }
keskiverto is correct. And if you do use numeric_limits, don't forget #include <limits> . If you want zero to be the smallest value, consider using an unsigned type for the integer variables.
Last edited on
Thanks for the fast input guys/gals, I'm begging to see there are many ways to solve problems that I am facing with code.

This has helped a lot thank you.
maby so
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int>::iterator V_it;
int larg(vector<int>&v);
int main() {
	int n;
	cout<<"Enter the number of elements"<<endl;
	cin>>n;
	vector<int>val;
	for(int i=0; i!=n; i++) {
		int m;
		cin>>m;
		   val.push_back(m);
	}
	for(V_it iter=val.begin();iter!=val.end(); iter++) {
		cout<<*iter<<" ";
	}
	cout<<endl;
	cout<<"Max: "<<larg(val)<<endl;
	return 0;
}
int larg(vector<int>&v) {
	int max=v[0];
	for(V_it i=v.begin(); i!=v.end(); i++)
		if(*i>max) {
			max=*i;
		}
	return max;
}
Last edited on
Topic archived. No new replies allowed.