In: Computer Science
Can you please tell me why my code isn't working? It won't calculate the values I have using my input file.
/*******************************************************************************
AUTHOR SECTION
ENGR 200.07 DATE: 10/23/2020
PROGRAM:
********************************************************************************
PROGRAM DESCRIPTION
This program takes a pre-made .txt file’s input values, and
calculates the
kinetic energy wind farms produce from moving air into electrical
energy.
Using 3 different formulas this program calculates the available
power in the
wind, the maximum available power that can be produced, and the
amount of
electricity produced by the turbines in kW. Only the diameter of
the wind
blades, number of turbines, and power in kW will be printed on
screen. These
3 values will also be output to a designated .txt file. The input
and output
file locations can be found in the preprocessor directives section
of the
program. These file locations can also be altered, allowing for
other .txt
files with different input values to be used.
DESCRIPTION OF VARIABLES
NAME | TYPE | DESCRIPTION
-----------------------------------------------------------------------------
PW | double | Power available in wind
PWM | double | Maximum power the wind turbine can produce
PE | double | The amount of electricity (in Watts) that can
be
produced for each turbine
BD | double | Diameter of blades
BR | double | Radius of blades
NT | double | Number of turbines available
A | double | Area swept by blades (m^2)
V | double | Velocity of the wind (9.5 m/s)
P | double | Density of the air (1.2754 kg/m^3)
CP | double | Benz Limit (0.48 unitless)
N | double | Combined electricity of gear box, generator, and
bearings (.33)
PI | symbolic | Constant for pi (3.14159)
i | int | Loop control variable
ndata | int | Number of record lines in input file
*******************************************************************************/
/* Preprocessor directives */
#include
#include
#define PI 3.14159
#define inputfile "C:\\Users\\16614\\Desktop\\ENG
200\\farms.txt"
#define outputfile "C:\\Users\\16614\\Desktop\\ENG
200\\power.txt"
/* Main function */
int main(void)
{
/* Declare variables */
double PW, PWM, PE, BD, BR, NT, A, V, P, CP, N;
int i, ndata;
FILE *INP = NULL, *OUTP = NULL;
/* Open Files */
INP = fopen(inputfile, "r");
OUTP = fopen(outputfile, "w");
/* Print headings */
printf("\n********************************************");
printf("\n WIND FARM CONFIGURATIONS"
"\n\nDIAMETER NUMBER OF TURBINES POWER"
"\n (m) (kW)");
fprintf(OUTP,"\n********************************************");
fprintf(OUTP,"\n WIND FARM CONFIGURATIONS"
"\n\nDIAMETER NUMBER OF TURBINES POWER"
"\n (m) (kW)");
/* Verify input file */
if(INP==NULL)
{printf("\n\n COULD NOT FIND FILE"
"\n PROGRAM TERMINATED"
"\n********************************************");
return 1;
}
else
{
/* Read Control numbers */
fscanf(INP, "%i",&ndata);
/* Calculate Values using Formulas and Print Results */
for(i=1;i<=ndata;i++)
{
fscanf(INP,"%lf %lf",&BD,&NT);
V = 9.5;
P = 1.2754;
CP = 0.48;
N = 0.33;
BR = 1/2*BD;
A = PI*pow(BR,2);
PW = 1/2*P*A*pow(V,3);
PWM = CP*PW;
PE = N*PWM;
printf("\n %3.1f %3.0f %5.1f",BD,NT,BR);
fprintf(OUTP,"\n %3.1f %3.0f %5.1f",BD,NT,BR);
}
}
printf("\n********************************************\n\n\n");
fprintf(OUTP,"\n********************************************\n\n\n");
/* Exit the program */
return 0;
}
/******************************************************************************/
Modified code is provided below. Explanation is given below. Output screenshot is also attached.
If you need any further clarification please feel free to ask in comments. let me now if it worked for you or not. iw ould really appreciate if you let me know if the explanation provided is to your satisfaction
###########################################################################
As you have mentioned, that your program is not calculating , but you have not mentioned it in clear manner. you also dint not provide the format of file. What is stored there. i have tried to understand the code and then tried to decode the format of file. What i have found is that the file has first line which tells number of enteries which is stored in ndata , and below it has BD and NT in each line like this:
If the file is not like this then provide me a comments below, and i will answer it again. If it is right then read the below explanation.
So i ran the program and found out that the program display everything except the value of BR. Value of BR is shown 0. So i am assuming that you are talking about this only.
Why is it showing 0? Look at the line no 56 in below code, where program calculates the value of BR. In your program you are calculating t like this: BR = 1/2*BD; , now what happens is that 1 and 2 both are integers, so when they divide they produce result 0 ad then final result in BR is zero. To avoid this we will use this : BR=1.0/2.0*BD; , now 1.0 ad 2.0 are double and result will be 0.5 . and now the calculation will be allright.
Additional information: As you are dealing with Files, You should know sometimes we done have the permission of modification in our system. So when we try to create something or modify something, it does not reflect. What i mean to say is , take an example, In windows System generally we dont have permission of modification in Disk C , Now if we try to create some file, it wont be created. As in our program, if the program runs and the power.txt file is not created , just change the path of outputfile to some different directory where there is permisiion for modification. Then the program will run perfectly and power.txt will be created in the given directory.
Below is the modified code, just focus on the line 56 and and 52, where i have done the modification.
###############################################################
WHOLE CODE
#include <stdio.h>
#define PI 3.14159
#define inputfile "C:\\Users\\16614\\Desktop\\ENG 200\\farms.txt"
#define outputfile "C:\\Users\\16614\\Desktop\\ENG 200\\power.txt"
/* Main function */
int main(void)
{
/* Declare variables */
double PW, PWM, PE, BD, BR, NT, A, V, P, CP, N;
int i, ndata;
FILE *INP = NULL, *OUTP = NULL;
/* Open Files */
INP = fopen(inputfile, "r");
OUTP = fopen(outputfile, "w");
/* Print headings */
printf("\n********************************************");
printf("\n WIND FARM CONFIGURATIONS"
"\n\nDIAMETER NUMBER OF TURBINES POWER"
"\n (m) (kW)");
fprintf(OUTP,"\n********************************************");
fprintf(OUTP,"\n WIND FARM CONFIGURATIONS"
"\n\nDIAMETER NUMBER OF TURBINES POWER"
"\n (m) (kW)");
/* Verify input file */
if(INP==NULL)
{printf("\n\n COULD NOT FIND FILE"
"\n PROGRAM TERMINATED"
"\n********************************************");
return 1;
}
else
{
/* Read Control numbers */
fscanf(INP, "%i",&ndata);
/* Calculate Values using Formulas and Print Results */
for(i=1;i<=ndata;i++)
{
fscanf(INP,"%lf %lf",&BD,&NT);
V = 9.5;
P = 1.2754;
CP = 0.48;
N = 0.33;
BR = 1.0/2.0*BD; //using 1.0/2.0 instead of 1/2 because 1/2=0 while, 1.0/2.0 is 0.5
A = PI*pow(BR,2);
PW = 1.0/2.0*P*A*pow(V,3); //using 1.0/2.0 instead of 1/2 because 1/2=0 while, 1.0/2.0 is 0.5
PWM = CP*PW;
PE = N*PWM;
printf("\n %3.1f %3.0f %5.1f",BD,NT,BR);
fprintf(OUTP,"\n %3.1f %3.0f %5.1f",BD,NT,BR);
}
}
printf("\n********************************************\n\n\n");
fprintf(OUTP,"\n********************************************\n\n\n");
/* Exit the program */
return 0;
}
OUTPUT PROOF