| | |
Help: Passing arrays between functions
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 18
Reputation:
Solved Threads: 0
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:
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()?
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;
}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.
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.
バルサミコ酢やっぱいらへんで
You should use pointers. Such as:
That should answer your latest question, as this method is more flexible than the one WolfPack suggested.
c Syntax (Toggle Plain Text)
// ... 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.
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."
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:
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.
C Syntax (Toggle Plain Text)
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.
Last edited by TylerSBreton; Nov 2nd, 2006 at 12:01 am.
•
•
Join Date: Oct 2006
Posts: 18
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Oct 2006
Posts: 18
Reputation:
Solved Threads: 0
•
•
•
•
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:
C Syntax (Toggle Plain Text)
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.
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.
•
•
•
•
You should use pointers. Such as:
That should answer your latest question, as this method is more flexible than the one WolfPack suggested.c Syntax (Toggle Plain Text)
// ... int array[][] = { {1, 12, 9}, {23, 12, 6} }; something(&array); } // end of main void something(int *something) { // ... }
Your snippet won't compile.
C Syntax (Toggle Plain Text)
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. // ... }
Last edited by cscgal; Nov 2nd, 2006 at 2:53 pm. Reason: can't see post. :(
バルサミコ酢やっぱいらへんで
![]() |
Similar Threads
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Passing Arrays of Objects to Member Functions (C++)
- Passing arrays of objects to functions (C++)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: Base conversion
- Next Thread: Stack using a linear linked list of arrays
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open openwebfoundation owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






