--Originally published at prgrm.co
In this week’s quiz, the goal was to ask the user for three different numbers and then make two functions. The first function was to display the lowest of the three numbers, and the second one was to add the squares of the three numbers.
In order to accomplish this, I needed to include three different libraries.
- iostream
- cmath
- algorithm
Then the making of the functions, the first one which is to find the lowest number looks like this:
int min3(int x, int y, int z){ return min(min(x, y), z); }
Notice the way the “return min(min(x , y), z)” is written, this is due the fact that the “min” function works only for two integers, so by repeating the “min” function first it chooses between “x” & “y” and then between the result from this later & “z”.
The second function is to add the squares of the three numbers, this one is relatively easier as the only thing you need to do is multiply and add, like so:
int sumofsquares(int x, int y, int z){ return ((x*x) + (y*y) + (z*z)); }
Here’s the full code:
#include <iostream> #include <algorithm> #include <cmath> using namespace std; int min3(int x, int y, int z){ return min(min(x, y), z); } int sumofsquares(int x, int y, int z){ return ((x*x) + (y*y) + (z*z)); } int main(){ int no1, no2, no3; cout << "This program will give you the lowest number and the sum of squares of the three numbers asked." << endl; cout << "First: "; cin >> no1; cout <<"Second: "; cin >> no2; cout << "Third: "; cin >> no3; cout << "The smallest of the three numbers is " << min3(no1, no2, no3) << " ,the sum of the squares is " << sumofsquares(no1, no2, no3) << Continue reading "Quiz 04"