I need to write a C program in C++ dev (again I said C program meaning it has to
ID: 3625277 • Letter: I
Question
I need to write a C program in C++ dev (again I said C program meaning it has to be saved as a .c and not a c++) that takes the values from a data.txt which contains a bunch of numbers ranging from -200 +200 and then it takes 2values and calculates the derivative at that point then it saves the data on a excel spread sheet. my code below only works till about half way, I cant seem to figure out why my code doesn't work. Please can someone look at it?const double h = .01;
const double low = -3;
const double high = 6;
double f(double y1, double y2);
int main() {
FILE *fp, *out;
fp = fopen("data.txt", "r");
if (fp==NULL)
{
printf("That file did not work out...");
}
out = fopen("output.csv", "w");
if(out == NULL)
{
printf("Output not working...");
}
double d1, d2;
int x = low;
fscanf("%f", &d2)
while(fp->open) {
x+=h;
fscanf("%f", &d1);
fprintf(out,"%f,%f ", x, f(d1,d2));
}
fclose(fp);
fclose(out);
}
double f(double y1, double y2) {
return (d1-d2)/h;
}
Explanation / Answer
const double h = .01;
const double low = -3;
const double high = 6;
double f(double y1, double y2);
int main()
{
double x=low;
double d1,d2;
FILE *fp, *out;
fp = fopen("data.txt", "r");
if (fp==NULL)
{
printf("That file did not work out...");
exit(0);
}
out = fopen("output.csv", "w");
if(out == NULL)
{
printf("Output not working...");
}
fscanf(fp,"%lf", &d2);
while(1)
{
x+=h;
if(fscanf(fp,"%lf", &d1)==EOF)
break;
fprintf(out,"%lf,%lf ", x, f(d1,d2));
}
fclose(fp);
fclose(out);
return 0;
}
double f(double y1, double y2)
{
return (y1-y2)/h;
}