• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

So...CS people...how can a function return value be on the lhs of an assignment?

beer

Lifer
This is a question for a test I have coming up tonight:

Problem 4. Suppose x is an array of n double values declared by the following statements.
double x[100];
int n;
Write the definition of the function Maximum() that will put the component with the maximum
value to 17.1 with the call
Maximum(x, n) = 17.1;
You may assume 0 < n < 101. In the case of ties for the maximum value, the component with
the smallest subscript should be changed.

The only way that I have been able that this wuold work was if I had Maximum() return a pointer and then dereference it to assign 17.1 to it. But I can't do that. Obviously I can't just return a double, I have to return a double pointer, but when I return the pointer to the highest value it fails to compile with the error, 'not an lvalue.' Is this possible?
 
Originally posted by: agnitrate
I'm not sure if that's even possible. Kinda pointless even if it was though IMHO.

-silver

Our test are all about pointless stuff. That's the point! (pun intended)

This is in C++. Sorry, forgot to specify.
 
Originally posted by: DaveSimmons
A function can return a reference, a pointer, or a value / object.

Of which all give me the error, 'not an lvalue' when I compile. Which means they belong on the rhs of an assignment, not the lhs, which this problem is asking.

 
Two of them will. Just tried the third and it compiled without errors.

edit: Why not post your code skeleton for the two types, pehaps you're making a mistake in your function declaration.
 
You have to return a reference instead of a pointer. If your function already works when you do this: *Maximum(x,n) = 17.1, all you have to do is change it to return a double&, and then dereference the pointer to the max value before returning it.
 
Originally posted by: Venix
You have to return a reference instead of a pointer. If your function already works when you do this: *Maximum(x,n) = 17.1, all you have to do is change it to return a double&, and then dereference the pointer to the max value before returning it.
Yes, if beer got an error compiling there's something wrong with his function declaration.
 
in c++.. overload ( ) to [ ] to make the code cryptic... it will work but is really dumb
in any other language... i have no f'in idea
 
Back
Top