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;
}
}
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.
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
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.
Related
#include <stdio.h>
#include <math.h>
int main()
{
double precision = 0;
printf("\ninsert number\n");
while(precision < 1){
scanf("%lf",&precision);
}
printf("the value of e with precision of %.0lf is %lf",precision,e(precision));
return 0;
}
int fact(int num){
int ris = 1;
for(int i = num;i > 0;i--){
ris = ris * i;
}
printf("res=%d\n",ris);
return ris;
}
int e(double precision){
double valE = 1;
for(double i = precision;i > 0 ;i--){
valE = valE + 1/fact(i);
printf("\nsame res:%.1lf\n",fact(i));
}
return (double)valE;
}
debug
i know there is an answer for that but my problem is the comunication between the 2 functions, i know i could solve it by slapping everything inside the main()
There are many issues:
format specifiers (for scanf and printf) must match the arguments
don't use floating point types as counters
if you divide one integer by another integer, the result will be an integer that is trucated. If you want the result to be a floating point type, you need to convert at least one of the operands to a floating point type.
you need to declare the functions you use (fact and e) before using them, or just put them before main, like below.
You want this, explanations in the comments:
#include <stdio.h>
#include <math.h>
int fact(int num) {
int ris = 1;
for (int i = num; i > 0; i--) {
ris = ris * i;
}
printf("res=%d\n", ris);
return ris;
}
double e(int precision) {
double valE = 1;
for (int i = precision; i > 0; i--) { // use int for loop counters
valE = valE + 1.0 / fact(i); // use `1.0` instead of `1`, otherwise an
// integer division will be performed
printf("\nsame res: %d\n", fact(i)); // use %d for int^, not %llf
}
return valE; // (double) cast is useless
}
// put both functions e and fact before main, so they are no longer
// declared implicitely
int main()
{
int precision = 0; // precision should be an int
printf("\ninsert number\n");
while (precision < 1) {
scanf("%d", &precision); // use %d for int
}
printf("the value of e with precision of %d is %lf", precision, e(precision));
return 0;
}
Consider my attempt to implement the Babylonian method in C:
int sqrt3(int x) {
double abs_err = 1.0;
double xold = x;
double xnew = 0;
while(abs_err > 1e-8) {
xnew = (2 * xold + x/(xold* xold))/3;
abs_err= xnew-xold;
if (abs_err < 0) abs_err = -abs_err;
xold=xnew;
}
return xnew;
}
int main() {
int a;
scanf("%d", &a);
printf(" Result is: %f",sqrt3(a));
return 0;
}
Result is for x=27: 0.0000?
Where is my mistake?
While the function returns an int, that value is printed with the wrong format specifier, %f instead of %d.
Change the signature (and the name, if I may) into something like this
double cube_root(double x) { ... }
Or change the format specifier, if you really want an int.
Following the explanation from tutorialspoint, which states, that the basic idea is to implement the Newton Raphson method for solving nonlinear equations, IMHO, the code below displays this fact more clearly. Since there is already an accepted answer, I add this answer just for future reference.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
double rootCube( double a)
{
double x = a;
double y = 1.0;
const double precision = 0.0000001;
while(fabs(x-y) > precision)
{
x = (x + y) / 2.0;
y = a / x / x;
}
return x;
}
int main(int argc, const char* argv[])
{
if(argc > 1)
{
double a =
strtod(argv[1],NULL);
printf("cubeRoot(%f) = %f\n", a, rootCube(a));
}
return 0;
}
Here, in contrast to the original code of the question, it is more obvious, that x and y are the bounds, which are being improved until a sufficiently accurate solution is found.
With modification of the line in the while block, where y is being updated, this code can also be used to solve similar equations. For finding the square root, for example, this line would look like this: y = a / x.
I am very new to C programming. Here, I have written a very simple C program to evaluate the Taylor series expansion of exponential function e^x, but I am getting error in my output, though the program gets compiled successfully.
#include <stdio.h>
int main()
{
double sum;
int x;
printf("Enter the value of x: ");
scanf("%d",&x);
sum=1+x+(x^2)/2+(x^3)/6+(x^4)/24+(x^5)/120+(x^6)/720;
printf("The value of e^%d is %.3lf",x,sum);
return 0;
}
^ in C is not an exponentiation operator. It is a bitwise operator. For a short number of terms, it is easier to just multiply.
You also need to take care of integer division. If you divide x*x/2, then you will get integer division. You need to divide the number to get a double answer, as shown below.
You can replace the line calculating the sum with the following line.
sum=1+x+(x*x)/2.0+(x*x*x)/6.0+(x*x*x*x)/24.0+(x*x*x*x*x)/120.0+(x*x*x*x*x*x)/720.0;
A better option would be to use a loop to calculate each term and add it to the answer.
double answer, term = 1;
int divisor = 1;
amswer = term;
for (i=0; i<6; i++)
{
term = term * x / divisor;
answer += term;
divisor *= (i+2);
}
Use pow() instead of ^
Use double x instead of int x
So the result code will look like:
#include <stdio.h>
#include <math.h>
int main()
{
double sum;
double x;
printf("Enter the value of x: ");
scanf("%lf",&x);
sum=1+x+pow(x,2)/2+pow(x,3)/6+pow(x,4)/24+pow(x,5)/120+pow(x,6)/720;
printf("The value of e^%f is %.3lf",x,sum);
return 0;
}
It should be linked with math lib, i.e.:
gcc prog.c -lm
As other people failed to provide proper piece of C code, I have to try it:
#include <stdio.h>
int main() {
printf("Enter the value of x: ");
double x;
scanf("%lf", &x);
double sum = 1.0 + x * (1.0 + x * (1.0 / 2 + x * (1.0 / 3 + x * (1.0 / 4 + x * (1.0 / 5 + x / 6.0)))));
printf("The value of e^%.3lf is %.3lf", x, sum);
}
Better make it dynamic like this one.
#include <stdio.h>
int power(int x,int n){
int sum=1,i;
if (n == 0)
return 1;
for(i=1; i<= n; i++){
sum *= x;
}
return sum;
}
int fact(int n){
if(n == 0)
return 1;
for(i=1; i<= n; i++){
fact *=i;
}
return fact;
}
int main()
{
float sum=0.0;
int i,x,n;
printf("Enter the value of x and n terms: ");
scanf("%d %d",&x,&n);
for(i=0; i<=n; i++){
sum += (float)power(x,i)/fact(i);
}
printf("The value of %d^%d is %.3f",x,n,sum);
return 0;
}
Quick question:
#include <stdio.h>
int main(void) {
int divisor, counter, binary, counter2;
int digit0, digit1, digit2, digit3;
float decimal;
printf("Decimal\t\tBinary\n");
for (counter = 0; counter <= 15; counter++) {
printf("%d\t\n", counter);
decimal = counter;
for (counter2 = 0; counter2 <= 3; counter2++) {
decimal % 2 == 1 ? digit0 = 1 : digit0 = 0);
}
}
return 0;
}
I keep getting the error that the "expression must be a modifiable value" on variable name "decimal" in the second for loop.
Why is this, and how can I fix it?
Thank you!
Because decimal is float,but % only for integers.If you really want to mod by using float, you can use function float fmod(float x, float y), it calculates x%y, and you should include #include <math.h> to use it.
Ive created a program to compute the value of an integral from zero to infinity, however when i go to print my answer it only prints it to 6 decimal places, could someone advise on why this is?
Thanks very much in advance :)
#include <stdio.h>
#include <math.h>
#include <float.h>
double integral_Function(double x){
double value;
value = 1/((1+ pow(x, 2))*(pow(x, 2/3)));
return value;
}
double trapezium_Rule(double lower, double upper, int n){
double h;
double total;
h = (upper - lower)/n;
total = (h/2) * (integral_Function(upper) + integral_Function(lower));
for(int i = 1; i < n; i++){
total = total + (h * integral_Function(lower + (i * h)));
}
return total;
}
int main() {
double sum = 0;
double upper = DBL_MIN * 1.001;
double lower = DBL_MIN;
while (upper <= DBL_MAX){
sum += trapezium_Rule(lower, upper, 5000) * 2;
lower = upper;
upper = upper * 1.02;
}
printf("%f", sum);
return 0;
}
This is because you use string format %f which is by default only print 6 digit after comma. To increase it, simply specify the length that you want:
printf("%.9f", sum); //note the 9 - it is the length.