Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

About Setfunction

22 views
Skip to first unread message

tharaph...@gmail.com

unread,
Oct 16, 2016, 6:15:38 AM10/16/16
to
Raising a number x to a power y (xy)is the same as multiplying x by itself y times. Write a function called xpowerY() that takes a int value for x and int value for y,and returns the result as a int value. Use a default argument of 3 for y, so that if this argument is omitted, the number x will be cubic. Write a main() function that gets values from the user to test this function.
(Behint the x of y is at the top) extra letter

Alf P. Steinbach

unread,
Oct 16, 2016, 7:13:41 AM10/16/16
to
Easy peasy! :) Your professor will be impressed by this solution:

[code]
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

auto foo1( int const x )
-> int
{ return x + 1; }

auto foo2( int const x )
-> int
{ return x - 1; }

auto foo3( int const x, int const y )
-> int
{ return (x == 0? y: foo1( foo3( foo2( x ), y ))); }

auto foo4( int const x, int const y )
-> int
{ return (x == 0? 0: foo3( foo4( foo2( x ), y ), y )); }

auto foo5( int const x, int const y )
-> int
{ return (y == 0? 1: foo4( x, foo5( x, foo2( y ) ) )); }

auto xpowerY( int const x )
-> int
{ return foo5( x, 3 ); }

auto xpowerY( int const x, int const y )
-> int
{ return foo5( x, y ); }

auto main( int n_args, char** args )
-> int
{
try
{
if( n_args != 3 ) { throw "Uh oh!"; }
cout << xpowerY( stoi( args[1] ), stoi( args[2] ) ) << endl;
return EXIT_SUCCESS;
}
catch( ... )
{
return EXIT_FAILURE;
}
}
[/code]

Invoke it like this:

[example]
[C:\my\forums\clc++\041]
> g++ foo.cpp

[C:\my\forums\clc++\041]
> a 5 3
125

[C:\my\forums\clc++\041]
> _
[/example]


Cheers & hth!,

- Alf

Mr Flibble

unread,
Oct 16, 2016, 3:59:37 PM10/16/16
to
Your coding style makes my eyes hurt and your use of -> on main() is the
egregious icing on your really bad cake.

/Flibble

Wouter van Ooijen

unread,
Oct 16, 2016, 4:12:45 PM10/16/16
to
Op 16-Oct-16 om 9:59 PM schreef Mr Flibble:
In this specific context that is high praise :)

Wouter "Objects? No Thanks!" van Ooijen

Öö Tiib

unread,
Oct 16, 2016, 4:44:47 PM10/16/16
to
Yes! However function names like 'fun1', 'fun2' are likely more enjoyable,
lively and pleasant to professor than the odd 'foo' used. ;)

Ian Collins

unread,
Oct 17, 2016, 12:10:31 AM10/17/16
to
On 10/17/16 08:59 AM, Mr Flibble wrote:
>
> Your coding style makes my eyes hurt and your use of -> on main() is the
> egregious icing on your really bad cake.

+1

--
Ian
0 new messages