Is there any way to make this function more elegant? I'm new to C++, I don't know if there is a more standardized way to do this. Can this be turned into a loop so the number of variables isn't restricted as with my code?
float smallest(int x, int y, int z) {
int smallest = 99999;
if (x < smallest)
smallest=x;
if (y < smallest)
smallest=y;
if(z < smallest)
smallest=z;
return smallest;
}
A small modification
You can store them in a vector and use
std::min_element
on that.For example:
Or you can just use define, to create a macro function.
There's a number of improvements that can be made.
You could use standard functions to make it clearer:
Or better still, as pointed out in the comments:
If you want it to operate on any number of ints, you could do something like this:
You could also make it generic so that it'll operate on any type, instead of just ints