Ok so where in the code can i put that? I don't know where i could put it in... GAH confusing!
Ok so where in the code can i put that? I don't know where i could put it in... GAH confusing!
Recursion could solve the problem, right? I mean if this were a function that took in an x and n parameter, you could just say something like:
Example:
Code:Function MyPow (double x, double n) double p = 1.0; int i; if(n < 0) return 1.0/MyPow(x, -n); else if(n) for(i = 0;i < n;++i) p *= x; return p; End function
Well i cant use the pow function.. and i have to use fnctions that we have learned inside of the class.
The other option would be something like this:
Example:
Code:Function MyPow(double x, double n) double p = 1.0; int i, is_neg; if(n < 0) { is_neg = 1; n = -n; } else is_neg = 0; ... Do your thing here if(is_neg) p = 1.0/p; return p;
I never said anything about using pow(). I wrote my code straight from your code (I didn't even think I just eliminated unnecessary lines--its all you bud).
The second example is more easily integrated into your current code, methinks. Since I am not going to urge you to use main() recursively. I suspect you are not on the topic of "writing your own functions" quite yet.
Code thus far:
Ok so positive and 0 works for the powers. Just need to figure out the negative integers!Code:#include <FPT.h> int main() { //p=x^n inputs x,n output P double x,n,i,p; outS("Input a base number to be powered\n"); x = inD() ; outS("Input a power\n"); n = inD(); i=0; p=1; if (n < 0){ while (i < n){ p=p*x; p=1/(p); i=i+1; } } else { while (i < n && n>0){ p = p * x; i = i+1; } if (n==0){ p=1; } } outD(p); }
Ya, im sorry i just dont understand it, its somehing simple im sure but i just cant figure it out!
*sigh* Lucid, I really can't just paste my code to where it applies on your project. Why? Because when you get this code working without one of us writing your code for you, you will be so stoked.
I will do this much for you since it is driving all of us nuts:
Indentation fixed
I also highlighted in red a logical issue with your code too.Code:int main() { //p=x^n inputs x,n output P double x,n,i,p; outS("Input a base number to be powered\n"); x = inD() ; outS("Input a power\n"); n = inD(); i=0; p=1; if (n < 0){ while (i < n){ p=p*x; p=1/(p); i=i+1; } /* Hint: Wouldn’t it make more sense to do 1.0/(p) after that other stuff? */ } else { while (i < n && n>0){ p = p * x; i = i+1; } if (n==0){ p=1; } } outD(p); }
If n is -5, and i is initialized to 0, how many loop iterations will you do here?Code:while (i < n){ p=p*x; p=1/(p); i=i+1; }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
GOD i can not think of what i need to set it to to make it enter the negative loop! Alright, i wont be mad if someone tells me... im an idiot. Im flustered to hell
HOLD UP! i think i got it!
AGREEMENTS?!Code:#include <FPT.h> int main() { //p=x^n inputs x,n output P double x,n,i,p; outS("Input a base number to be powered\n"); x = inD() ; outS("Input a power\n"); n = inD(); i=0; p=1; if (n < 0){ while (i < fabs(n)){ p=p*x; i=i+1; } p=1/(p); } else { while (i < n && n>0){ p = p * x; i = i+1; } if (n==0){ p=1; } } outD(p); }
Ok well what if instead of saying if I have 0 is and I want to work my way up to a positive n I instead use my knowledge of the fact that n is less than 0 and say as long as n is less than i I can work my way down to n. Make sense?
Ok final check....if i input 300 for x(base number) and -3 for n i get: .00000003Code:#include <FPT.h> int main() { //p=x^n inputs x,n output P double x,n,i,p; outS("Input a base number to be powered\n"); x = inD() ; outS("Input a power\n"); n = inD(); i=0; p=1; if (n < 0){ while (n < i){ p=p*x; i=i-1; } p=1/(p); } else { while (i < n && n>0){ p = p * x; i = i+1; } if (n==0){ p=1; } } outD(p); }
Looks good to meI am glad you actually tested the code. I would have had to flame you for asking me to babystep you through compiling your hard labored work.