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).
#include <iostream>
usingnamespace 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:
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;
}
elseif (y > w && y > x && y > z)
{
return y;
}
elseif (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.
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:
@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.
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.
#include <iostream>
int find_largest(int array[], constint size);
int main()
{
constint 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[], constint size)
{
int largest = array[0];
for (size_t loop = 1; loop < size; loop++)
{
if (largest < array[loop])
{
largest = array[loop];
}
}
return largest;
}
#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.