2

i'm trying to compute "2^0 + 2^1 + 2^2 + ... + 2^14", using the following program(i'm a newbie and can only compute a exponent by multiply itself a certain times). The result should be 32767, but i ran it and got 270566475, i thought for long but can't figure out why...

#include <stdio.h>

int main(void)
{
   int i, e, exponent, sum;

   e = 1;
   exponent = 1;
   sum = 1;

   for (i = 1; i <=14; i++)
   {
       for (e = 1; e <= i; e++)
       {
          exponent *= 2;
       }

       sum += exponent;
   }

   printf("%d\n", sum);

   return 0;
}

So what's wrong with this??? Thanks!!!

3 Answers 3

7

You don't need the inner loop. Just execute exponent *= 2 once, directly inside the outer loop. BTW, I think you have to do it after the sum += ....

Also, you could start with sum = 0 and i = 0, which is closer to the math you described.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah!~~~ After you told me this, i really don't know why i did it in that complex way, thankkkkks!
2

Look at your inner loop by itself. It's trying to calculate, for one specific value of i, 2^i.

But exponent does not start at 1 every time. So you go into that loop with exponent already some very large value.

for (i = 1; i <=14; i++)
{
    exponent = 1;
    for (e = 1; e <= i; e++)
    {
        exponent *= 2;
    }

    sum += exponent;
}

Now you've reset exponent (which, to be clear, isn't the exponent at all but the calculated result) for each new power of 2.

2 Comments

You don't have to use the inner loop as it can be done with the outer loop only, as answered above by Marcelo.....
Oh, yes~~ initialization! Thank you very much!!
2

If you have right to create a function it better to do it like this with a recursive function :

#include <stdio.h>

int power(int x, int exp) {
    if (exp == 0)
        return 1;
    else
        return x * power(x, exp-1);
}

int main (int argc, const char * argv[])
{
    int i;
    int sum = 0;
    for (i = 0; i <= 14; i++) {
        sum += power(2, i);
    }

    printf("%d",sum);
    return 0;
}

I hope it helps.

You just need one loop because each you already have the result of n-1 value. I had correct your code it works.

#include <stdio.h>

int main (int argc, const char * argv[])
{
    int i, e, exponent, sum;

    e = 1;
    exponent = 1;
    sum = 1;

    for (i = 1; i <= 14; i++)
    {
        exponent *= 2;
        sum += exponent;
    }

    printf("%d\n", sum);

    return 0;
}

Both codes work

5 Comments

You can reuse this function to do for example 3^3, 4^4 any value you want (x^exp).
Don't recommend recursion where iteration is called for. (And likewise don't make complex solutions to simple errors.)
Oh, thank you! but for me stuff like power, argc are symbols that are more mysterious than Latin now, hehe~~ thanks a lot!
Your are welcome. The argc is automatically generated by the software.
I didn't saw the comment before. For some people like recursion is faster and easier to set in some cases. And I know it takes place in the memory but for a small operation like this it's nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.