I am working on a program to create a calculator with only addition. I still need subtraction, division, and exponent. I am having trouble with this if anyone could help it would be greatly appreciated.
This is a discussion on Does anyone know how to do exponent with just addition? within the C Programming forums, part of the General Programming Boards category; I am working on a program to create a calculator with only addition. I still need subtraction, division, and exponent. ...
I am working on a program to create a calculator with only addition. I still need subtraction, division, and exponent. I am having trouble with this if anyone could help it would be greatly appreciated.
Multipication with addition is easy enough (which you seem to have gotten already).
Exponent is simply repeated multiplication.
a^2 = a * a
a^3 = a * a * a
Why not simply have a loop to call your multiplication code, while holding the current product?
Fractional exponents and negative exponents may be a little more difficult.
do I need a nested loop or just do my multiplication then get that sum and multiply it again
except obviously replace the multiply with whatever you are using to multiply (hopefully a function, as that would make sense).Code:while the iterator is less than the exponent total = base * total iterator++ end loop
i am trying to do this with using only addition so far i have
insertCode:for (i = 0; i < num2; i++){ sum = sum + num1; sum++;
That's not much. Why is that all you have? You were presented with perfectly useful ideas for implementing such a routine.
Why use cryptic names like num1 and num2 - call one exponent and the other base.
Anyway, what is it supposed to do? Does it work?
Last edited by mike65535; 05-02-2011 at 11:52 AM.
alright so i have
insertCode:for(i = 0; i < num1; i++){ sum = sum + num2; for(i = 0 ; i < num1 ; i++){ sum = sum + num1; } }
That's a joke, isn't it?
Did you compile it? Do you not see what happens to i if you reuse it like that?
Where's your respectful use of my suggestion to use descriptive variable names?
its not a joke...just change the names to exponent and base same thing.
insertCode:for (i = 0; i < exponent ; i++){ sum = sum + exponent; } for(i = 0; i < exponent; i++){ sum = sum + base; )
How about posting your entire code rather than a snippet that doesn't do anything. The question is does this compile? The answer is clearly a big fat NO.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
okay well i was just needing help with the for loop for the exponential function everything else complies right