944,038 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 6054
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 1st, 2006
0

Help: Passing arrays between functions

Expand Post »
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);
    }

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

    }    

    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()?
Last edited by tehloki; Nov 1st, 2006 at 11:53 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tehloki is offline Offline
18 posts
since Oct 2006
Nov 1st, 2006
1

Re: Help: Passing arrays between functions

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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Nov 1st, 2006
0

Re: Help: Passing arrays between functions

You should use pointers. Such as:
  1. // ...
  2. int array[][] = { {1, 12, 9}, {23, 12, 6} };
  3. something(&array);
  4.  
  5. } // end of main
  6.  
  7. void something(int *something) {
  8. // ...
  9. }

That should answer your latest question, as this method is more flexible than the one WolfPack suggested.
Last edited by John A; Nov 2nd, 2006 at 12:05 am.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Nov 1st, 2006
0

Re: Help: Passing arrays between functions

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:

  1. int a, b;
  2. a = 2;
  3. b = 3;
  4. 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.
Last edited by TylerSBreton; Nov 2nd, 2006 at 12:01 am.
Reputation Points: 25
Solved Threads: 3
Junior Poster in Training
TylerSBreton is offline Offline
89 posts
since Oct 2006
Nov 1st, 2006
0

Re: Help: Passing arrays between functions

Click to Expand / Collapse  Quote originally posted by WolfPack ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tehloki is offline Offline
18 posts
since Oct 2006
Nov 2nd, 2006
0

Re: Help: Passing arrays between functions

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:

  1. int a, b;
  2. a = 2;
  3. b = 3;
  4. 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tehloki is offline Offline
18 posts
since Oct 2006
Nov 2nd, 2006
0

Re: Help: Passing arrays between functions

Click to Expand / Collapse  Quote originally posted by tehloki ...
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.
Reputation Points: 25
Solved Threads: 3
Junior Poster in Training
TylerSBreton is offline Offline
89 posts
since Oct 2006
Nov 2nd, 2006
0

Re: Help: Passing arrays between functions

Just go through the link I posted. It has examples for single and 2 dimensional array passing.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Nov 2nd, 2006
0

Re: Help: Passing arrays between functions

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[i].
Last edited by TylerSBreton; Nov 2nd, 2006 at 12:29 am.
Reputation Points: 25
Solved Threads: 3
Junior Poster in Training
TylerSBreton is offline Offline
89 posts
since Oct 2006
Nov 2nd, 2006
0

Re: Help: Passing arrays between functions

You should use pointers. Such as:
  1. // ...
  2. int array[][] = { {1, 12, 9}, {23, 12, 6} };
  3. something(&array);
  4.  
  5. } // end of main
  6.  
  7. void something(int *something) {
  8. // ...
  9. }
That should answer your latest question, as this method is more flexible than the one WolfPack suggested.

Your snippet won't compile.
  1.  
  2. int array[][] = { {1, 12, 9}, {23, 12, 6} }; // cant declare 2-D arrays like this.
  3. void something(int *something) { // cant pass 2-D arrays like this.
  4. // ...
  5. }
Last edited by cscgal; Nov 2nd, 2006 at 2:53 pm. Reason: can't see post. :(
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Base conversion
Next Thread in C Forum Timeline: Stack using a linear linked list of arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC