Code:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
//GLOBAL VARIABLES AND CONSTANTS:
const float dx = 0.125; //change in x displacement, i.e measurment interval.
float v[11], U;//My global floating varables, v is the speeds being read from the data files, U is the free stream velocity - the greatest recorded speed above the wing
int i,n;//My global integers, counts for loops etc
FILE *fp=NULL; //create a pointer, fp to point at the file the data points are stored in
/*
WILL ONLY RUN ON WINDOWS(R) OPERATING SYSTEMs DUE TO WINDOWS LIBRARY BEING USED, THE PROGRAM CALLS WINDOWS API's
The three data files, ms1, ms2 and ms3 are stored in the same folder as the application, they are text files with the data in like so:
0.3
0.7
1.2
1.7
2.1
2.5
2.6
2.8
3.1
3.0
3.0
***PLEASE READ***
I only made one array (that will eventually have stored all of the data points from all three files, at different times) because I figured that
the computing power that would be required to recalculate the results would be insignificant to the amount of memory saved (which would be three times the memory) from making three arrays.
This idea also means my program can be much shorter, not needing three functions to calculate the displacement thickness
The results this program returns are:
--------------------------------------
For 3m/s : 0.456mm ;
For 4m/s : 0.391mm ;
For 5m/s : 0.363mm ;
*/
//FUNCTIONS BEGIN HERE
int maxu()//maxu runs an algorithm to find out which of the data points is the largest, and therefore also the free stream velocity
{
for (i=0;i<11;i++)
{
for (n=0;n<11;n++)
{
if (v[i] > v[n])
{
U= v[i];
}
else
{
U = v[n];
break;
}
}
}
printf("\nFree Stream velocity = %.2f m/s\n", U);
}
int collectdata1() //retrieves data from text file data1
{
fp = fopen("ms1.txt", "r"); //open the data file from the saved location and read from it
if (fp == NULL)
{
}
else
{
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", &v[i]); //scans for integers in data file
}
fclose(fp); //close data file
}
}
int collectdata2() //retrieves data from text file data2
{
fp = fopen("ms2.txt", "r"); //open the data file from the saved location and read from it
if (fp == NULL)
{
}
else
{
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", &v[i]); //scans for integers in data file
}
fclose(fp); //close data file
}
}
int collectdata3() //retrieves data from text file data3
{
fp = fopen("ms3.txt", "r"); //open the data file from the saved location and read from it
if (fp == NULL)
{
}
else
{
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=0,b, edge;
//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)); //function of x
}
for (n=1;n<10;n++)
{
a = a +fx[n]; //a is a section of the trapezium rule - the sum of all the strips
}
b= fx[0] + fx[10]; //more trapezium rule
edge = 0.07 * ((1 + fx[0])/2);//little slither of area between x=0 and x=0.07mm
y = (dx/2) * (b + (2*a)) + edge; // final trapezium rule
printf("\ny, the displacement thickness, using the trapezium method = %5.3f mm\n\n", y); //print the estimated area under the graph
}
int comparedata()
{
float data1[11], data2[11], data3[11];
collectdata1();
system("cls");
printf("\n\nDisplacement velocity table:\n\n");
for (i=0;i<11;i++) //the following for loops stores all of the data from all three text files
{
data1[i] = v[i];
}
collectdata2();
for (i=0;i<11;i++)
{
data2[i] = v[i];
}
collectdata3();
for (i=0;i<11;i++)
{
data3[i] = v[i];
}
printf("3 m/s 4 m/s 5 m/s\n"); //begins velocity chart
printf("-----------------------------\n");
for (i=0; i<11; i++) //for loop draing chart of data
{
printf("%.2f %.2f %.2f\n", data1[i], data2[i], data3[i]);
}
}
int main() //main function
{
int a;
while (1) //menu system
{
system("cls");
printf("\n");
printf("Alex Walker - 4054287 MENG. Displacement Thickness across a wing\n\n");
printf(" Options\n");
printf(" -------\n");
printf(" Please enter a selection from the menu\n\n");
printf(" 1. Velocity Chart\n");
printf(" 2. Displacement thickness when wind speed = 3m/s\n");
printf(" 3. Displacement thickness when wind speed = 4m/s\n");
printf(" 4. Displacement thickness when wind speed = 5m/s\n\n");
printf(" 5. Exit\n");
scanf("%d", &a);
printf("\n");
switch ( a ) //option for user to select which set of data to use
{
case 1 :
{
comparedata();
printf("\n");
system("pause");
}
break;
case 2 :
{
collectdata1();//runt the function to collect the data from the text file
system("cls");
if (fp == NULL)//encase the file cant be found on the computer
{
printf("File Not Found\n\n");
system("pause");
}
else
{
printf("\nVelocities for 3 m/s\n\n");
printf("Reading Velocity\n");
printf("--------------------\n");
for(i=0;i<11;i++)//for loop to print all data points to the screen
{
//When you see the data displayed it is in nice straight columns, which are decided by the number of spaces between the number of the data point (1-11).
//When that number is 2 digits (10 and 11 in this case) the program removes a [space] to accomodate for the extra digit, continuing to keep the columns straight.
if ((i+1)>99)
{
printf("%d. %5.2f\n",(i+1), v[i]); //print the speeds retireved from the file
}
if ((i+1)>9)
{
printf("%d. %5.2f\n",(i+1), v[i]); //print the speeds retireved from the file
}
else if ((i+1) < 10)
{
printf("%d. %5.2f\n",(i+1), v[i]); //print the speeds retireved from the file
}
}
maxu();
trapezium(); // run the function to calculate the displacement thickness
system("pause"); //wait for user to press a key
}
}
break;
case 3 :
{
collectdata2();//runt he function to collect the data from the text file
system("cls");
if (fp == NULL)//encase the file cant be found on the computer
{
printf("File Not Found\n\n");
system("pause");
}
else
{
printf("\nVelocities for 4 m/s\n\n");
printf("Reading Velocity\n");
printf("--------------------\n");
for(i=0;i<11;i++)//for loop to print all data points to the screen
{
if ((i+1)>9)
{
printf("%d. %5.2f\n",(i+1), v[i]); //print the speeds retireved from the file
}
if ((i+1)>99)
{
printf("%d. %5.2f\n",(i+1), v[i]); //print the speeds retireved from the file
}
else if ((i+1) < 10)
{
printf("%d. %5.2f\n",(i+1), v[i]); //print the speeds retireved from the file
}
}
maxu();
trapezium(); // run the function to calculate the displacement thickness
system("pause");
}
}
break;
case 4 :
{
collectdata3();//run the function to collect the data from the text file
system("cls");
if (fp == NULL)//encase the file cant be found on the computer
{
printf("File Not Found\n\n");
system("pause");
}
else
{
printf("\nVelocities for 5 m/s\n\n");
printf("Reading Velocity\n");
printf("--------------------\n");
for(i=0;i<11;i++)//for loop to print all data points to the screen
{
if ((i+1)>9)
{
printf("%d. %5.2f\n",(i+1), v[i]); //print the speeds retireved from the file
}
if ((i+1)>99)
{
printf("%d. %5.2f\n",(i+1), v[i]); //print the speeds retireved from the file
}
else if ((i+1) < 10)
{
printf("%d. %5.2f\n",(i+1), v[i]); //print the speeds retireved from the file
}
}
maxu();
trapezium(); // run the function to calculate the displacement thickness
system("pause");
}
}
break;
case 5 :
{
exit(0);
}
break;
default:
{
printf("\n\n");
printf(" * Invalid Entry *\n");
printf(" -------------\n");
printf("\n\n");
system("pause");
}
break;
}
}
return 0;
}