You are on page 1of 2

#include<stdio.

h>
#include<math.h>
#include<stdlib.h>
double solve_laminar(double x)
{
return 64/x;
}
double solve_turbulent(double x, double y)
{
double A,B,C;
A = -2*log10(y/3.7+12/x);
B = -2*log10(y/3.7+2.51*A/x);
C = -2*log10(y/3.7+2.51*B/x);
return A - (pow(B-A,2))/(C-2*B+A);
}

main()
{
///Declaration of Initial Variables and Printing of what the program is abou
t
double f=0.0300;
int d,e,g,i,j,k;
double *turbulent=malloc(1*sizeof(double));
double *roughness=malloc(1*sizeof(double));
printf("Moody Diagram\nRe<2000:laminar flow\nRe>2000:turbulent flow\n");
///Input for turbulent flow values
printf("How many turbulent flow points do you want to test? ");
scanf("%d", &e);
turbulent=realloc(turbulent, e*sizeof(double));
if(e==0)
{
}
else
{
printf("Enter the values for the Reynolds number, Re, for turbulent flow
.\n");
for(i=0;i<e;i++)
{
Save2:
printf("Re[%d]:", i);
scanf("%lf", &turbulent[i]);
if(turbulent[i]<=2000)
{
printf("Invalid value for turbulent flow. Please Enter another v
alue\n");
goto Save2;
}
}
printf("For the turbulent flow, how many different roughness value, E/D,
do you want to test?\n(# of E/D should not be zero) ");
scanf("%d", &g);
roughness=realloc(roughness, g*sizeof(double));
printf("Enter the values for the roughness E/D.\n");
for(i=0;i<g;i++)
{
printf("E/D[%d]:", i);

scanf("%lf", &roughness[i]);
}
}
///Computation for turbulent flow
double f_turbulent[e][g];
double tol=0.001;
for(i=0;i<e;i++)
{
for(j=0;j<g;j++)
{
double s=1/sqrt(f);
s=solve_turbulent(turbulent[i],roughness[j]);
f_turbulent[i][j]=1/(s*s);
}
}
///Printing of the values in a file
FILE *ptr;
ptr=fopen("results.csv", "w");
if(e==0)
{
fprintf(ptr, "No given turbulent flow");
}
else
{
fprintf(ptr, "Turbulent Flow\nE/D,");
for(i=0;i<g;i++)
{
fprintf(ptr, "%lf,", roughness[i]);
}
fprintf(ptr, "\nRe,");
for(i=0;i<g;i++)
{
fprintf(ptr, "f,");
}
fprintf(ptr, "\n");
for(i=0;i<e;i++)
{
fprintf(ptr, "%lf,", turbulent[i]);
for(j=0;j<g;j++)
{
fprintf(ptr, "%lf,", f_turbulent[i][j]);
}
fprintf(ptr, "\n");
}
}
printf("Values are printed in results.csv file");
fclose(ptr);
}

You might also like