-2

I am trying to approximate Euler's number using the formula (1+(1/n))^n. The compiler is telling me that there is an "expected expression before 'double'" Here is the code:

#include <stdio.h>
#include <math.h>

int main()
{
    int x, y, power;
    int num = 1;
    int position = 1;
    while (position <= 100)
    {
        num = 1/num;
        num = num + 1;
        x = num;
        power = double pow(x, x); //here
        printf("%f", power);
        position += 1;
        num = position;
    }
}
  • There is multiple problems with your code, that I can see. It is slightly unclear to me how your code is supposed to work. What is 'power' supposed to store? What is 'y' for? – Hagadol Oct 24 '19 at 16:21
  • 1
    Well, first thing you should notice that you *can't* perform such a calculation with integers. – Eugene Sh. Oct 24 '19 at 16:24

3 Answers3

1

If you want a number to be a double (number with decimals), you need to define it as a double, not an integer. I have this code which should solve your problem. Also make sure to compile gcc FILEPATH -lm -o OUTPUTPATH if you are using UNIX.

#include <stdio.h>
#include <math.h>

int main()
{
    double x, y, power, num = 1; //doubles allow for decimal places so declare it as double
    int position = 1; //Position seems to only be an integer, so declare it as an int.
    while (position <= 100)
    {
        num = 1/num;
        num++;
        x = num;
        power = pow(x, x);
        printf("%f", power);
        position += 1;
        num = position;
    }
}

Another option is a for loop:

#include <stdio.h>
#include <math.h>

int main()
{
    double x, y, power, num = 1;
    for (int i = 1; i <= 100; i++) {
        num = 1/num;
        num = num + 1;
        x = num;
        power = pow(x, x);
        printf("%f", power);
        position += 1;
        num = i;
    }
}

If you are trying to approximate Euler's number, I don't see why not just try something like:

static const double E = 2.718281828459045;

I have simply corrected syntax errors in your program, but I don't think it will actually get you E. See this page about calculating E in C.

0

I'm no C master but isnt just calling double by itself a type declaration and not type casting? wouldnt it be power = (double) pow(x, x); if you are type casting? see: https://www.tutorialspoint.com/cprogramming/c_type_casting.htm

  • You are correct that it is not type cast, but it is not a type declaration either: it is called a syntax error, and this does not answer the question. – Larry Oct 24 '19 at 16:30
  • This change is not sufficient to fix to code; note, that power in the original code is of type int. – Hagadol Oct 24 '19 at 16:36
  • i figured i was just thinking about what the interpreter was flipping out about. Sorry If I was misleading. – Henk J Reder Oct 24 '19 at 16:43
0

I reworked some mistakes in your code and think it should work now; however, the style, which I kept untouched, is confusing.

#include <stdio.h>
#include <math.h>

int main()
{
    double power;  //stores floating point numbers!
    double num = 1;//stores floating point numbers!
    int position = 1;
    while (position <= 100)
    {
        num = 1/num;
        num = num + 1;
        power = pow(num, position); //x not needed, this is what you ment
        printf("%f\n", power); //%d outputs decimal numbers, f is for floats
        position += 1;
        num = position;
    }
}

To improve your code, I would suggest to simplify it. Something along the lines of this

#include <stdio.h>
#include <math.h>

int main()
{
    double approx;
    for(int iter=1; iter<=100; iter++){
        approx=pow((1+1./iter),iter);
        printf("%f\n", approx);
    }
}

is much easier to understand.

Hagadol
  • 362
  • 1
  • 3
  • 16