Like Tree1Likes
  • 1 Post By mike65535

Does anyone know how to do exponent with just addition?

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. ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    10

    Does anyone know how to do exponent with just addition?

    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.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    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.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    do I need a nested loop or just do my multiplication then get that sum and multiply it again

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Code:
    while the iterator is less than the exponent
            total = base * total
            iterator++
    end loop
    except obviously replace the multiply with whatever you are using to multiply (hopefully a function, as that would make sense).

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cowboyz209 View Post
    do I need a nested loop or just do my multiplication then get that sum and multiply it again
    Don't nest them... use one loop to get your first product then a second loop to do exponentiation on the product.
    There should be no reason to repeatedly calculate the same number...

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    i am trying to do this with using only addition so far i have

    insert
    Code:
    for (i = 0; i < num2; i++){
    sum = sum + num1;
    sum++;

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    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.
    Salem likes this.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    alright so i have
    insert
    Code:
         for(i = 0; i < num1; i++){
               sum = sum + num2;
         for(i = 0 ; i < num1 ; i++){
               sum = sum + num1;
    }
    }

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    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?

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    its not a joke...just change the names to exponent and base same thing.

    insert
    Code:
      for (i = 0; i < exponent ; i++){
                sum = sum + exponent; }
      for(i = 0; i < exponent; i++){
                sum = sum + base;
    )

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    okay well i was just needing help with the for loop for the exponential function everything else complies right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some Exponent Help
    By jrahhali in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-04-2005, 07:31 PM
  2. Exponent help
    By flatline911 in forum C++ Programming
    Replies: 4
    Last Post: 08-15-2003, 01:34 AM
  3. exponent
    By tmoney$ in forum C Programming
    Replies: 2
    Last Post: 04-14-2003, 02:24 PM
  4. my nifty little exponent program
    By maxwell_edison in forum C++ Programming
    Replies: 3
    Last Post: 03-18-2002, 09:15 AM
  5. Illegal exponent operator, how do i fix this?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 09-21-2001, 09:13 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21