rainwater_stillicide

Challange: BASIC002

9 posts in this topic

Write a function f which takes an int as an argument and where f(f(x)) = -x

eg.

my_function( my_function ( 5 ) ) = -5

You can use any language you like. Feel free to post any partial solutions you come up with also.

0

Share this post


Link to post
Share on other sites

Write a function f which takes an int as an argument and where f(f(x)) = -x

eg.

my_function( my_function ( 5 ) ) = -5

You can use any language you like. Feel free to post any partial solutions you come up with also.

Here my C++ solution.

Excuse me, but I've solved my problem and I've posted the correct solution in the same post to take clear the topic!!:)


#include <iostream>
using namespace std;
int opposite ( int num ) ;

int main()
{
int n;
cout << "Insert a number " << endl;
cin >> n;

cout << "Opposite number = " << opposite ( n ) << endl;

return 0;

}

int opposite ( int num )
{
num =~ num;

return num+1;
}

Edited by AlexZ
0

Share this post


Link to post
Share on other sites

Write a function f which takes an int as an argument and where f(f(x)) = -x

eg.

my_function( my_function ( 5 ) ) = -5

You can use any language you like. Feel free to post any partial solutions you come up with also.

Here my C++ solution.

Excuse me, but I've solved my problem and I've posted the correct solution in the same post to take clear the topic!!:)


#include <iostream>
using namespace std;
int opposite ( int num ) ;

int main()
{
int n;
cout << "Insert a number " << endl;
cin >> n;

cout << "Opposite number = " << opposite ( n ) << endl;

return 0;

}

int opposite ( int num )
{
num =~ num;

return num+1;
}

I'm afraid that is not correct. You are only applying opposite once, the challenge is to write a function which you apply twice to get the negative of the original argument:

opposite ( opposite ( x ) ) = -x

0

Share this post


Link to post
Share on other sites

Write a function f which takes an int as an argument and where f(f(x)) = -x

eg.

my_function( my_function ( 5 ) ) = -5

You can use any language you like. Feel free to post any partial solutions you come up with also.

Here my C++ solution.

Excuse me, but I've solved my problem and I've posted the correct solution in the same post to take clear the topic!!:)


#include <iostream>
using namespace std;
int opposite ( int num ) ;

int main()
{
int n;
cout << "Insert a number " << endl;
cin >> n;

cout << "Opposite number = " << opposite ( n ) << endl;

return 0;

}

int opposite ( int num )
{
num =~ num;

return num+1;
}

I'm afraid that is not correct. You are only applying opposite once, the challenge is to write a function which you apply twice to get the negative of the original argument:

opposite ( opposite ( x ) ) = -x

Oh yes you're right!!Sorry...I'll try to do another function, but can you explain me better your exercise?

EDIT:

But this code works fine just like you've suggested to me


#include <iostream>
using namespace std;
int opposite ( int num ) ;

int main()
{
int n;
cout << "Insert a number " << endl;
cin >> n;

cout << "Opposite number = " << opposite ( opposite(n) ) << endl;

return 0;

}

int opposite ( int num )
{
if ( num > 0 )
{
num =~ num;
return num+1;
}
else { return num; }
}

Edited by AlexZ
0

Share this post


Link to post
Share on other sites

well if python worked with imaginary numbers:


import math
i = math.sqrt(-1)
def doubinv(num):
num = num*i
x = input()
print doubinv(doubinv(x))

but it doesn't... :-(

edit: here's a cheaty, non-math way to do it... also python


import math
i = 0
def doubinv(num):
i += 1
if (i = 2):
num = num*-1
x = input()
print doubinv(doubinv(x))

Edited by JBu92
0

Share this post


Link to post
Share on other sites

Here is my solution in C.


#include <stdio.h>
#include <math.h>

int inverso( int num ) {
return - fabs(num);
}

int main(void) {
int num_in, num_out;

num_in = 0;
num_out = 0;
printf("Input Number: ");
scanf("%d", &num_in);
num_out = inverso(inverso(5));
printf("Opposite: %d\n", num_out);

return(0);
}

0

Share this post


Link to post
Share on other sites

Here is my solution in C.


#include <stdio.h>
#include <math.h>

int inverso( int num ) {
return - fabs(num);
}

int main(void) {
int num_in, num_out;

num_in = 0;
num_out = 0;
printf("Input Number: ");
scanf("%d", &num_in);
num_out = inverso(inverso(5));
printf("Opposite: %d\n", num_out);

return(0);
}

counterexample: -1

0

Share this post


Link to post
Share on other sites

Here's mine. :)

// File: main.cpp
// Title: BinRev BASIC002
// Description: Write a function f which takes an int as an argument and where
// f(f(x)) = -x
// Author: codered22
// Date: 2010-10-23

#include<iostream>

using namespace std;

int getNegative(int _num) {
if (_num <= 0)
return _num;
else
return _num * -1;
}

int main() {
int x;

cin >> x;

cout << getNegative(getNegative(x));

return 0;
}

0

Share this post


Link to post
Share on other sites


$inverse = inverse($number);
sub inverse() {
shift;
$return_number = $_ * -1;
return $retun_number;
}

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now