# Function calling

Dear Sir,
While extending a program , I was writing code to calaculate the result , raising ' x ' to power ' y ' . It worked well , when power is a positive integer.
then I extended the program to take ' y ' , to all integers positive , zero and negative.
The problem i am facing is that using a for - else loop , the program is not returning the correct values.
The declarations , and definition of the fuction has been rechecked several times , but could not locate the error.

Plz help me in locationg and correcting the error.
Tahnking you,
ah.
......................................................................
/* 786 */
/* **** Lect_09AA *****/

#include
#include

using namespace std ;

// ***** DECLARATION
double raiseToPower(double , int ) ;

int main()
{
double x ;
int i ;

// ***** PROMPT THE INPUTS
cout <<" Enter the number : " ;
cin >> x ;
cout <<" Enter the power , to which number is raised : " ;
cin >> i ;
cout << "

" ;

cout << " The number "<< x <<" raised to Power "<< i << " = "<< raiseToPower(x , i ) ;

cout << "

" ;
system ("pause") ;
}

// ***** DEFINITION
double raiseToPower(double x , int power )
{
int j ;
double result = 1 ;

if ( power > 0 )
{
for ( j = 1 ; j <= power ; j++ )
result = result * x ;

return result ;
}

else if ( power < 0 )
{
for ( j = -1 ; j <= power ; j--)
result = result * 1 / x ;

return result ;
}
else
{
result= 1 ;
}

}

• : // ***** DEFINITION
: double raiseToPower(double x , int power )
: {
: int j ;
: double result = 1 ;
:
: if ( power > 0 )
: {
: for ( j = 1 ; j <= power ; j++ )
: result = result * x ;
:
: return result ;
: }
:
:
: else if ( power < 0 )
: {
: [green]//for ( j = -1 ; j <= power ; j--)[/green]
[red]for ( j = -1 ; j >= power ; j--)[/red]
: result = result * 1 / x ;
:
: return result ;
: }
: else
: {
: result= 1 ;
: }
:
:
: }

[red]your [blue]main()[/blue] does not return an integer, which is the return type. write [blue]return 0;[/blue] (most common practise) at the end of the [blue]main()[/blue].[/red]

use code tags for post code.
• Or more easy...

x^(-y) = 1 / x^y

So:
[code]
for (int i = 0; i < y; i++)
result = result * x;

return (y > 0 ? result : 1 / result);
[/code]

But it basically does the same...

Greets...
Richard

• : : // ***** DEFINITION
: : double raiseToPower(double x , int power )
: : {
: : int j ;
: : double result = 1 ;
: :
: : if ( power > 0 )
: : {
: : for ( j = 1 ; j <= power ; j++ )
: : result = result * x ;
: :
: : return result ;
: : }
: :
: :
: : else if ( power < 0 )
: : {
: : [green]//for ( j = -1 ; j <= power ; j--)[/green]
: [red]for ( j = -1 ; j >= power ; j--)[/red]
: : result = result * 1 / x ;
: :
: : return result ;
: : }
: : else
: : {
: : result= 1 ;
: : }
: :
: :
: : }
:
: [red]your [blue]main()[/blue] does not return an integer, which is the return type. write [blue]return 0;[/blue] (most common practise) at the end of the [blue]main()[/blue].[/red]
:
: use code tags for post code.
:
********************************************************************
Thanks a lot ,
I worked.
ah.
• : Dear Sir,
: While extending a program , I was writing code to calaculate the result , raising ' x ' to power ' y ' . It worked well , when power is a positive integer.
: then I extended the program to take ' y ' , to all integers positive , zero and negative.
: The problem i am facing is that using a for - else loop , the program is not returning the correct values.
: The declarations , and definition of the fuction has been rechecked several times , but could not locate the error.
:
: Plz help me in locationg and correcting the error.
: Tahnking you,
: ah.

sir,
It may be
: ......................................................................
: /* 786 */
: /* **** Lect_09AA *****/
:
: #include
: #include
:
: using namespace std ;
:
: // ***** DECLARATION
: double raiseToPower(double , int ) ;
:
: int main()
: {
: double x ;
: int i ;
:
:
: // ***** PROMPT THE INPUTS
: cout <<" Enter the number : " ;
: cin >> x ;
: cout <<" Enter the power , to which number is raised : " ;
: cin >> i ;
: cout << "

" ;
:
: cout << " The number "<< x <<" raised to Power "<< i << " = "<< raiseToPower(x , i ) ;
:
: cout << "

" ;
: system ("pause") ;
: }
:
: // ***** DEFINITION
: double raiseToPower(double x , int power )
: {
: int j ;
: double result = 1 ;
:
: if ( power > 0 )
: {
: for ( j = 1 ; j <= power ; j++ )
: result = result * x ;
:
: return result ;
: }
:
:
: else if ( power < 0 )
: {
: for ( j = -1 ; j <= power ; j--)
: result = result * 1 / x ;
:
: return result ;
: }
: else
: {
: result= 1 ;
: }
:
:
: }
:
:

sir,
solution of this may be like this

here is second condition i.e.
else stetement

there is error in "for" loop.

for(j=-1;j<=power;j--)

this "for" loop should be as follows

for(j=-1;j>=power;j--)

bexcause -1 is greater than -2,-3 and so on.