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.
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.
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.
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.
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.
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?
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.
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.
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++?
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
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?
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.
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*
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.