Can you please trouble shoot this code, it will not run. I need it to keep the s
ID: 3817449 • Letter: C
Question
Can you please trouble shoot this code, it will not run. I need it to keep the same functionality and format and run in C language CODE BLOCKS
//Trapezoid Rule CODE
#include<stdio.h>
#include<math.h>
//Using structures
struct var {
float x[20];
float y[20];
};
float f(float x)
{
return(exp(-pow(x,2)));
}
void main()
{
struct var var1;
int i,n;
float x0,xn,h,so,se,ans;
FILE *f;
f = fopen("data.txt", "a");
printf(" Enter values of x0,xn,h: ");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf(" refined value of n and h are:%d %f ",n,h);
printf(" Y values ");
for(i=0; i<=n; i++)
{
var1.x[i]=x0+i*h;
var1.y[i]=f(var1.x[i]);
printf(" %f ",y[i]);
}
int so=0; int *sp;
int se=0;int *sq;
for(i=1; i<n; i++)
{
if(i%2==1)
{
*sp=*sp+var1.y[i];
}
else
{
*sq=*sq+var1.y[i];
}
}
ans=h/3*(var1.y[0]+var1.y[n]+4*so+2*se);
printf(" final integral value is %f",ans);
fprintf(f, "Integral value is: %f ", ans);
fclose(f);
}
//Simpsons Rule CODE
#include<stdio.h>
#include<math.h>
struct var {
float x[20];
float y[20];
};
float f(float x)
{
return(exp(-pow(x,2));
}
void main()
{
int i,n;
FILE *f;
f = fopen("data.txt", "a");
struct var var2;
float x0,xn,h,so,se,ans;
printf(" Enter values of x0,xn,h: ");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf(" Refined value of n and h are:%d %f ",n,h);
printf(" Y values: ");
for(i=0; i<=n; i++)
{
var2. x[i]=x0+i*h;
var2.y[i]=f(var2.x[i]);
printf(" %f ",var2.y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+var2.y[i];
}
else
{
se=se+var2.y[i];
}
}
ans=h/3*(var2.y[0]+var2.y[n]+4*so+2*se);
printf(" Final integration is %f",ans);
fprintf(f, "Integral value is: %f ", ans);
fclose(f);
getch();
}
Explanation / Answer
Trapezoid Rule Code modification:
In this code, the first error was you had one function named f() and inside main(), you have FILE *f so both of them are having same name which is not allowed.
So I renamed the function as fn() and inside main() also I changed it.
The next thing was : you were declaring 'so' and 'se' variables 2 times. First time you were declaring them as float, but they were not used, then you are declaring them as int and using them.
So I removed the variable declarations as float.
The third error was in this line : printf(" %f ",y[i]);
Here, the compiler was not able to identify y[i] as it is not declared in main(). It is a member of structure variable v1. So, I modified it as: printf(" %f ",var1.y[i]);
The modified code is:
//Trapezoid Rule CODE
#include<stdio.h>
#include<math.h>
//Using structures
struct var {
float x[20];
float y[20];
};
float fn(float x)
{
return(exp(-pow(x,2)));
}
void main()
{
struct var var1;
int i,n;
float x0,xn,h,ans;
FILE *f;
f = fopen("data.txt", "a");
printf(" Enter values of x0,xn,h: ");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf(" refined value of n and h are:%d %f ",n,h);
printf(" Y values ");
for(i=0; i<=n; i++)
{
var1.x[i]=x0+i*h;
var1.y[i]=fn(var1.x[i]);
printf(" %f ",var1.y[i]);
}
int so=0; int *sp;
int se=0;int *sq;
for(i=1; i<n; i++)
{
if(i%2==1)
{
*sp=*sp+var1.y[i];
}
else
{
*sq=*sq+var1.y[i];
}
}
ans=h/3*(var1.y[0]+var1.y[n]+4*so+2*se);
printf(" final integral value is %f",ans);
fprintf(f, "Integral value is: %f ", ans);
fclose(f);
}
So now this code is fine and it allowed to start execution, so use this code with your file data.txt and check what are the values it is printing.
=======================================================================================
Simpsons Rule Code Modification:
Here, the first modification was on this line inside the function:
return(exp(-pow(x,2));
Here, 1 ')' is missing so I modified it as: return(exp(-pow(x,2)));
The next error is same as above. The same name of function and the file pointer. So I modified the function name as 'fn' at every place in the code.
The last thing is: you are using Code::Blocks right..!! So, by default it is using GCC compiler to compile the C files.
GCC does not support getch() and such functions as it does not have support for conio.h header file. The functionalities of this header file is already implemented in GCC implicitly. So you do not require to use any functions like getch(). So just remove it.
Yes, if you are compiling this code on TurboC++ IDE, then you need to write this getch(). So in this case, you also need to include the header file conio.h.
But here, it is not needed. So I removed it.
The modified code is:
//Simpsons Rule CODE
#include<stdio.h>
#include<math.h>
struct var {
float x[20];
float y[20];
};
float fn(float x)
{
return(exp(-pow(x,2)));
}
void main()
{
int i,n;
FILE *f;
f = fopen("data.txt", "a");
struct var var2;
float x0,xn,h,so,se,ans;
printf(" Enter values of x0,xn,h: ");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf(" Refined value of n and h are:%d %f ",n,h);
printf(" Y values: ");
for(i=0; i<=n; i++)
{
var2.x[i]=x0+i*h;
var2.y[i]=fn(var2.x[i]);
printf(" %f ",var2.y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+var2.y[i];
}
else
{
se=se+var2.y[i];
}
}
ans=h/3*(var2.y[0]+var2.y[n]+4*so+2*se);
printf(" Final integration is %f",ans);
fprintf(f, "Integral value is: %f ", ans);
fclose(f);
}
This code allowed me to start the execution. So you can say that it does not contain further errors.
Now you execute it keeping the file data.txt if present and check the execution.
So, all the errors are now resolved and you can execute it now.
Do comment if there is any query. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.