Click here to Skip to main content
15,885,842 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a function f where f(f(x)) = -x (assume x to be an integer), any programming language would do.


What I have tried:

How can I write the following fuction?
Posted
Updated 15-Apr-21 2:25am
Comments
Richard MacCutchan 15-Apr-21 9:34am    
Think about what value of x would give negative x in the case above.

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
You know, you may accomplish this task using a function returning the complex number obtained multiplying x by sqrt(-1). Using other approaches you have to return, together with the integer result, and additional info. There are several ways to do that, the viable one depends on your exact requirements.
 
Share this answer
 
Comments
[no name] 15-Apr-21 15:58pm    
The only good answer here! To make it more easy for OP: Multiply your x by i ;)
CPallini 15-Apr-21 17:13pm    
The problem is: there is not a good answer, unfortunately.
Arvind Patel Apr2021 16-Apr-21 0:27am    
Okay, thanks for the answer
CPallini 16-Apr-21 1:59am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900