Code:
#include <stdio.h>
#include <stdlib.h>
/* Field Definitions */
typedef struct competitor{
char name[50];
int comp_number;
int river_fish;
int sea_fish;
int fly_fish;
} data ;
/*Method to print out structure, using pointer 'entry'*/
void print_entry(struct competitor * entry){
int lbs, st, oz, lbs1, st1, oz1, lbs2, st2, oz2; /*Temporary variables to used to convert total ounces bact to lb's, st's, oz's*/
st=(entry->river_fish/224);
lbs=((entry->river_fish%224)/16);
oz=((entry->river_fish%224)%16);
st1=(entry->sea_fish/224);
lbs1=((entry->sea_fish%224)/16);
oz1=((entry->sea_fish%224)%16);
st2=(entry->fly_fish/224);
lbs2=((entry->fly_fish%224)/16);
oz2=((entry->fly_fish%224)%16);
/* Print out formatted data from pointer 'entry' */
printf("%s \t\t %d \t\t %d %d %d \t\t %d %d %d\t\t %d %d %d\n",
entry->name, entry->comp_number, lbs,st,oz, lbs1, st1, oz1, lbs2, st2, oz2);
printf("\n");
}
main(){
int n, count, lbs, st, oz, x = 0; data *record;
printf("\n************************ Welcome To The Fishing Competition Program! *****************************\n\n");
printf("Please enter number of competitors:\n");
scanf("%d", &n);
/* Creates dynamic array */
record = (data *)malloc(n * sizeof(data));
for(count = 0; count <= (n-1); count++){
printf("\nPlease enter name (separated by underscores) of competitor # %d:\n", count+1);
scanf("%s", &record[count].name);
printf("\nPlease enter weight of Fish from the River Competition (Stones Pounds Ounces) for competitor # %d:\n", count+1);
scanf("%d %d %d", &lbs, &st, &oz); /* Add values to temporary variables */
record[count].river_fish = (((st*14)+lbs)*16)+oz; /* Formula to convert total weight in ounces and store in array */
printf("\nPlease enter weight of Fish from the Sea Competition (Stones Pounds Ounces) for competitor # %d:\n", count+1);
scanf("%d %d %d", &lbs, &st, &oz);
record[count].sea_fish = (((st*14)+lbs)*16)+oz;
printf("\nPlease enter weight of Fish from the Fly Competition (Stones Pounds Ounces) for competitor # %d:\n", count+1);
scanf("%d %d %d", &lbs, &st, &oz);
record[count].fly_fish=(((st*14)+lbs)*16)+oz;
/* Creates competitor number, initialises it as 1, then increments by 1 each time */
record[count].comp_number = count +1;
}
printf("\n\nNAME competitor number river fishing sea fishing fly fishing total weight\n");
printf("============================================= ===========================================\n");
for(count = 0; count <= (n-1); count++){
print_entry(&record[count]);
}
}
Many thanks x