LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 06-10-2013, 12:46 AM   #16
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319

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
 
1 members found this post helpful.
Old 06-10-2013, 09:18 AM   #17
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by Beryllos View Post
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);

Code:
double foo (double X, double Y)
{
     if(Y <= 0)
          return 1;
     else
          return X * foo (X, Y-1);
}
 
Old 06-10-2013, 09:31 AM   #18
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Looks good to me, but I didn't test it.
You should test it.
When you think you might have the right answer, don't ask us. Try it.
 
1 members found this post helpful.
Old 06-10-2013, 09:53 AM   #19
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Yep. What johnsfine said.

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?" ;-)

Last edited by Beryllos; 06-10-2013 at 10:05 AM.
 
1 members found this post helpful.
Old 06-10-2013, 10:08 AM   #20
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
yes everyone on this thread who helped will get those . ill be home to test this in a bit. looking forward to it.
 
Old 06-10-2013, 10:40 AM   #21
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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$
 
Old 06-11-2013, 03:37 PM   #22
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Great!
 
Old 06-13-2013, 04:57 PM   #23
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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.

still a long ways to go .

Last edited by lleb; 06-13-2013 at 04:59 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to fully understand Linux and its advantages,Capabilities, etc. KalydedecoMCE LinuxQuestions.org Member Intro 0 05-24-2013 07:03 AM
I dont understand this! zeldafanfreak Linux - Hardware 1 08-12-2005 02:52 PM
I dont understand blacktattoo Linux - Newbie 1 12-26-2004 08:41 PM
i dont really understand this. Brain Drop General 3 08-18-2003 08:54 PM
Things dont work when you dont understand withoutaclue Linux - Newbie 3 03-12-2003 09:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration