"Rewrite a the findMax() function in Program 6.5 so that the variable max, declared in main(), is used to store the maximum value of the two passed numbers. The value of max should be set from within findMax().
#include <iostream>
usingnamespace std;
int findMax(int, int, int&);
int main()
{
int firstnum, secnum, max;
cout << "\nEnter a number: ";
cin >> firstnum;
cout << "Great! Please enter a second number: ";
cin >> secnum;
findMax(firstnum, secnum, max);
cout << "\nThe maximum of the two number is " << max << endl;
return 0;
}
int findMax(int x, int y, int& maxnum)
{
if (x >= y)
maxnum = x;
else
maxnum = y;
return;
}
Im not sure why its not compiling. Im getting this error:
G:\cpp>g++ findMaxFunction.cpp -o findMaxFunction.exe
findMaxFunction.cpp: In function `int findMax(int, int, int&)':
findMaxFunction.cpp:29: error: return-statement with no value, in function returning 'int'
So you need to return a value. These are the two options:
1 2 3 4 5 6 7 8 9 10 11 12
void findMax(int x, int y, int& maxnum)
{
// modify maximum
// don't return anything
}
int findMax(int x, int y)
{
int maximum;
// ...
return maximum;
}
Thank you I got it to work. Now Ive been stuck on this random generator problem for a while. Im sorry if im a pain haha. Im just having problems with functions. :
#include <iostream>
usingnamespace std;
void randnum(float, float&);
int main()
{
int num = 0;
float number = 0;
float seed = 0;
cout << "Enter a six-digit integer (not divisible by 2 or 5) : ";
cin >> num;
randnum(seed, number&);
cout << "\nRandom Number: " << number << endl;
return 0;
}
void randnum(float i, float &x)
{
i = int(997.0 * x / 1.e6);
x = 997.0 * x - i * 1.e6;
return;
}
Its using power-residue. Heres the steps:
1.) Have a user enter a six-digit odd integer seed that isnt divisible by 2 or 5
2.) Multiply the seed by the number 997.
3.) Extract the lower six digits of the result produced by step 2. Use this random number as the next seed.
4.) Repeat steps 2 and 3 for as many random numbers as needed.
The actual part im stuck on:
a.) create a funtion named randnum() that accepts a floating-point "seed" as a parameter and returns a floating-point random number between 0 and 1.e6.
Im having a problem getting randnum() to set the value (x) of number in main().