LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-07-2013, 07:23 PM   #1
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
an other C question. I dont fully understand this bit of code


Thanks again for the help. I have working code, but I am not 100% sure exactly what it is doing. Here is the code:

Code:
double Power(double X, double Y)
{
	// setting local variables
	double ANS=1, i=1;

	while (i<=Y)
	{
		ANS=ANS*X;
		i++;
	}

	return ANS;
}
This is my user defined function for calculating x^y.

why do I have to set ANS and i both = 1?

also with i++ Im guessing that it is multiplying ANS*X again and again until we get to the value of Y? Or is it subtracting the value of Y each time it goes through the while statement?

This is where Im confused...

Yes it works, but i want to understand more of the why. thanks.

P.S. yes i know there is a pow(double, double) in the math.h, this is for learning.
 
Old 06-08-2013, 12:07 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
1. Uninitialised numeric vars default to 0
2. i++ is incrementing i; equiv to 'i=i+1', so yes, its multiplying the result over & over until we've done it enough times.
3. a simple way to analyse/debug C (or any lang) is to add print statements to print out the vars until you understand.
Obviously use low values for the loop limit var ( 'Y' ), so you don't get swamped.

HTH
Good book
http://users.soe.ucsc.edu/~pohl/abc4.html
 
1 members found this post helpful.
Old 06-08-2013, 12:32 AM   #3
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 chrism01 View Post
1. Uninitialised numeric vars default to 0
2. i++ is incrementing i; equiv to 'i=i+1', so yes, its multiplying the result over & over until we've done it enough times.
3. a simple way to analyse/debug C (or any lang) is to add print statements to print out the vars until you understand.
Obviously use low values for the loop limit var ( 'Y' ), so you don't get swamped.

HTH
Good book
http://users.soe.ucsc.edu/~pohl/abc4.html
sorry, im lost on the ' a simple way to analyse/debug C (or any lang) is to add print statements to print out the vars until you understand.'

printf? how do i print out what is going on? sorry, have not spent much time looking for C debugging on MAC OSx 10.6 tis what im programming on.
 
Old 06-08-2013, 01:04 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by chrism01 View Post
1. Uninitialised numeric vars default to 0
True for uninitialized static and global variables. Dynamic local variables in functions, like the variables in question here, are allocated space on the stack, and are initialized to whatever garbage was in that memory location prior to the function being called. Using such variables without initializing them is a common source of hard-to-reproduce failures.
 
Old 06-08-2013, 01:13 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by lleb View Post
I have working code, but I am not 100% sure exactly what it is doing.
Your questions show you don't understand any significant aspect of this code.

Maybe you need to start with a simpler example, and probably you need to read a little about C syntax.

Quote:
also with i++ Im guessing that it is multiplying ANS*X again and again until we get to the value of Y? Or is it subtracting the value of Y each time it goes through the while statement?
Where do you see anything that looks like subtracting Y?
 
1 members found this post helpful.
Old 06-08-2013, 01:39 PM   #6
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
lleb, How are you learning C programming? Are you taking a course, teaching yourself by an introductory C/C++ programming book, or going about it some other way? Like johnsfine, I am puzzled about the level of your questions, and we want you to learn efficiently and have fun with it.

Last edited by Beryllos; 06-08-2013 at 01:40 PM.
 
Old 06-09-2013, 02:57 PM   #7
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 johnsfine View Post
Your questions show you don't understand any significant aspect of this code.

Maybe you need to start with a simpler example, and probably you need to read a little about C syntax.



Where do you see anything that looks like subtracting Y?
i dont and that is my question. how does i++ know when it has reached the "number" that is equal to Y?
 
Old 06-09-2013, 03:05 PM   #8
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
lleb, How are you learning C programming? Are you taking a course, teaching yourself by an introductory C/C++ programming book, or going about it some other way? Like johnsfine, I am puzzled about the level of your questions, and we want you to learn efficiently and have fun with it.
a combination of both. I am in a class for C atm, but the professor is not always clear on how/why things do what they do. We just started learning about user defined functions in class. one of the homework problems is to create a function that "recursively" (sadly mine is not recursive at all) performs the power function without using pow(x,y);

using google i found something very simular to the code I created, but as -- ++ and other, for lack of a better word, 'functions' have not been fully explained to us by the prof.

we did an example with n! for factorials, that used n-1 like below, but i was unable to convert that example into something usable for the power function:

Code:
double factorial_N (double n)
{
     if (n <= 1)
          return 1;
     else
          return n * factorial_N (n-1);
}
so here the prof. created a user defined function that calls itself until it returns a 1 as a value, then creates the proper product.

i have a basic comprehension on howto create my own functions and use them. I was able to successfully do so with the other homework problems and even resolve a typo I had made by getting the input variables backwards in my function itself. easy enough to fix, but this recursive example has me more then a little stumped.

i am not asking for the answer in the form of code as that is both wrong AND against forum rules, i am looking for a better explanation or better examples of what it appears the prof. is looking for.
 
Old 06-09-2013, 04:21 PM   #9
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
Originally Posted by lleb View Post
i dont and that is my question. how does i++ know when it has reached the "number" that is equal to Y?
In your program,
Code:
i++;
accomplishes the same thing as
Code:
i=i+1;
In other words, it increments i.

Likewise,
Code:
i--;
decrements i, the same as
Code:
i=i-1;
The way the program "knows" when i reaches Y is by the conditional i<=Y in the while statement. The while statement executes the code enclosed in the braces as long as the conditional is true.

By the way, which language are you programming in, C or C++?
 
1 members found this post helpful.
Old 06-09-2013, 04:28 PM   #10
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
Originally Posted by lleb View Post
one of the homework problems is to create a function that "recursively" (sadly mine is not recursive at all) performs the power function without using pow(x,y);
I hope you don't mind me giving you a hint, though I will not supply code:
The factorial function can be expressed recursively as
Code:
n!=n*(n-1)!
The power function can be expressed recursively as
Code:
x^y=x*(x^(y-1))
 
1 members found this post helpful.
Old 06-09-2013, 07:09 PM   #11
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
thank you both, that is starting to clear things up. I am working in C, not C++ sorry if that was not clear.

Beryllos, so with n! = n*(n-1) that is what the function is i typed earlier that calls on itself until n<=1, then it stops and returns 1 as both 1! and 0! = 1


now with the x^y example would this be on the right track?

Code:
double foo (double X, double Y)
{
     if(Y <= 1)
          return 1;
     else
          return X * foo (Y-1);
}
that just looks wrong, but is it at least on the right track?
 
Old 06-09-2013, 08:53 PM   #12
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
You are getting closer. Make sure the recursive function call (the one inside the function) has the correct arguments. Also the final calculation will be x^0=1.

Here is a fine point that you probably don't have to worry about, since this is just a schoolbook recursive-function exercise: This function will fail for X<0 or Y<0 or non-integer Y. If we were developing a power function for scientific calculations, we would have to test for these conditions and process them differently.
 
1 members found this post helpful.
Old 06-09-2013, 10:08 PM   #13
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
You are getting closer. Make sure the recursive function call (the one inside the function) has the correct arguments. Also the final calculation will be x^0=1.

Here is a fine point that you probably don't have to worry about, since this is just a schoolbook recursive-function exercise: This function will fail for X<0 or Y<0 or non-integer Y. If we were developing a power function for scientific calculations, we would have to test for these conditions and process them differently.
true, can not use mine for a x^(1/2)

when you say i have the wrong argument...im not seeing that i have the wrong one in order, but i could be way off base here...

what bothers me is that the value of X is not being multiplied by itself at least not as far as i can see. that is just one of my confusions *smiles*
 
Old 06-09-2013, 10:16 PM   #14
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
compare the number of arguments:
Quote:
Originally Posted by lleb View Post
Code:
double foo (double X, double Y)
{
     if(Y <= 1)
          return 1;
     else
          return X * foo (Y-1);
}
 
1 members found this post helpful.
Old 06-09-2013, 11:27 PM   #15
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
Code:
double foo (double X, double Y)
{
     if(Y <= 1)
          return 1;
     else
          return X * ( X * foo (Y-1));
}
that looks a bit more like math to me, but i still dont entirely get the recursion of this.
 
  


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 11:18 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