This is the assignment:
Define a structure called BasballPlayer with the following members:
Team
Number (number worn on the uniform)
First name
Last name
Position
Batting Average (double)
Write a program that defines an array of pointers to objects of type BaseballPlayer. Enter all the above information from the keyboard for each player. If the player’s Batting Average is at least .275, call a function, MakePlayer(). This function must dynamically allocate memory for an object of type BaseballPlayer, store the values in the object, and return a pointer to the object. Then have the next available pointer in the array point to that object. If the player’s average is lower than .275 discard and input another player. Input “end” for Team as a sentinel value (Hint: strcmp()).
And my code so far( i know there are some useless parts, they will be used later):
#include <stdio.h>
typedef struct BaseballPlayer {
int Number;
char FirstName[25];
char LastName[25];
char Team[25];
double BattingAverage;
};
/* Function Prototypes */
void Sort( struct BaseballPlayer list[], int size );
void Print( struct BaseballPlayer list[], int size );
double Average( struct BaseballPlayer list[], int size );
int main()
{
/* declare and initialize variables */
int size = 10, i = 0;
double average = 0.0;
struct BaseballPlayer list[10];
printf("Enter a Number: ");
scanf("%d", &list[i].Number);
while (i < 10 && list[i].Number != -1)
{
printf("Enter a First Name: ");
scanf("%s", &list[i].FirstName);
printf("Enter a Last Name: ");
scanf("%s", &list[i].LastName);
printf("Enter a Team: ");
scanf("%s", &list[i].Team);
printf("Enter a Batting Average: ");
scanf("%lf", &list[i].BattingAverage);
printf("-------------------------------\n");
i++;
printf("Enter a Number: ");
scanf("%d", &list[i].Number);
}
Sort( list, i );
printf("-------------------------------\n\n");
Print( list, i );
printf("-------------------------------\n\n");
average = Average( list, i );
printf("The average Batting Average is %5.3f\n", average);
printf("-------------------------------\n\n");
return;
}
void Sort( struct BaseballPlayer list[], int size )
{
int min = 0, i = 0, j = 0;
struct BaseballPlayer temp;
for( i = 0; i < size; i++ )
{
min = i;
for( j = i + 1; j < size; j++ )
{
if( strcmp(list[j].LastName, list[min].LastName) < 0 )
{
min = j;
}
}
if( min != i )
{
temp = list[min];
list[min] = list[i];
list[i] = temp;
}
}
return;
}
void Print( struct BaseballPlayer list[], int size )
{
int i = 0;
for( i = 0; i < size; i++ )
{
printf("%5d %10s %10s %15s %5.3f \n", list[i].Number, list[i].FirstName, list[i].LastName, list[i].Team, list[i].BattingAverage);
}
return;
}
double Average( struct BaseballPlayer list[], int size )
{
int i = 0;
double sum = 0.0;
for( i = 0; i < size; i++ )
{
sum += list[i].BattingAverage;
}
return sum/i;
}

New Topic/Question
Reply




MultiQuote




|