-
basic maths
Hi, I am trying to work out how to write an equation in C, i know it sounds stupid, but it when i set a variable, a equal to an equation, it says there is bad sytax before the =
i.e
Code:
int trapezium()//this part copes with calculating in value of f(x) and then outputting the estimated area under the graph
{
float fx[11],y, a,b,c;
for(i=0;i<11;i++)
{
fx[i] = (1 - v[i])/U;
printf("function of x at speed %5.2f, is %5.2f\n", v[i], fx[i]);
}
a =dx/2 ;
y = (2*(((1-v[1])/U) + ((1-v[2])/U) + ((1-v[3])/U)+ ((1-v[4])/U) + ((1-v[5])/U)+ ((1-v[6])/U)+ ((1-v[7])/U)+ ((1-v[8])/U)+ ((1-v[9])/U))));
printf("y = %f", y);
}
where it says a =dx/2 ; the error here is
E:\xxxxxxxxxxxxxxxxx\Boundary_Layer_Displacement\m ain.c|35|error: syntax error before '=' token|
i think there are a few brackets wrong with the y= but that can be sorted...
cheers guys
-
What is dx? It looks like you forgot to define it.
-
And this:
Code:
y = (2*(((1-v[1])/U) + ((1-v[2])/U) + ((1-v[3])/U)+ ((1-v[4])/U) + ((1-v[5])/U)+ ((1-v[6])/U)+ ((1-v[7])/U)+ ((1-v[8])/U)+ ((1-v[9])/U))));
Can be done in a loop, which would make it MUCH more readable.
--
Mats
-
Also, where are v and U declared?
-
Cheers guys that was enough to get me through that problem, and yeah have now done that bit in a loop, but it gives out RANDOM answers, the function is:
Code:
int trapezium()//this part copes with calculating in value of f(x) and then outputting the estimated area under the graph
{
float fx[11],y,a,b;
U = v[10];
for(i=0;i<11;i++)
{
fx[i] = ((i - v[i])/U);
for (n=1;n<10;n++)
{
a = a +fx[n]; //a is a section of the trapezium rule
printf("a = %5.2f\n", a);
}
}
float dx = 0.125;
b= fx[0] + fx[10];
y = (dx/2) * (b + (2*a));
printf("y = %5.2f", y);
}
the answer comes out as y =1#R
have i done something like multiply a foat by an integer or something stupid like that?
-
When you get "1#xxx" as the output, it usually means that you have an invalid floating point value - usually caused by using uninitialized data or doing stupid things like divide by zero, log(negative), sqrt(negative) and other "invalid" math operations.
Divide by zero is normally caught by the processor. Other invalid math is often not caught.
--
Mats
-
In the code you show, you still haven't declared the U and v[] variables. What type are they? Are they global variables?
-
OK so the entire program is below:
Code:
#include <stdio.h>
//#include <math.h>
const float dx = 0.125; //change in x displacement, i.e measurment interval.
float v[11], U;//My global floating varables
int i,n;//My global integers, counts for loops etc
int collectdata() //retrieves data from text file
{
int i;
FILE *fp=NULL; //create a pointer, fp to point at the file the data points are stored at
fp = fopen("data.txt", "r"); //open the data file from the saved location and read from it
for (i=0; i<11; i++) //for loop scans each data point from the file and stores it in the velocity array, v.
{
fscanf (fp, "%f\n", &v[i]); //scans for integers in data file
}
fclose(fp); //close data file
}
int trapezium()//this part copes with calculating each value of f(x), i.e f(x1), f(x2) etc and then outputting the estimated area under the graph
{
float fx[11],y,a,b;
//a and b are variables that help towards the final value for y, y being the displacement thickness
//fx is the function value, in this case the displacement thickness
for(i=0;i<11;i++)
{
fx[i] = ((1 - v[i])/U);
}
for (n=1;n<10;n++)
{
a = a +fx[n]; //a is a section of the trapezium rule
//printf("a = %5.2f\n", a);
}
b= fx[0] + fx[10]; //more trapezium rule
y = (dx/2) * (b + (2*a)); // final trapezium rule
printf("y = %5.2f", y); //print the estimated area under the graph
}
int main()
{
collectdata();//runt he function to collect the data from the text file
for(i=0;i<11;i++)//for loop to print all data points to the screen
{
printf("v%d= %5.2f\n", i+1, v[i]); //print the speeds retireved from the file
}
U = v[10]; //set U to the free stream veolcity - the greatest recorded speed above the wing
trapezium(); // run the function to calculate the displacement thickness
return 0;
}
Like it is it outputs an answer y=-0.72
But it prints all the values for the speed above, from the line
printf("v%d= %5.2f\n", i+1, v[i]); //print the speeds retireved from the file in the main function,
if you remove that line you get y=#R
-
a = a +fx[n]
a is not initialized so the result contains garbage as well