0

So i'm in the process of learning about functions and I'm not grasping the full operation of them. I have a menu in which I have utilized a function but I want to create a fucntion for each of the menu options. I've heard of pointers but I haven't researched them enough to know what they are. I'm also trying to get the area and the power to calculate in their own functions as well. Can anyone give me a basic breakdown of how I can do that! Thanks. I'm new to programming and I came across functions and how they can make things easier so I curious to see how they work.

#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;
}

Edited by Chris_45

2
Contributors
1
Reply
20
Views
2 Weeks
Discussion Span
Last Post by ddanbe
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.