This is for extra credit, please do not give me the code. If you can just point me in the right direction, tell me where to look, etc.
I am writing a program to display minimum and maximum. I am not getting compilation errors, however it is not behaving as it is supposed to. It needs to get a number from user until the user enters zero, and then display the minimum and maximum of the numbers entered.
Questions:
I know that a function can only return one value. However if using reference parameters, can return more than one. Correct?
#include <iostream>
usingnamespace std;
//declare functions
float getNum(float&);
float MinMax(float&);
float MinMax(float&);
void MinMax(float, float);
int main()
{
float num, max, reset, min;
cout << "Enter number (0 to stop): " << endl;
do {
//get number from user
getNum(number);
//initialize min and max
//Can't do this right? I am not sure how to get this to return the two values.
max = MinMax(num);
min = MinMax(num);
} while (num != 0);
//reset minimum and maximum
//Don't think I am doing this right either.
reset = MinMax(num);
//display minimum and maximum
MinMax(max, min);
return 0 ;
}
//*************************getNum*******************************
float getNum(float& num) {
cin >> num;
}
//*************************MinMax******************************
float MinMax(float& init) {
float max, min;
if (initial == 0)
{ cout << "No number given - exiting. " << endl;
return 0 ;
}
max = initial;
min = initial;
return max;
return min;
}
//*************************MinMax******************************
float MinMax(float& update) {
float max, min;
if (reset != 0) {
if (update > max)
max = reset;
if (reset < min)
min = reset;
}
return reset;
}
//*************************MinMax******************************
void MinMax(float maximum, float minimum) {
cout << "Maximum is: " << max << endl;
cout << "Minimum is: " << min<< endl;
}
float initMinMax(float& initial) {
float max, min;
if (initial == 0) // user may enter zero as the first number
{ cout << "No number given. Program terminated. " << endl;
return 0 ;
}
max = initial; // since only one number is entered so far, it is taken
min = initial; // as minimum as well as maximum;
return max;
return min;
}
this will only return max. because it reaches this first. function can only return 1 value. if you want multiple values, maybe use a structure.
We have not used or gone over struct/class, arrays or typedef. I cannot any of those.
The instructor specifically required that zero be the terminating condition for the loop. The user must enter zero to stop the program, so wrong or not, it must stay in.
So, given the above, even if it is more work, how would I go about returning min and max? Should they be different functions, that way I can return them separately? For example, initMin() and initMax()? Or am I thinking about this incorrectly?
Perhaps I will just give it a try anyway, and if someone responds with a better approach, I will scrap it/modify it.
note: you will need to use a different loop. e.g. instead of do/while just use while.
1 2 3 4
void GetNum(float &num); //Note this returns void but takes a reference as it's parameter...
void minimum(float num, float &min) //Note, the first param passes by value (so no changes), param 2 is by reference (expects a change)
void maximum(float num, float &max)//Note, the first param passes by value (so no changes), param 2 is by reference (expects a change)
//Both min and max functions return (void)
@clanmjc,
Thank you for the tips, as my last attempt at dividing them up didn't work.
I am pretty confused on reference parameters. When I look in my book it says that reference parameters are for output or "in/out". So when you pass something by reference, there is a change that is being made to the data?
Passing by value means there is no change to the data, it is simply being "caught"/copied by/to the named parameters in the definition?
Just to be sure I am understanding the terminology in the book and in my notes:
Prototypes are another name for functions?
Arguments are in the function call?
Parameters are in the function declaration?
I will try the above tips and post back with any issues.
The reason I pointed out which parameters were "by value" and "by reference" was to hint to you that when you are implementing those function you will want to do something... or nothing... to the parameters.
I understand why you pointed it out - was just trying to get confirmation on the terminology as well as whether or not my interpretation of them, from class and from my book, are correct.
I was actually interrupted during my response so I had to quickly send it out.
1>
Passing by value means there is no change to the data, it is simply being "caught"/copied by/to the named parameters in the definition?
By value means, you are making a copy, always a copy. Anything you do to a copy is irrelevant when it comes to the original.
Pass by value when - you are using primitive types and they do not need to be changed.
2>
Prototypes are another name for functions?
Prototype is the function declaration.
Prototype/Declaration: int function(int one, int two);
Definition:
1 2 3 4
int function(int one, int two)
{
return one + two;
}
Arguments:
1 2
//... other code...
int result = function(4,6); //4 and 6 are arguments.
Honestly, arguments and parameters are interchangeable. If for testing purposes you are required to distinguish them as you have stated then I would remember them for the test, but just to make sure for honesty's sake, I'm looking in "The C++ Programming language Special Edition" by Bjarne Stroustrup right now and he uses them interchangeably. I would accept either usage during a design meeting myself. There may be others that have to have it more verbose, I'm not one of them.