Hey, it's me again, with another problem on an assignment.

The input/output looks like this:

Enter the wind speed on the Beaufort Scale: 7
Enter the number of boats racing (1-10): 7
Enter the code for boat 1: LASE
Enter the time for boat 1: 29:41
Enter the code for boat 2: SN
Enter the time for boat 2: 29:03
etc... etc...

Boat # Boat Type Raw Time Handicap Corrected Time
====== ========= ======== ======== ==============
1 LASE 29:41 88.2 33:39 (this should look nice but the forum post makes it ugly, oh well)
2 SN 29:03 89.0 32:38
etc... etc...

with the user input in red.

My code for this is as follows:

#include <stdio.h>
#include <string.h>

double handicap_calculate (int windspeed, char * type)

{
    double handicap;
    handicap = 2000000;
    
    if (!(strcmp("CL14", type))){
        switch (windspeed)
        {
            case 0:
            case 1:
                handicap = 103.3;
                break;
            case 2:            
            case 3:
                handicap = 101.7;
                break;
            case 4:
                handicap = 100.3;
                break;
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
                handicap = 94.4;
                break;
            default:
                handicap = 10000000;
        } 
    }

    if (!(strcmp("LASE", type))){
        switch (windspeed)
        {
            case 0:
            case 1:
                handicap = 93.8;
                break;
            case 2:
            case 3:
                handicap = 92.2;
                break;
            case 4:
                handicap = 91.0;
                break;
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
                handicap = 88.2;
                break;
            default:
                handicap = 10000000;
        } 
    }
        
    if (!(strcmp("SN", type))){
        switch (windspeed)
        {
            case 0:
            case 1:
                handicap = 94.9;
                break;
            case 2:
            case 3:
                handicap = 92.6;
                break;
            case 4:
                handicap = 91.4;
                break;
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
                handicap = 89.0;
                break;
            default:
                handicap = 10000000;
        } 
    }

    return handicap;
}

void printrow (int number, char * type, int raw_time, double handicap)
    
{
    int raw_minutes, raw_seconds, adj_minutes, adj_seconds, adj_time;

    raw_minutes = (raw_time / 60);
    raw_seconds = (raw_time % 60);

    adj_time = ((raw_time * 100) / handicap);

    adj_minutes = (adj_time / 60);
    adj_seconds = (adj_time % 60);

    printf("%6d  %9s  %5d:%02d  %8.1lf  %11d:%02d\n", number, type, raw_minutes, raw_seconds, handicap, adj_minutes, adj_seconds);

}

int main( )  

/* This function takes no arguments and returns a value of 0. */

{
    int x;

    int num_boats;
    int beaufort_scale;

    char boat_type[10][128] = {'\0'};
    int boat_time[10];
    double boat_handicap[10];

    char type_temp [128] = {'\0'};
    int minutes_temp,seconds_temp;

    beaufort_scale = 0;
    num_boats= 0;
    x = 0;

    printf("Enter the wind speed on the Beaufort Scale:  ");
    scanf("%d",&beaufort_scale);
    fflush(stdin);

    while ((beaufort_scale < 0) || (beaufort_scale > 12)){
        printf("\n\nError: Invalid Beaufort value!\n\n");
        printf("Enter the wind speed on the Beaufort Scale:  ");
        scanf("%d",&beaufort_scale);
        fflush(stdin);
    }

    if (beaufort_scale > 9) {
        printf("\nError:  Storm Warning!  Race Cancelled!\n");
        return 0;
    }


    printf("\nEnter the number of boats racing (1-10):  ");
    scanf("%d",&num_boats);

    fflush(stdin);

    while ((num_boats < 1) || (num_boats > 10)){
        printf("\n\nError: Invalid number of boats!\n\n");
        printf("Enter the number of boats:  ");
        scanf("%d",&num_boats);
        fflush(stdin);
    }

[I]     for (x = 0; x < num_boats; x++){
        printf("\nEnter the code for boat %d:  ", x + 1);
        gets(type_temp);
        fflush(stdin);

        while ((strcmp(type_temp,"LASE")) && (strcmp(type_temp,"CL14")) && (strcmp(type_temp,"SN"))){
            printf("\nError:  Invalid boat code!");
            printf("\nEnter the code for boat %d:  ", x + 1);
            gets(type_temp);
            fflush(stdin);
        }

        strcpy(boat_type[x], type_temp);

        printf("\nEnter the time for boat %d:  ", x + 1);
        scanf("%d:%d", &minutes_temp, &seconds_temp);
        fflush(stdin);
        
        while ((minutes_temp < 0) || (seconds_temp < 0) || (seconds_temp > 59)){
            printf("\n\nError: Invalid time!\n");
            printf("\nEnter the time for boat %d:  ", x + 1);
            scanf("%d:%d", &minutes_temp, &seconds_temp);
            fflush(stdin);
        }

        boat_time[x] = ((minutes_temp * 60) + seconds_temp);

    }[/I]    

    for (x = 0; x < num_boats; x++){
        boat_handicap[x] = handicap_calculate(beaufort_scale, boat_type[x]);
    }

    printf("\n\nBoat #  Boat Type  Raw Time  Handicap  Corrected Time\n");
    printf("======  =========  ========  ========  ==============\n");

    for (x = 0; x < num_boats; x++){
        printrow(x+1, boat_type[x], boat_time[x], boat_handicap[x]);
    }
    printf("\n");
     

    return 0;
}

The code I have is working, but I need to split off a module from something that's in my main function. Specifically, the part that reads the information for each boat (the blue code that's in italics). The only problem with that is that I need to pass arrays between main and the function.. both int[10] and char[10][128]. I then need to store these values in main().

How do I read in the boat information in a separate function while still storing the values for boat_time[x] and boat_type[x] in the arrays in main()?

Recommended Answers

All 13 Replies

This should serve as a good example. As the arrays are passed by reference, the modifications you make inside the function will be shown even after exiting it. So you can enter the values inside the function and use the contents inside main.

commented: useful resource +1

You should use pointers. Such as:

// ...
int array[][] = { {1, 12, 9}, {23, 12, 6} };
something(&array);

} // end of main

void something(int *something) {
// ...
}

That should answer your latest question, as this method is more flexible than the one WolfPack suggested.

Unfortunately you cannot return an array type in C, but you can pass back values through the parameters of your method to the array variable stored in main. To do so, pass the address of an array variable to the function and use a pointer in your function to that variable to edit the original variable in main. Sounds tricky. Example:

int a, b;
a = 2;
b = 3;
swap(&a, &b);

The call of swap passes the address of a and b into the method swap, and the values of a and b can be accessed by using a pointer inside that method. If you change the values those parameters hold in method swap, when the method returns, a and b will also be changed. So if you pass an array variable name (which in C holds its beginning address of that array) to a function, you should get the same effect of passing back through the parameter. Let me know if this is what you were looking for or not.

This should serve as a good example. As the arrays are passed by reference, the modifications you make inside the function will be shown even after exiting it. So you can enter the values inside the function and use the contents inside main.

Will that work for both single- and multi-dimensional arrays? I tried this method and I got a bunch of "incompatible input type for function" errors.

Unfortunately you cannot return an array type in C, but you can pass back values through the parameters of your method to the array variable stored in main. To do so, pass the address of an array variable to the function and use a pointer in your function to that variable to edit the original variable in main. Sounds tricky. Example:

int a, b;
a = 2;
b = 3;
swap(&a, &b);

The call of swap passes the address of a and b into the method swap, and the values of a and b can be accessed by using a pointer inside that method. If you change the values those parameters hold in method swap, when the method returns, a and b will also be changed. So if you pass an array variable name (which in C holds its beginning address of that array) to a function, you should get the same effect of passing back through the parameter. Let me know if this is what you were looking for or not.

You've probably answered my question, but in terms I can't come close to understanding. Is it possible for you to show me what my function is supposed to look like? I have tried simply passing the arrays (because they're passed by reference) and get incompatibility errors.

Will that work for both single- and multi-dimensional arrays? I tried this method and I got a bunch of "incompatible input type for function" errors.

Please note that 2-d arrays are pointers to pointers. Not sure if this is the problem, but it could be if your having problem with 2-d arrays.

Just go through the link I posted. It has examples for single and 2 dimensional array passing.

Probably dont even need &array to pass it to the function. The variable name itself should hold the beginning address. As a+i is the same as a.

You should use pointers. Such as:

// ...
int array[][] = { {1, 12, 9}, {23, 12, 6} };
something(&array);
 
} // end of main
 
void something(int *something) {
// ...
}

That should answer your latest question, as this method is more flexible than the one WolfPack suggested.

Your snippet won't compile.

int array[][] = { {1, 12, 9}, {23, 12, 6} }; // cant declare 2-D arrays like this.
void something(int *something) { // cant pass 2-D arrays like this.
// ...
}

Your snippet won't compile.

int array[][] = { {1, 12, 9}, {23, 12, 6} }; // cant declare 2-D arrays like this.
void something(int *something) { // cant pass 2-D arrays like this.
// ...
}

Sorry -- my bad! I just assumed that arrays could be passed with a memory address, and I forgot to specify the number of collumns in the second dimension in array .

WolfPack is right, just do what he suggested.

Is it just me, or have a lot of posts not been showing up lately?

soooo... to recap... a function that takes:

an integer x (passed by value, variable in main, not modified)
a char[10][128] array "boat_type" (passed by reference, variable in main, modified in function)
and an int[10] array "boat_time" (passed by reference, variable in main, also modified within)

should be declared like this?

void get_boat (int count, char[10][128] type, int time[10])
{
}

this is what I have and it's not accepting the values. incompatibility errors.

it should be like this.

void get_boat (int count, char type[10][128], int time[10])
{
}

thanks guys. I fixed my program.

The problem turned out to be in trying to use a for loop to execute a function several times to gather data for each boat, instead of having the loop to gather data for each boat inside the function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.