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

3
Contributors
4
Replies
34
Views
2 Months
Discussion Span
Last Post by Joris Claassen
0

If your question about using functions could be (more or less) translated into 'how can I change this big switch with little overview into a switch with good overview using calls to (selfmade) functions' ...., then this could be step 1 (leading to not compiling/working code!)

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define PI 3.14159
#define row 10
#define col 3
int menuOption(void);

int case0(void){
    printf("\n| User has exited |\n\n");
    flag = 0;
}
int case1(void){
    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]);
    }
}
int case2(void){
    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]);
    }
}
int case3(void){
    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);
}
int case4(void){
    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");
        }
}
int caseElse(void){
    printf("\n\nInvalid choice.\nPlease choose a valid menu option.\n\n");
}

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:   // Does case0 need any data?
                  // Does it make sense to set flag by case0?
            case0();
            flag = 0;
            break;//break case 0.
        case 1:   // Does case1 need any data? 
            case1();
            break;//break case 1.
        case 2:   // Does case2 need any data?
            case2();
            break;//break case 2.
        case 3:   // Does case3 need any data?
            case3();
            break;//break case 3.
        case 4:   // Does case4 need any data?
            case4();
            break;//break case 4.
        default:   // Does caseElse need any data?
            caseElse();
            break;//break default.
        }//end swtich.
    }//end while (flag).
}//end main.

// no need to change anything for: menuOption(void)

[Of course you could choose for function names that describe better what they do/how they act! ]

For each variable defined in main you should ask yourself:
    • Isn't this actually a constant? --> candidate for #define ...
    • Is it still in use by main? (After moving the code to the case… functions.)
   • If not in use by main: is it in use by only one function? ---> Move variable definition to function case…?
    • If not in use by main, but in use by more functions: are their values actually shared? If not, then define in each case… function.
    • If in use by main and at least one case… function, or in use by more than one case… function: how to exchange the values?

0

After analysing the code I have noticed that:
     • x, k, m, waterDensity and airDensity are actually constants. You can define them at 'file level' as constants (with const int and const fload) or use #define.
     • the float variables tidalFlowSpeed, Area, r occur in more functions but their value isn't supposed to be shared across them! They just happen to have the same name but they could have had different names keeping the same functionality. So their definition can be moved from main to the case… functions.
     • only the values of windTurbine and marineTurbine are shared across main and several case… functions. I think the most efficient way to achieve this is to define them at 'file level'. Otherwise you have to call those functions with pointer to these variables.

After I have noticed this, I have made below changes

// Force « float » for waterDensity by adding « .0 »!
#define  x  0.000001 
#define  waterDensity   1000.0 
#define  airDensity  1.225 

    //Let k = the power coefficient for wind.
#define   k  0.40  
    //Let m = the power coefficient for marine.
#define   m  0.35  

static float windTurbine[row][col];
static float marineTurbine[row][col];

int menuOption(void);
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;
}

int  case0(void){
    printf("\n| User has exited |\n\n");
    //? flag = 0;
    return 0;
}
int  case1(void){
    float r;
    int n = 1;
    float Area;
    float windSpeed;
    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);
    int 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]);
    }
    return 0;
}
int  case2(void){
    float tidalFlowSpeed;
    float r;
    int n = 1;
    float Area;
    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);
    int 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]);
    }
    return 0;
}
int  case3(void){
    float tidalFlowSpeed;
    float Power;
    float r;
    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);
    return 0;
}
int  case4(void){
    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");
        }
    return 0;
}
int  caseElse(void){
    printf("\n\nInvalid choice.\nPlease choose a valid menu option.\n\n");
    return 0;
}

int main()
{
      //const int row = 10;
      //const int col = 3;   // Which variables are still in use by main?
      // Variable definitions moved to 'file level' or case… !
            // int loopcount, n = 1, 
            // float r, Area, Power, windSpeed, …Turbine…

           int choice, flag = 1;
           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
      /// No more changes below this line!
0

If you don't want to define

static float windTurbine[row][col];
static float marineTurbine[row][col];

at 'file level', then you can keep the definition in main and use arguments in the function definitions by changing  int case1(void){ into   int case1(float windTurbine[row][col]){ , likewise  int case2(void){ into   int case2(float marineTurbine[row][col]){ and  int case4(void){ into int case4(float windTurbine[row][col], float marineTurbine[row][col]){
Which works because row and col are defined with #define!

But you have asked for pointers ....
(without using typedefs I couldn't manage to get a working cast ..... so I have added typedef float f2Da[row][col]; to the 'common definitions section' to make below changes possible/working)

I have changed  int case1(void){ into

       int case1(void* wndTurb){
            f2Da* windTurbine = (f2Da*) wndTurb;

, likewise  int case2(void){ into

     int  case2(void* marnTurb){
            f2Da* marineTurbine = (f2Da*) marnTurb;

and     int case4(void){ into

     int  case4(void* wndTurb, void* marnTurb){
           f2Da* windTurbine = (f2Da*) wndTurb;
           f2Da* marineTurbine = (f2Da*) marnTurb;

Above changes required additional * in the functions case1, case2 and case4, leading to:
   *windTurbine[index] instead of windTurbine[index] and
   *marineTurbine[index] instead of marineTurbine[index]!

Finally the required changes in main:

    case 1:  
        case1((float**) windTurbine);
        break;//break case 1.
    case 2:  
        case2((float**) marineTurbine);
        break;//break case 2.
    case 3:   // Does case3 need any data?
        case3();
        break;//break case 3.
    case 4:  
        case4((float**) windTurbine, (float**)marineTurbine);
        break;//break case 4.

You might wonder the need for type int of these functions. Actually you can use void and skip return 0;.
In case0 and caseElse you could make use of the return type like this:

(The pure-functional way ...)

int  case0(int flag){
    printf("\nDo you really want to exit? n: no continue, any other key (like enter) to confirm exit!   [Accidentically choosen 'wrong option': %d] ", flag - 1);
    char c = _getche();  // requires  #include <conio.h>  otherwise getchar/scanf needs additional enter ... 
    if( c == 'n' ){
        return ++flag;
    }
    printf("\n| User has exited |\n\n");
    //? flag = 0;
    return 0;
}

int  caseElse(int flag){
    printf("\n\nInvalid choice.\nPlease choose a valid menu option. [Accidentically choosen 'wrong option': %d]\n\n", flag - 1);
    return ++flag;
}

and at main

    case 0:
        flag = case0(flag);
        break;//break case 0.
....
    default:
        flag = caseElse(flag);
        break;//break default.

or using references (aka not editable pointers: editable as in changing the memory location)

void case0(int* flag){
    printf("\nDo you really want to exit? n: no continue, any other key (like enter) to confirm exit!   [Accidentically choosen 'wrong option': %d] ", *flag - 1);
    char c = _getche();  // requires  #include <conio.h>  otherwise getchar/scanf needs additional enter ... 
    if( c == 'n' ){
        (*flag)++;
    } else {
            printf("\n| User has exited |\n\n");
        *flag = 0;
    } 
}

int  caseElse(int* flag){
    printf("\n\nInvalid choice.\nPlease choose a valid menu option. [Accidentically choosen 'wrong option': %d]\n\n", *flag - 1);
    (*flag)++;
}

and at main

    case 0: 
        case0(&flag);
        break;//break case 0.
....
    default:
        caseElse(&flag);
        break;//break default.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.