an other C question. I dont fully understand this bit of code
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
There are still problems, keep trying, sorry I can't just give you the code:
incorrect number of arguments in foo(Y-1)
incorrect termination condition in if(Y <= 1)
incorrect recursion formula X * ( X * foo (Y-1))
Review these hints:
recursion formula is x^y=x*(x^(y-1))
recursion will terminate with the calculation x^0=1
no its good, this is a good learning experience, and more what im after then just being force fed code i dont comprehend.
hmm, foo requires 2 args, so would foo (X, Y-1) work?
if ( Y = 0) return 1;
for recursion, i suppose that puts me back at the original of X * foo (X, Y-1);
and now you can see how the value of X is being multiplied by itself.
The termination condition (Y==0) would also be acceptable, but I like your choice of (Y<=0) because it will stop recursion if invalid Y is entered. (With negative or non-integer Y, the condition Y==0 will never be met, and the recursion would go on and on, until it uses up all available memory. Yikes!)
P.S. For this, you are going to owe me some clicks on "Did you find this post helpful?" ;-)
yeah thanks all. here is the completed code with the proper recursive bit instead of what i had earlier:
Code:
//4. Write a complete C program that uses a recursive function to calculate the value of x^y
// x and y should be inputted by the user and the result should be displayed.
//
//##########################################################
// Created by Raymond L. Brunkow
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 or version 3 of the
// license, at your option.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//##########################################################
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double Power(double, double);
int main(void)
{
// set veriables
double x, y, result;
// getting information from user
printf("\nThis program will calculate a simple exponent in the form of x^y\n");
printf("\nPlease enter the (x) value: ");
scanf("%lf", &x);
printf("\nPlease enter the (y) value: ");
scanf("%lf", &y);
printf("\nYou have entered %.2f^%.2f ",x, y);
// setting result to the value calculated by my function Power
result = Power(x,y);
printf("\nThe value of %.2f^%.2f is %.2f \n", x, y, result);
sleep(5);
return(0);
}
//double Power(double X, double Y)
//{
// setting local variables
// double ANS=1, i=1;
//
// while (i<=Y)
// {
// ANS=ANS*X;
// i++;
// }
//
// return ANS;
//}
double Power(double X, double Y)
{
if(Y <=0 )
return 1;
else
return X * Power(X,Y-1);
}
works as expected:
Code:
user-imac:ENG-3211 user$ gcc -o hw_2_3 hw_2_3.c
user-imac:ENG-3211 user$ ./hw_2_3 3
This program will calculate a simple exponent in the form of x^y
Please enter the (x) value: 8
Please enter the (y) value: 2
You have entered 8.00^2.00
The value of 8.00^2.00 is 64.00
user-imac:ENG-3211 user$ ./hw_2_3 3
This program will calculate a simple exponent in the form of x^y
Please enter the (x) value: 2
Please enter the (y) value: 3
You have entered 2.00^3.00
The value of 2.00^3.00 is 8.00
user-imac:ENG-3211 user$
yup, simple little project, but with your guys help i now have a much better understanding of what im doing. that is great. thank you again.
one thing that was tossing me for a loop was the fact that you can manipulate the function variable inside the function, the Power(X,Y-1) was what i was having the hardest time wrapping my head around. now that i see that it can, and should be done that way things are a bit more clear.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.