Thanks to all those who have helped me on this forum thus far; now that I'm registered and the program is coming along I thought it would make it easier for any would-be benefactors to start my problem anew. I'm trying to write a program which inputs one floating point and one integer from the user, then sends these to a function which calculates the floating point to the power of the integer. There are no longer any linking errors, but the program just does base * base no matter what the power is (apart for power 0 and power 1, which have been written in seperately and work fine). Please see my program below...
float raise_float (float float_num, int int_num)
{float ans;
int count;
if (int_num == 0)
ans = 1;
else if (int_num == 1)
ans = float_num;
else
for (count=2; count<=int_num; count++)
(float_num*float_num, &ans);
return(ans);
}
As mentioned before am having a lot of trouble getting the function to work properly, I can sort of see why it's not but I'm not sure how to make it right. If any kind soul floating around here could lend a hand I'd be extremely grateful. Thanks again, chris
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hmmm, thanks for the effort guys, but I reckon I might be a lost cause. I'm thinking of just doing a switch statement for powers up to 20 or so (unless I have the stamina to do 999). And to answer your collective wtf?s, my unique grasp of c comes from hardly ever turning up to the lectures on the subject, and occasionally attempting to play catch-up using my text book. It's been ingrained into me how to do basic programs using printf, scanf, and assigning variables etc; but as soon as things get more tricky its painstaking trial-and-error. I basically can't see how to do the cumulative multiply AND stitch the answer back into the program (thanks for the example wayne, but trying to use something like it just ended up with giving nonsense answers for some reason, instead of the wrong numerical answers I was getting before). Thanks anyway all those helpful strangers. chris.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
We went through a stretch a few months ago where people would post code
that had a weird combination of "cute" logic with much lower level mistakes
in it - at the time I wondered if there was a teacher out there who was
giving assignments to "fix" code samples.
Its just hard to imagine someone who writes code with these tricks would
have problems mastering a loop with an cumulative multiply, i.e something like
y = 1;
for (i=1; i <= n; i++)
{
y *= x;
}
Wayne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks to all those who have helped me on this forum thus far; now that I'm registered and the program is coming along I thought it would make it easier for any would-be benefactors to start my problem anew. I'm trying to write a program which inputs one floating point and one integer from the user, then sends these to a function which calculates the floating point to the power of the integer. There are no longer any linking errors, but the program just does base * base no matter what the power is (apart for power 0 and power 1, which have been written in seperately and work fine). Please see my program below...
include <stdio.h>
float raise_float(float, int);
int main (void)
{
float float_num;
int int_num;
}
float raise_float (float float_num, int int_num)
{float ans;
int count;
}
As mentioned before am having a lot of trouble getting the function to work properly, I can sort of see why it's not but I'm not sure how to make it right. If any kind soul floating around here could lend a hand I'd be extremely grateful. Thanks again, chris
Hmmm, thanks for the effort guys, but I reckon I might be a lost cause. I'm thinking of just doing a switch statement for powers up to 20 or so (unless I have the stamina to do 999). And to answer your collective wtf?s, my unique grasp of c comes from hardly ever turning up to the lectures on the subject, and occasionally attempting to play catch-up using my text book. It's been ingrained into me how to do basic programs using printf, scanf, and assigning variables etc; but as soon as things get more tricky its painstaking trial-and-error. I basically can't see how to do the cumulative multiply AND stitch the answer back into the program (thanks for the example wayne, but trying to use something like it just ended up with giving nonsense answers for some reason, instead of the wrong numerical answers I was getting before). Thanks anyway all those helpful strangers. chris.
O_o
"(float_num*float_num, &ans);"
Exactly what do you imagine that does?
Soma
I was wondering that myself. I figured it was just some weird syntax I'd never seen before. How did it even compile?
It only makes use of the comma operator, but it isn't the syntax that is problematic; the logic is the issue.
Soma
Interesting mix of obfuscation, "cleverness" and newbie level logic errors.
I have accidentally used the comma operator a few times, when hopping between
Python and C array notation.
Wayne
We went through a stretch a few months ago where people would post code
that had a weird combination of "cute" logic with much lower level mistakes
in it - at the time I wondered if there was a teacher out there who was
giving assignments to "fix" code samples.
Its just hard to imagine someone who writes code with these tricks would
have problems mastering a loop with an cumulative multiply, i.e something like
y = 1;
for (i=1; i <= n; i++)
{
y *= x;
}
Wayne
LOL, what's up with counting from 1 to n like a human being? I almost forgot you could do that. I only know how to count from zero any more.
I get burned about once a week at work dealing with 3D data where the
vertex numbering starts at 1....
Waynebozo