Question

In: Computer Science

Can you please tell me why my code isn't working? It won't calculate the values I...

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;
}
/******************************************************************************/

Solutions

Expert Solution

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


Related Solutions

I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------ class Robot{ int serialNumber; boolean flies,autonomous,teleoperated; public void setCapabilities(int serialNumber, boolean flies, boolean autonomous, boolean teleoperated){ this.serialNumber = serialNumber; this.flies = flies; this.autonomous = autonomous; this.teleoperated = teleoperated; } public int getSerialNumber(){ return this.serialNumber; } public boolean canFly(){ return this.flies; } public boolean isAutonomous(){ return this.autonomous; } public boolean isTeleoperated(){ return this.teleoperated; } public String getCapabilities(){ StringBuilder str = new StringBuilder(); if(this.flies){str.append("canFly");str.append(" ");} if(this.autonomous){str.append("autonomous");str.append("...
My Javascript code isn't working (when i press calculate button) - what's wrong with it ?...
My Javascript code isn't working (when i press calculate button) - what's wrong with it ? Car Rental Enter Number of Rental Days: Select Car Type: onclick= "priceofcar = 50;"/> Compact onclick= "priceofcar = 60;"/> Economy onclick= "priceofcar = 70;"/> Intermediate Select Loss Damage Waiver onclick= "damagewaiver='yes'"/> Yes onclick= "damagewaiver='no'"/> No damagewaiver = boxDays.value * 25;} else if (damagewaiver == No) { damagewaiver = 0; }/> Select Roadside Issues Coverage: onclick= "issuescoverage='yes'"/> Yes onclick= "issuescoverage='no'"/> No issuescoverage = boxDays.value *...
Can someone tell me how to fix warning msg in my code of C ++? I...
Can someone tell me how to fix warning msg in my code of C ++? I got run-time error for this question please help me asap! Errors are: In function 'void bfs(int, int)': warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(int j = 0; j < adj[pppp].size(); j++){ ^ In function 'int main()': warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result] scanf("%d %d %d %d %d", &a, &q, &c, &N, &m); ^...
I was wondering if you can tell me if the following code is correct and if...
I was wondering if you can tell me if the following code is correct and if its not can it be fixed so it does not have any syntax errors. Client one /** * Maintains information on an insurance client. * * @author Doyt Perry/<add your name here> * @version Fall 2019 */ public class Client { // instance variables private String lastName; private String firstName; private int age; private int height; private int weight; /** * First constructor for...
Can you please tell me if this code needs to be fixed, if it does can...
Can you please tell me if this code needs to be fixed, if it does can you please post the fixed code below please and thank you /** * Models a Whole Life Policy. * * @author Tina Comston * @version Fall 2019 */ public class WholeLifePolicy { // instance variables    private double faceValue; // your code here - code the remaining instance field // constants /** * surrender rate. */ public static final double SURRENDER_RATE = .65; /**...
Can you please tell me if this code needs to be fixed, if it does can...
Can you please tell me if this code needs to be fixed, if it does can you please post the fixed code below please and thank you /** * Driver to demonstrate WholeLifePolicy class. * * @author Tina Comston * @version Fall 2019 */ public class WholeLifePolicyDriver { /** * Creates WholeLifePolicy object, calls methods, displays values. * */ public static void main() { WholeLifePolicyDriver myDriver = new WholeLifePolicyDriver(); // create a policy WholeLifePolicy policy = new WholeLifePolicy("WLP1234567", 50000, 20);...
Can anyone just check my code and tell me why the program doesn't show the end...
Can anyone just check my code and tell me why the program doesn't show the end date & stops at the date before the end date? Tell me how i can fix it. Question: Write a program compare.cpp that asks the user to input two dates (the beginning and the end of the interval). The program should check each day in the interval and report which basin had higher elevation on that day by printing “East” or “West”, or print...
Please tell me why you enjoy studying languages. I can accept why you don't like to...
Please tell me why you enjoy studying languages. I can accept why you don't like to studying language. Please be detail it
I need to fix this code, and could you please tell me what was the problem...
I need to fix this code, and could you please tell me what was the problem options 1 and 9 don't work #include <stdio.h> #include <time.h> #include <stdlib.h> // generate a random integer between lower and upper values int GenerateRandomInt(int lower, int upper){     int num =(rand()% (upper - lower+1))+lower;     return num; } // use random numbers to set the values of the matrix void InitializeMatrix(int row, int column, int dimension, int mat[][dimension]){     for(int i =0; i<row; i++){...
Okay, can someone please tell me what I am doing wrong?? I will show the code...
Okay, can someone please tell me what I am doing wrong?? I will show the code I submitted for the assignment. However, according to my instructor I did it incorrectly but I am not understanding why. I will show the instructor's comment after providing my original code for the assignment. Thank you in advance. * * * * * HourlyTest Class * * * * * import java.util.Scanner; public class HourlyTest {    public static void main(String[] args)     {        ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT