Also, I am trying to add another separate function that takes 3 parameters of floats and returns the biggest number. I know I want to use VOID. Any thoughts? So far I have this:
#include <iostream>
#include <time.h>
#include <stdlib.h>
usingnamespace std;
float biggestNumber(float , float , float ); // Function2 prototype
int main ()
{
cout << biggestNumber(1,2,3) << '\n';
cout << biggestNumber(1.2,2.1,-3.7) << '\n';
return 0;
}
float biggestNumber(float num1, float num2, float num3) // Function2 definition
{
float max = 0;
if (num1 > max)
max = num1;
if (num2 > max)
max = num2;
if (num3 > max)
max = num3;
return max;
}
A couple of things.
It's important the function declaration, function prototype and function definition have compatible types for variables including return value.
The cascade of if statements whether they are complicated like this previous one or even the simple way I have shown are only good for a couple of numbers before it gets out of hand. So arrays, which you may not have come across yet, are something to look forward to.
#include <iostream>
#include <time.h>
#include <stdlib.h>
usingnamespace std;
// Function 1
int randomNumber(int val1, int val2); // Function 1 prototype
// Function 2
float biggestNumber(float num1, float num2 , float num3 ); // Function 2 prototype
int main ()
{
// Function 1 random number from 11 to 23 inclusive
srand((int)time(NULL));
cout << randomNumber(11, 23) << '\n';
// Function 2 finds the biggest number of 3 values
float largest = biggestNumber(1.2,2.1,3.7);
cout << "Largest is " << largest << endl;
// Function 1 return
return rand()%(val2 - val1 + 1) + val1;
return 0;
}
int randomNumber(int val1, int val2) // Function 1 definition
float biggestNumber(float num1, float num2, float num3) // Function 2 definition
{
// Function 2 return
float max = 0;
if (num1 > max)
max = num1;
if (num2 > max)
max = num2;
if (num3 > max)
max = num3;
return max;
}
Also, I am trying to add another separate function that takes 3 parameters of floats and returns the biggest number. I know I want to use VOID.
A return type of void means "this function returns nothing" so that preference is at odds with the description of what you want to do. One approach to doing this is to define a function that returns the larger of 2 values, and use that to implement the function that returns the larger of 3 values. This approach is more scalable than those presented so far. In fact, given an array of arbitrary length, using only the function that returns the larger of 2 values, we can return the largest value in the array. It also illustrates the divide-and-conquer approach that is common to many algorithmic solutions.
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
// max of 2
float maxOf(float a, float b) { return a > b ? a : b; }
// max of 3 - uses max of 2.
float maxOf(float a, float b, float c) { return maxOf(maxOf(a, b), c); }
int main()
{
std::cout << "Largest is " << maxOf(1.2, 2.1, 3.7) << '\n';
}
#include <iostream>
#include <time.h>
#include <stdlib.h>
usingnamespace std;
// Function 1
int randomNumber(int val1, int val2); // Function 1 prototype
// Function 2
float biggestNumber(float num1, float num2 , float num3 ); // Function 2 prototype
int main ()
{
// Function 1 random number from 11 to 23 inclusive
srand((int)time(NULL));
cout << randomNumber(11, 23) << '\n';
// Function 2 finds the biggest number of 3 values
float largest = biggestNumber(1.2,2.1,3.7);
cout << "Largest is " << largest << endl;
// Function 1 return
return rand()%(val2 - val1 + 1) + val1;
return 0;
}
int randomNumber(int val1, int val2) // Function 1 definition
float biggestNumber(float num1, float num2, float num3) // Function 2 definition
{
// Function 2 return
float max = 0;
if (num1 > max)
max = num1;
if (num2 > max)
max = num2;
if (num3 > max)
max = num3;
return max;
}