Question

So I have a question in regards to my program. I'm learning to program in C...

So I have a question in regards to my program. I'm learning to program in C and I was curious about the use of functions. We don't

get into them in this class but I wanted to see how they work. Here is my program and I would like to create a function for each of my menu options.

I created a function to display and read the menu and the option that is entered, but I would like to see how I can make each menu option a function

as well as calculating the are and power as functions. Thanks. Any guidance would be great, the book doesn't go much into them.

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#define PI 3.14159

#define row 10

#define col 3

int menuOption(void);

int main()

{

//const int row = 10;

//const int col = 3;

int choice, loopcount, n = 1, flag = 1;

float x = 0.000001;

float tidalFlowSpeed;

float windSpeed;

float waterDensity = 1000;

float airDensity = 1.225;

float k = 0.40; //Let k = the power coefficient for wind.

float m = 0.35; //Let m = the power coefficient for marine.

float r, Area, Power;

float windTurbine[row][col];

float marineTurbine[row][col];

printf(" | uPower Corporation |\n");

printf("\t\t\t\t\t\t----------------------\n\n\n\n");

printf("Welcome analyst: |Chris Gorman| \n\n");

// set all to zero

for (int r = 0; r < row; ++r)

for (int c = 0; c < col; ++c)

{

windTurbine[r][c] = 0.0;

marineTurbine[r][c] = 0.0;

}

while (flag)

{

choice = menuOption();

switch (choice)

{

case 0:

printf("\n| User has exited |\n\n");

flag = 0;

break;//break case 0.

case 1:

printf("\n\n| Wind Turbine - Power Calculator |\n\n");

printf("User can enter up to 10 turbines.\n\n");

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", &r);

printf(">>Enter the wind speed for turbine |%d| in meters/second: ", loopcount);

scanf("%f", &windSpeed);

windTurbine[index][0] = r; //Turbine Radius

Area = PI * pow(r, 2); //Area

windTurbine[index][1] = windSpeed; //Wind Speed   

windTurbine[index][2] = ((airDensity * Area * pow(windSpeed, 3) * k) / 2) * x; //Power

printf("\n |Power = %.3f megawatts|\n\n", windTurbine[index][2]);

loopcount++;

printf("Data entered: %f : %f : %f\n", windTurbine[index][0], windTurbine[index][1], windTurbine[index][2]);

}

break;//break case 1.

case 2:

printf("\n\n| Marine Turbine - Power Calculator |\n\n");

printf("User can enter up to 10 turbines.\n\n");

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", &r);

printf(">>Enter the tidal flow speed for turbine |%d| in meters/second: ", loopcount);

scanf("%f", &tidalFlowSpeed);

marineTurbine[index][0] = r; //Turbine Radius

Area = PI * pow(r, 2); //Area

marineTurbine[index][1] = tidalFlowSpeed; //Tidal Flow Speed   

marineTurbine[index][2] = ((waterDensity * Area * pow(tidalFlowSpeed, 3) * k) / 2) * x; //Power

printf("\n |Power = %.3f megawatts|\n\n", marineTurbine[index][2]);

loopcount++;

printf("Data entered: %f : %f : %f\n", marineTurbine[index][0], marineTurbine[index][1], marineTurbine[index][2]);

}

break;//break case 2.

case 3:

printf("\n\| Analysis - Marine Turbine Specifications |\n\n");

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);

r = sqrt((2 * Power) / (waterDensity * PI * pow(tidalFlowSpeed, 3) * m)) * 1000;

printf("\n |Radius = %.3f meters|\n\n", r);

printf(">>The marine turbine radius has to be %.3f meters in order to produce %.3f megawatts of power.\n\n", r, Power);

break;//break case 3.

case 4:

printf("\n| Wind Turbine Data |:\n\n");

printf("Turbine Radius(m)\t\tWind Speed (m/sec)\t\tPower (MW)\n");

for (int r = 0; r < row; ++r)

if (windTurbine[r][0] != 0.0)

{

for (int c = 0; c < col; ++c)

printf("%f\t\t\t", windTurbine[r][c]);

printf("\n");

}

printf("\n| Marine Turbine Data |:\n\n");

printf("Turbine Radius(m)\t\tTidal Flow Speed (m/sec)\tPower (MW)\n");

for (int r = 0; r < row; ++r)

if (marineTurbine[r][0] != 0.0)

{

for (int c = 0; c < col; ++c)

printf("%f\t\t\t", marineTurbine[r][c]);

printf("\n");

}

break;//break case 4.

default:

printf("\n\nInvalid choice.\nPlease choose a valid menu option.\n\n");

break;//break default.

}//end swtich.

}//end while (flag).

}//end main.

int menuOption(void)

{

int choice = 0;

printf("\n----------MENU----------\n\n");

printf(" >> 0: Exit\n\n");

printf(" >> 1: Wind Turbine\n\n");

printf(" >> 2: Marine Turbine\n\n");

printf(" >> 3: Analysis\n\n");

printf(" >> 4: Turbine Table Data\n\n");

printf("\nPlease enter your choice: ");

scanf("%d", &choice);

return choice;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#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 windTurbineDat(float windTurbine[row][col],float marineTurbine[row][col]);
int main()

{

    //const int row = 10;

    //const int col = 3;

    int choice, loopcount, n = 1, flag = 1;

    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];
    int r, c;
    printf(" | uPower Corporation |\n");

    printf("----------------------\n\n\n\n");

    printf("Welcome analyst: |Chris Gorman| \n\n");

    // set all to zero

    for (r = 0; r < row; ++r)

        for (c = 0; c < col; ++c)

        {

            windTurbine[r][c] = 0.0;

            marineTurbine[r][c] = 0.0;
        }

    while (flag)

    {

        choice = menuOption();

        switch (choice)

        {

        case 0:

            printf("\n| User has exited |\n\n");

            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:
           windTurbineDat(windTurbine,marineTurbine);
            break; //break case 4.

        default:
          
            printf("\n\nInvalid choice.\nPlease choose a valid menu option.\n\n");

            break; //break default.

        } //end swtich.
    } //end while (flag).

} //end main.

int menuOption(void)

{

    int choice = 0;

    printf("\n----------MENU----------\n\n");

    printf(" >> 0: Exit\n\n");

    printf(" >> 1: Wind Turbine\n\n");

    printf(" >> 2: Marine Turbine\n\n");

    printf(" >> 3: Analysis\n\n");

    printf(" >> 4: Turbine Table Data\n\n");

    printf("\nPlease enter your choice: ");

    scanf("%d", &choice);

    return choice;
}

void windTurbinePowerCalculator(float windTurbine[row][col],float x,float k,float airDensity)
{
    int loopcount, n = 1;
    float Area, windSpeed;
    int r, c;
    printf("\n\n| Wind Turbine - Power Calculator |\n\n");

    printf("User can enter up to 10 turbines.\n\n");

    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", &r);

        printf(">>Enter the wind speed for turbine |%d| in meters/second: ", loopcount);

        scanf("%f", &windSpeed);

        windTurbine[index][0] = r; //Turbine Radius

        Area = PI * pow(r, 2); //Area

        windTurbine[index][1] = windSpeed; //Wind Speed

        windTurbine[index][2] = ((airDensity * Area * pow(windSpeed, 3) * k) / 2) * x; //Power

        printf("\n |Power = %.3f megawatts|\n\n", windTurbine[index][2]);

        loopcount++;

        printf("Data entered: %f : %f : %f\n", 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;

    float tidalFlowSpeed;

    float Area;
    int r, c;

    printf("\n\n| Marine Turbine - Power Calculator |\n\n");

    printf("User can enter up to 10 turbines.\n\n");

    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", &r);

        printf(">>Enter the tidal flow speed for turbine |%d| in meters/second: ", loopcount);

        scanf("%f", &tidalFlowSpeed);

        marineTurbine[index][0] = r; //Turbine Radius

        Area = PI * pow(r, 2); //Area

        marineTurbine[index][1] = tidalFlowSpeed; //Tidal Flow Speed

        marineTurbine[index][2] = ((waterDensity * Area * pow(tidalFlowSpeed, 3) * k) / 2) * x; //Power

        printf("\n |Power = %.3f megawatts|\n\n", marineTurbine[index][2]);

        loopcount++;

        printf("Data entered: %f : %f : %f\n", marineTurbine[index][0], marineTurbine[index][1], marineTurbine[index][2]);
    }
}

void analysisMarineTurbine(float m,float waterDensity)
{
    float Power, tidalFlowSpeed;
    int r;
    printf("\n\n| Analysis - Marine Turbine Specifications |\n\n");

    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);

    r = sqrt((2 * Power) / (waterDensity * PI * pow(tidalFlowSpeed, 3) * m)) * 1000;

    printf("\n |Radius = %.3f meters|\n\n", r);

    printf(">>The marine turbine radius has to be %.3f meters in order to produce %.3f megawatts of power.\n\n", r, Power);
}

void windTurbineDat(float windTurbine[row][col],float marineTurbine[row][col])
{
   int r,c;
    printf("\n| Wind Turbine Data |:\n\n");

    printf("Turbine Radius(m)\t\tWind Speed (m/sec)\t\tPower (MW)\n");

    for (r = 0; r < row; ++r)

        if (windTurbine[r][0] != 0.0)

        {

            for (c = 0; c < col; ++c)

                printf("%f\t\t\t", windTurbine[r][c]);

            printf("\n");
        }

    printf("\n| Marine Turbine Data |:\n\n");

    printf("Turbine Radius(m)\t\tTidal Flow Speed (m/sec)\tPower (MW)\n");

    for (r = 0; r < row; ++r)

        if (marineTurbine[r][0] != 0.0)

        {
            for (c = 0; c < col; ++c)
                printf("%f\t\t\t", marineTurbine[r][c]);

            printf("\n");
        }
}

Output:

Add a comment
Know the answer?
Add Answer to:
So I have a question in regards to my program. I'm learning to program in C...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C programming - This is what I have so far but it is not working correctly....

    C programming - This is what I have so far but it is not working correctly. It must contain the function and it also needs a provision to write output to a file. Can anyone help? #include <stdio.h> I #include <math.h int main (void) int V float Rint, RLE0, 0; printf New power supply voltage scanf ("td", &V) printf Enter a valid Internal Resistance (Rint) range between 200 and 5k ohms: scanf ("tf", Rint if (V> 1 && VK 15)...

  • Identify the errors in this C program (struct - calculating volume, area of cylinder). Explain. #include...

    Identify the errors in this C program (struct - calculating volume, area of cylinder). Explain. #include <stdio.h> #define PI 3.14 typedef struct cylinder{    float r,h;    float areacircle=2*PI*r;    float areacylinder;    float volumecylinder;    areacylinder=2*PI*r*h+2*PI*r*r;    volumecylinder=PI*r*r*h; }; int main() {    float start,numcyls;    printf("How many cylinders do you want? Enter a positive integer.\n");    scanf("%f",&numcyls);    if(numcyls<1||ceilf(numcyls)!=numcyls)    while()    {printf("The number you have entered is not a positive integer. Please try again.\n");        printf("Remember,...

  • I need a basic program in C to modify my program with the following instructions: Create...

    I need a basic program in C to modify my program with the following instructions: Create a program in C that will: Add an option 4 to your menu for "Play Bingo" -read in a bingo call (e,g, B6, I17, G57, G65) -checks to see if the bingo call read in is valid (i.e., G65 is not valid) -marks all the boards that have the bingo call -checks to see if there is a winner, for our purposes winning means...

  • I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the...

    I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the program until they enter "q" to quit. However, my program runs through one time, the prompt appears again, but then it terminates before the user is allowed to respond to the prompt again. I'm not able to debug why this...

  • C++ HELP I need help with this program. I have done and compiled this program in...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...

  • I am trying to add a string command to my code. what am i doing wrong?...

    I am trying to add a string command to my code. what am i doing wrong? #define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <stdio.h> #include <string.h> #include <math.h> int main(void) {    int i, j;    int rowA, colA, rowB, colB;    int A[10][10], B[10][10];    int sum[10][10];    char str1[10];    printf("This is a matrix calculator\n");    //read in size from user MATRIX A    printf("Enter in matrix A....\n");    printf("\t#row = ");    scanf("%d", &rowA);    printf("\t#col = ");   ...

  • I need little help with C language. I need to pass what I get from HexToBin(char*...

    I need little help with C language. I need to pass what I get from HexToBin(char* hexdec) into char* input so that what I got there it should pass it as string array parametr. Example: Enter IEEE-Hex: 40200000 Equivalent Binary value is : 01000000001000000000000000000000 Decimal Number: 2.5 #include <stdio.h> void HexToBin(char* hexdec) {    long int i = 0;    while (hexdec[i]) {        switch (hexdec[i]) {        case '0':            printf("0000");            break;...

  • I'm having trouble rounding the numbers i'm getting in my output here's my code: #include<stdio.h> int...

    I'm having trouble rounding the numbers i'm getting in my output here's my code: #include<stdio.h> int main() { char gender; float a1, a2, a3, a4, a5; float waistmeasurement, wristmeasurement, hipmeasurement, forarmmeasurement; float B, bodyweight, Bodyfat, Bodyfatpercentage;    printf("This program determines the body fat of a person.Enter your gender (f|F|m|M): "); scanf("%c", &gender); printf("\n");    if(gender=='F' || gender=='f'){ printf("Enter body weight (in pounds): \n"); scanf("%f", &bodyweight); printf("Enter wrist measurement at fullest point (in inches): \n"); scanf("%f", &wristmeasurement); printf("Enter waist measurement at...

  • Question- How would I allow the program to run both upper and lower case letters. How...

    Question- How would I allow the program to run both upper and lower case letters. How would I write a switch statement for upper and lower cases to see if the value entered for Grade2 is a A or a? C programming int main() { char Grade2; float gradepoint; char Grade = 'X'; // Declares a character type variable named Grade printf("Enter a grade\t"); // Prompts for Grade scanf("%c", &Grade); // Inputs Grade printf("Grade is: \t%c\n", Grade); // Prints the...

  • Why when i enter A, and the second scanf founction is not work, there are no...

    Why when i enter A, and the second scanf founction is not work, there are no value for cis. int main() char in, Cls; int r-0; printf("A: Circle nB: Square\nC: Triangle\n"); printf("Enter Which Shape to use: \n"); scanf("%c", &in); switch(in) [ case A' printf("Enter P for solve for perimeter and A for solve for Area\n"); printf("Choice: "); scanf(%c", &cis); / /cis is choice printf("Enter circle radius: "); scanf("%lf", &r); printf("Circle perimeterr is:", perimeter('A', 2, 0, e)); return 0;

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT