Hey there everyone^^
Im almost finished on my mighty code of mighty... urm, -ness :mrgreen:
The finished code is actually to calcuate displacement thicknesses of various velocity profiles (read from flies as floats) using the trapezium rule.
Its sooo close, ive got a few little bugs remaining tho. Firstly, the first array value always reads in zero, instead of the correct number (in this case 0.3). Secondly, ive totally made a silly math error in the equaiton for the Trap rule, but dont worry about that ill fix it in no time!
If you'd like to compile it with my data, it uses a file of 10 float values as follows:
0.3
0.7
1.2
1.7
2.1
2.5
2.8
3
3
3
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define initial_value 0.07
#define interval 0.125
int read_one_float( float* out_float, FILE *f ) ;
int main(int argc, char *argv[])
{
while (1)
{
system("cls");
FILE *file_pointer = 0;
char ch = 0;
char filename[] = "" ;
long lSize = 0;
float tmp_float = 0, *buffer = 0, sum=0;
int i = 0, j = 0, count;
// Input filename.
printf("\nEnter a filename : ");
gets(filename);
// Try to open the file.
if ( (file_pointer = fopen(filename, "rb")) == NULL )
{
perror("\nError opening file");
puts("\nEnter x to exit, any other to try again.");
if ( (ch = getch()) == 'x')
break;
}
else
{
printf("\nFile successfully opened\n");
// assume total number of floats in file is same as number of new lines + 1.
// calculate the size of buffer:
while( EOF != (ch = fgetc(file_pointer)) )
if( ch == '\n' )
lSize++ ;
lSize++ ;
rewind (file_pointer);
// allocate memory to contain the whole file.
buffer = (float*) malloc (sizeof(float)*lSize);
if (buffer == NULL) return 1;
else printf("\n%d bytes of memory allocated to buffer...", sizeof(float)*lSize);
// copy the file into the buffer.
while( 0 != read_one_float( &tmp_float, file_pointer ) )
buffer[i++] = tmp_float ;
printf("\nNumber of entries read : %d\n\n", i-- );
//================================================================================================================================================
// calculate the displacement thickness using the Trapezium rule
for (count=1; count<i; count++)
sum+=(interval/2)*(1-((buffer[count]/buffer[i-1])))+(1 -((buffer[count-1]/buffer[i-1])));
printf("boundary layer thickness = %f\n", sum);
// terminate
fclose (file_pointer);
free (buffer);
// prints values assigned into the array.
printf("\nData read from file is :\n") ;
for( j = i; j >= 0; j-- )
printf("\nbuffer[%d] = %.1f", i-j, buffer[i-j] ) ;
printf("\n\n") ;
// waits for user input to continue
puts("\nEnter x to exit, any other begin again!");
if ( (ch = getch()) == 'x')
break;
}
}
}
//--------------FUNCTIONS--------------
int read_one_float( float* out_float, FILE *f )
{
char buf[100] = "" ;
if (0 == fgets(buf, sizeof buf, f))
return 0 ;
*out_float = atof( buf ) ;
return 1;
}
I suspect the erroneous section of code is lines 48-49!
Again, help with correcting the first array value would be great^^
Chris