Help: Passing arrays between functions

Thread Solved

Join Date: Oct 2006
Posts: 18
Reputation: tehloki is an unknown quantity at this point 
Solved Threads: 0
tehloki tehloki is offline Offline
Newbie Poster

Help: Passing arrays between functions

 
0
  #1
Nov 1st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Help: Passing arrays between functions

 
1
  #2
Nov 1st, 2006
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.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Help: Passing arrays between functions

 
0
  #3
Nov 1st, 2006
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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 89
Reputation: TylerSBreton is an unknown quantity at this point 
Solved Threads: 3
TylerSBreton's Avatar
TylerSBreton TylerSBreton is offline Offline
Junior Poster in Training

Re: Help: Passing arrays between functions

 
0
  #4
Nov 1st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 18
Reputation: tehloki is an unknown quantity at this point 
Solved Threads: 0
tehloki tehloki is offline Offline
Newbie Poster

Re: Help: Passing arrays between functions

 
0
  #5
Nov 1st, 2006
Originally Posted by WolfPack View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 18
Reputation: tehloki is an unknown quantity at this point 
Solved Threads: 0
tehloki tehloki is offline Offline
Newbie Poster

Re: Help: Passing arrays between functions

 
0
  #6
Nov 2nd, 2006
Originally Posted by TylerSBreton View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 89
Reputation: TylerSBreton is an unknown quantity at this point 
Solved Threads: 3
TylerSBreton's Avatar
TylerSBreton TylerSBreton is offline Offline
Junior Poster in Training

Re: Help: Passing arrays between functions

 
0
  #7
Nov 2nd, 2006
Originally Posted by tehloki View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Help: Passing arrays between functions

 
0
  #8
Nov 2nd, 2006
Just go through the link I posted. It has examples for single and 2 dimensional array passing.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 89
Reputation: TylerSBreton is an unknown quantity at this point 
Solved Threads: 3
TylerSBreton's Avatar
TylerSBreton TylerSBreton is offline Offline
Junior Poster in Training

Re: Help: Passing arrays between functions

 
0
  #9
Nov 2nd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Help: Passing arrays between functions

 
0
  #10
Nov 2nd, 2006
Originally Posted by joeprogrammer View Post
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. :(
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC