So my question is in regards to structs within the C programming language. I\'m
ID: 3866401 • Letter: S
Question
So my question is in regards to structs within the C programming language. I'm currently learning functions and their operations and the book is
talking about structs used for storing data. I want to use a struct to store generic information for the turbines I'm calculating. Specifically, I would
like to create a struct to hold the radius, speed and power. From what I have obtained, does this seem right.
typedef struct turbine_struct
{
float r;
float speed;
float power;
} turbine;
^^^^^^^ Am I on the right track? Thanks.
Here is the code, I'm just not sure how to implement this correctly. Thanks for any help.
#include
#include
#include
#define PI 3.14159
#define row 10
#define col 3
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
In this code logic is ok, but problem is in header line,
You have not include any header file (missing)
and you have not define Structure and PI value ,
So i will correct this error .
#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(" ");
}
}
If you like , then please give me a rating .
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.