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()?