I am new to programming and am wondering if someone can help me with this code...
Please implement concisely and efficiently the following function overloading (in red) which calculates the minimum of different numbers of inputs. OUTPUT all the results into an output file, out.txt.
Here is my source code to change... can anyone help me with this??
//overmin.cpp
#include <iostream>
using namespace std;
// Function Prototypes
//The following are all considered by the compiler
//to be different functions.
int min(int, int, int);
int min(int, int);
int min(int, int, int, int);
int min(int, int, int, int, int);
// declare the outfile out.txt here
int min (int x, int y){
if (x>y)
return y;
return x;
}
int min(int, int, int) {…} // Hint: set min to be the first parameter, etc. for concise logic
int min(int, int, int, int){..}
int min(int, int, int, int, int){…}
int min (int x, int y){
if (x>y)
return y;
return x;
}
int min (int x, int y, int z){
if (x<y){
if (x<z)
return x;
else
return z;
}
else{
if (y<z)
return y;
else
return z;
}
}
int min (int x, int y, int z, int r){
if (x<y){
if (x<z)
return x;
else
return z;
}
else{
if (y<z){
if (y<r)
return y;
else
return r;
}
else{
if (r<x){
if (r<z)
return r;
else
return z;
}
}}}
Avoid complicated else/if chains. it's hard to make sense of your min functions because they're such a large tangled mess. That's also why you're having difficulty with the last function.
Instead.... don't be afraid to make new variables. For example, in each function you could have a "lowest" variable. If a number is lower than the lowest, then it becomes the new lowest.