Hello all, I have a question in regards to functions. I have my program and I ha
ID: 3866480 • Letter: H
Question
Hello all, I have a question in regards to functions. I have my program and I have implemented functions when asked, but if I wanted to calculate the area and the power in their own
functions, how could I incorporate this into my code. I've tried a few ways and my compiler is not liking it. I'm not sure if my logic is correct nor my syntax. Everything else is working
as it should but my code for the individual functions of area and power will not work. We've been trying for the las few hours and can't seem to get it right. Any help would be great, thanks
in regards! Here is the code as it is!
#include<stdio.h>
#include<string.h>
#include<math.h>
#define row 10
#define col 3
#define PI 3.147
typedef struct turbine_struct
{
float r;
float speed;
float power;
} turbine;
int menuOption(void);
void windTurbinePowerCalculator(float windTurbine[row][col], float x, float k, float airDensity);
void marineTurbinePowerCalculator(float marineTurbine[row][col], float x, float k, float m, float waterDensity);
void analysisMarineTurbine(float m, float waterDensity);
void TurbineDat(float windTurbine[row][col], float marineTurbine[row][col]);
int main()
{
int choice, loopcount, n = 1, flag = 1;
int ROW, c;
float x = 0.000001;
float tidalFlowSpeed;
float airDensity = 1.225;
float waterDensity = 1000;
float k = 0.40; //Let k = the power coefficient for wind.
float m = 0.35; //Let m = the power coefficient for marine.
float Area, Power;
float windTurbine[row][col];
float marineTurbine[row][col];
printf(" | uPower Corporation | ");
printf("---------------------- ");
printf("Welcome analyst: |Chris Gorman| ");
// set all to zero
for (ROW = 0; ROW < row; ++ROW)
for (c = 0; c < col; ++c)
{
windTurbine[ROW][c] = 0.0;
marineTurbine[ROW][c] = 0.0;
}
while (flag)
{
choice = menuOption();
switch (choice)
{
case 0:
printf(" | User has exited | ");
flag = 0;
break; //break case 0.
case 1:
windTurbinePowerCalculator(windTurbine, x, k, airDensity);
break; //break case 1.
case 2:
marineTurbinePowerCalculator(marineTurbine, x, k, m, waterDensity);
break; //break case 2.
case 3:
analysisMarineTurbine(m, waterDensity);
break; //break case 3.
case 4:
TurbineDat(windTurbine, marineTurbine);
break; //break case 4.
default:
printf(" Invalid choice. Please choose a valid menu option. ");
break; //break default.
} //end swtich.
} //end while (flag).
} //end main.
int menuOption(void)
{
int choice = 0;
printf(" ----------MENU---------- ");
printf(" >> 0: Exit ");
printf(" >> 1: Wind Turbine ");
printf(" >> 2: Marine Turbine ");
printf(" >> 3: Analysis ");
printf(" >> 4: Turbine Table Data ");
printf(" Please enter your choice: ");
scanf("%d", &choice);
return choice;
}
void windTurbinePowerCalculator(float windTurbine[row][col], float x, float k, float airDensity)
{
int loopcount, n = 1;
int c;
float Area, windSpeed;
float Radius;
printf(" | Wind Turbine - Power Calculator | ");
printf("User can enter up to 10 turbines. ");
printf(">>Enter the number of turbines to be calculated: ");
scanf("%d", &n);
loopcount = 1;
while (loopcount <= n && loopcount < row + 1)
{
int index = loopcount - 1;
printf(">>Enter the radius for turbine |%d| in meters: ", loopcount);
scanf("%f", &Radius);
printf(">>Enter the wind speed for turbine |%d| in meters/second: ", loopcount);
scanf("%f", &windSpeed);
windTurbine[index][0] = Radius; //Turbine Radius
Area = PI * pow(Radius, 2); //Area
windTurbine[index][1] = windSpeed; //Wind Speed
windTurbine[index][2] = ((airDensity * Area * pow(windSpeed, 3) * k) / 2) * x; //Power
printf(" |Power = %.3f megawatts| ", windTurbine[index][2]);
loopcount++;
printf("Data entered: %f : %f : %f ", windTurbine[index][0], windTurbine[index][1], windTurbine[index][2]);
}
}
void marineTurbinePowerCalculator(float marineTurbine[row][col], float x, float k, float m, float waterDensity)
{
int loopcount, n = 1, flag = 1;
int c;
float tidalFlowSpeed;
float Area;
float Radius;
printf(" | Marine Turbine - Power Calculator | ");
printf("User can enter up to 10 turbines. ");
printf(">>Enter the number of marine turbines to be calculated: ");
scanf("%d", &n);
loopcount = 1;
while (loopcount <= n && loopcount < row + 1)
{
int index = loopcount - 1;
printf(">>Enter the radius for turbine |%d| in meters: ", loopcount);
scanf("%f", &Radius);
printf(">>Enter the tidal flow speed for turbine |%d| in meters/second: ", loopcount);
scanf("%f", &tidalFlowSpeed);
marineTurbine[index][0] = Radius; //Turbine Radius
Area = PI * pow(Radius, 2); //Area
marineTurbine[index][1] = tidalFlowSpeed; //Tidal Flow Speed
marineTurbine[index][2] = ((waterDensity * Area * pow(tidalFlowSpeed, 3) * k) / 2) * x; //Power
printf(" |Power = %.3f megawatts| ", marineTurbine[index][2]);
loopcount++;
printf("Data entered: %f : %f : %f ", marineTurbine[index][0], marineTurbine[index][1], marineTurbine[index][2]);
}
}
void analysisMarineTurbine(float m, float waterDensity)
{
float Power, tidalFlowSpeed, Radius;
printf(" | Analysis - Marine Turbine Specifications | ");
printf(">>Enter the power of a wind turbine in megawatts: ");
scanf("%f", &Power);
printf(">>Enter the tidal flow speed in meters/second: ");
scanf("%f", &tidalFlowSpeed);
Radius = sqrt((2 * Power) / (waterDensity * PI * pow(tidalFlowSpeed, 3) * m)) * 1000;
printf(" |Radius = %.3f meters| ", Radius);
printf(">>The marine turbine radius has to be %.3f meters in order to produce %.3f megawatts of power. ", Radius, Power);
}
void TurbineDat(float windTurbine[row][col], float marineTurbine[row][col])
{
int ROW, c;
printf(" | Wind Turbine Data |: ");
printf("Turbine Radius(m) Wind Speed (m/sec) Power (MW) ");
for (ROW = 0; ROW < row; ++ROW)
if (windTurbine[ROW][0] != 0.0)
{
for (c = 0; c < col; ++c)
printf("%f ", windTurbine[ROW][c]);
printf(" ");
}
printf(" | Marine Turbine Data |: ");
printf("Turbine Radius(m) Tidal Flow Speed (m/sec) Power (MW) ");
for (ROW = 0; ROW < row; ++ROW)
if (marineTurbine[ROW][0] != 0.0)
{
for (c = 0; c < col; ++c)
printf("%f ", marineTurbine[ROW][c]);
printf(" ");
}
}
Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<math.h>
#define row 10
#define col 3
#define PI 3.147
typedef struct turbine_struct
{
float r;
float speed;
float power;
} turbine;
int menuOption(void);
float area(float radius);
float power(float Density,float Area,float Speed,float k,float x);
void windTurbinePowerCalculator(float windTurbine[row][col], float x, float k, float airDensity);
void marineTurbinePowerCalculator(float marineTurbine[row][col], float x, float k, float m, float waterDensity);
void analysisMarineTurbine(float m, float waterDensity);
void TurbineDat(float windTurbine[row][col], float marineTurbine[row][col]);
int main()
{
int choice, loopcount, n = 1, flag = 1;
int ROW, c;
float x = 0.000001;
float tidalFlowSpeed;
float airDensity = 1.225;
float waterDensity = 1000;
float k = 0.40; //Let k = the power coefficient for wind.
float m = 0.35; //Let m = the power coefficient for marine.
float Area, Power;
float windTurbine[row][col];
float marineTurbine[row][col];
printf(" | uPower Corporation | ");
printf("---------------------- ");
printf("Welcome analyst: |Chris Gorman| ");
// set all to zero
for (ROW = 0; ROW < row; ++ROW)
for (c = 0; c < col; ++c)
{
windTurbine[ROW][c] = 0.0;
marineTurbine[ROW][c] = 0.0;
}
while (flag)
{
choice = menuOption();
switch (choice)
{
case 0:
printf(" | User has exited | ");
flag = 0;
break; //break case 0.
case 1:
windTurbinePowerCalculator(windTurbine, x, k, airDensity);
break; //break case 1.
case 2:
marineTurbinePowerCalculator(marineTurbine, x, k, m, waterDensity);
break; //break case 2.
case 3:
analysisMarineTurbine(m, waterDensity);
break; //break case 3.
case 4:
TurbineDat(windTurbine, marineTurbine);
break; //break case 4.
default:
printf(" Invalid choice. Please choose a valid menu option. ");
break; //break default.
} //end swtich.
} //end while (flag).
} //end main.
int menuOption(void)
{
int choice = 0;
printf(" ----------MENU---------- ");
printf(" >> 0: Exit ");
printf(" >> 1: Wind Turbine ");
printf(" >> 2: Marine Turbine ");
printf(" >> 3: Analysis ");
printf(" >> 4: Turbine Table Data ");
printf(" Please enter your choice: ");
scanf("%d", &choice);
return choice;
}
void windTurbinePowerCalculator(float windTurbine[row][col], float x, float k, float airDensity)
{
int loopcount, n = 1;
int c;
float Area, windSpeed;
float Radius;
printf(" | Wind Turbine - Power Calculator | ");
printf("User can enter up to 10 turbines. ");
printf(">>Enter the number of turbines to be calculated: ");
scanf("%d", &n);
loopcount = 1;
while (loopcount <= n && loopcount < row + 1)
{
int index = loopcount - 1;
printf(">>Enter the radius for turbine |%d| in meters: ", loopcount);
scanf("%f", &Radius);
printf(">>Enter the wind speed for turbine |%d| in meters/second: ", loopcount);
scanf("%f", &windSpeed);
windTurbine[index][0] = Radius; //Turbine Radius
Area = area(Radius); //Area
windTurbine[index][1] = windSpeed; //Wind Speed
windTurbine[index][2] = power(airDensity,Area,windSpeed,k,x); //Power
printf(" |Power = %.3f megawatts| ", windTurbine[index][2]);
loopcount++;
printf("Data entered: %f : %f : %f ", windTurbine[index][0], windTurbine[index][1], windTurbine[index][2]);
}
}
void marineTurbinePowerCalculator(float marineTurbine[row][col], float x, float k, float m, float waterDensity)
{
int loopcount, n = 1, flag = 1;
int c;
float tidalFlowSpeed;
float Area;
float Radius;
printf(" | Marine Turbine - Power Calculator | ");
printf("User can enter up to 10 turbines. ");
printf(">>Enter the number of marine turbines to be calculated: ");
scanf("%d", &n);
loopcount = 1;
while (loopcount <= n && loopcount < row + 1)
{
int index = loopcount - 1;
printf(">>Enter the radius for turbine |%d| in meters: ", loopcount);
scanf("%f", &Radius);
printf(">>Enter the tidal flow speed for turbine |%d| in meters/second: ", loopcount);
scanf("%f", &tidalFlowSpeed);
marineTurbine[index][0] = Radius; //Turbine Radius
Area = area(Radius); //Area
marineTurbine[index][1] = tidalFlowSpeed; //Tidal Flow Speed
marineTurbine[index][2] = power(waterDensity,Area,tidalFlowSpeed,k,x); //Power
printf(" |Power = %.3f megawatts| ", marineTurbine[index][2]);
loopcount++;
printf("Data entered: %f : %f : %f ", marineTurbine[index][0], marineTurbine[index][1], marineTurbine[index][2]);
}
}
void analysisMarineTurbine(float m, float waterDensity)
{
float Power, tidalFlowSpeed, Radius;
printf(" | Analysis - Marine Turbine Specifications | ");
printf(">>Enter the power of a wind turbine in megawatts: ");
scanf("%f", &Power);
printf(">>Enter the tidal flow speed in meters/second: ");
scanf("%f", &tidalFlowSpeed);
Radius = sqrt((2 * Power) / (waterDensity * PI * pow(tidalFlowSpeed, 3) * m)) * 1000;
printf(" |Radius = %.3f meters| ", Radius);
printf(">>The marine turbine radius has to be %.3f meters in order to produce %.3f megawatts of power. ", Radius, Power);
}
void TurbineDat(float windTurbine[row][col], float marineTurbine[row][col])
{
int ROW, c;
printf(" | Wind Turbine Data |: ");
printf("Turbine Radius(m) Wind Speed (m/sec) Power (MW) ");
for (ROW = 0; ROW < row; ++ROW)
if (windTurbine[ROW][0] != 0.0)
{
for (c = 0; c < col; ++c)
printf("%f ", windTurbine[ROW][c]);
printf(" ");
}
printf(" | Marine Turbine Data |: ");
printf("Turbine Radius(m) Tidal Flow Speed (m/sec) Power (MW) ");
for (ROW = 0; ROW < row; ++ROW)
if (marineTurbine[ROW][0] != 0.0)
{
for (c = 0; c < col; ++c)
printf("%f ", marineTurbine[ROW][c]);
printf(" ");
}}
float area(float radius)
{
return PI * pow(radius, 2);
}
float power(float Density,float Area,float Speed,float k,float x)
{
return ((Density * Area * pow(Speed, 3) * k) / 2) * x;
}
working code with 2 separate functions for power and area
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.