# Thread: 50 percent for this homework?

1. ## 50 percent for this homework?

So I had a piece of homework to do, where my program had to read from 3 files, each with 11 pieces of data in, then it had to estimate the graph (due to the data being empirical) using the trapezium rule. The user must be able to select which data to use.

Can someone please tell me what more i could/should have done to get more than 50%, my friends could print the results, and save them to text file, and he got 65%...

Cheers

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

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;
{

system("cls");
printf("\n");
printf("Alex Walker - 4054287 MENG. Displacement Thickness across a wing\n\n");
printf("                                 Options\n");
printf("                                 -------\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
{
system("pause");
}
else
{
printf("\nVelocities for 3 m/s\n\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
{
system("pause");
}
else
{
printf("\nVelocities for 4 m/s\n\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
{
system("pause");
}
else
{
printf("\nVelocities for 5 m/s\n\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;
}```

2. If your teacher didn't give you reasons for such a low grade then you probably didn't write the program that was specified.
WILL ONLY RUN ON WINDOWS(R) OPERATING SYSTEMs DUE TO WINDOWS LIBRARY BEING USED, THE PROGRAM CALLS WINDOWS API's

[...]

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 ;
Most of the stuff in here sounds like excuses: if three functions to calculate the displacement thickness was part of the rubric, you lost out on those points fair and square. Using windows api was probably unacceptable as well.

I mean, what's wrong with it? A lot. Let me name a few examples:
- Indentation style is ugly an inconsistent.
- Comments make the code less readable because they show up on all random locations in the code. I advice to make a single comment per block of code, like:
Code:
```// Comment here
if(something) {
// Some other comment
Do something

Do something else
}
else {
// Something
Do something again
}```
- The header on top is laughable.
- Your code is copy-paste code. You do the same thing three times but in three different cases and functions. Why the heck did you copy it and not reuse code? The only difference I can find is the filename, which then should have been the ONLY thing that would be different in those cases - probably using a switch or maybe even just sprintf.
- Variable names are inconsistent as well
- Function names are horrible
- What is this maxu function? Why write an O(n^2) algorithm for something so simple? You just have to loop through everything once:
Code:
```max = array[0]
for each item in array
if item > max
max = item```
- You don't return any value in a function that should return something.
- Using global variables - bad
- Using a global variable for the file handle only to check whether the file has been opened and closed properly the last time. What's wrong with return values?
- The comment on using only one array and not three seems to tell us you think you had a pretty "kewl" optimisation there. This optimisation should not even have been required, because of points I already specified.

Pop-quiz for you. What would happen with your code if I would write something similar to this:
Code:
```for(i = 0; i < 100; i++) {
collectdata2();
// Handle data 2
}```
I can tell you it'd loop forever. Was that something you expected? Try to find out why by yourself.

Based on this code, you seemed to have failed to grasp the most basic functionality of the programming language. I actually think you should feel lucky you received 50%...