Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So, I need to create interactive program in \'C\' that will input each player\'

ID: 3647027 • Letter: S

Question

So, I need to create interactive program in 'C' that will input each player' s name and their last three at bats and display a listing showing each player's last 3 games plus that average. Finally, display the cumulative average for all players.

Create a structure player_t to store the players name and their last three at bats. There will be no more than 10 at bats by a player in a single game. The user should be prompted and enter first the name of the player, three numbers (ex. 3 2 4) and then it should loop until ^Z is entered like the the Output Example. I'm only worried about the variables such as name, atbats, and avg.

Output Example
-------------------------------------------------------------
Enter Name <Ctrl-Z to stop>: Juan Lopez
Enter the number of at bats for the last three games: 3 2 4

Enter Name <Ctrl-Z to stop>: Manny Smith
Enter the number of at bats for the last three games: 6 2 4

Enter Name <Ctrl-Z to stop>: ^Z


Name Games 1 Games 2 Game 3 Average
Juan Lopez 3 2 4 3.0

Manny Smith 6 2 4 4.0


The average number of at bats for the last three game is : 3.5
Press any key to continue...

---------------------------------------------------------------------

This is what I have so far. I know that its not correct and some code is out of place but I really need help.
---------------------------------------------------------------------

#include<stdio.h>

typedef struct{
char fName[60];
char lName[60]; /* name is 60 characters long */
double atBats [3]; /* 3 atbats */
} player_t;


int main()
{
player_t players[50]; /* up to 50 players */

int i;
int sum;
float avg;
....

while(gets(players[count].name) // loop
{
...
printf(" Enter name <Ctrl-Z to stop>: ");
scanf("%s",&players[count].fName[i],&players[count].lName[i] );
for (i = 0; i < 3; i++)
....
printf("Enter the number of atBats in the last three games: ");
scanf("%d", &players[count].atBats[i]); // Should allow the user to input the values for three values (ex. 3 4 5)
...
printf(" Name Game 1 Game 2 Game 3 Average "); // Should be output in this formatt
...
system("pause");
return 0;
}

Explanation / Answer

I've edited your program to a larger extent,please go through it carefully although I've used comments for your better understanding,this code doesn't have any errors and works perfect.

#include<stdio.h>

struct players {
char fName[60];
char lName[60]; /* name is 60 characters long */
double atBats [3]; /* 3 atbats */
float avg;
} ;
int main()
{
/* up to 50 players */
struct players player_t[50]; //structure variable array declaration
int i,count=0;//count variable must be intitialized before we use elsewhere in our code
float avg,sum=0,sum_v=0;//to obtain accurate results,use float datatype for the sum calculation

while(1) /* loop repeats until ctrl-Z is pressed,hence no condition is required in the while loop*/
{

printf(" Enter name <Ctrl-Z to stop>: ");
scanf("%s",&player_t[count].fName[i],&player_t[count].lName[i] );
for (i = 0; i < 3; i++)
{
printf("Enter the number of atBats in the last three games: ");
scanf("%d", &player_t[count].atBats[i]); /* Should allow the user to input the values for three values (ex. 3 4 5) */
sum=sum+player_t[count].atBats[i]; //summing up the atBat values
}
player_t[count].avg=sum/3; //average of the values entered
printf(" Name Game 1 Game 2 Game 3 Average "); // Should be output in this formatt
printf("%s %f %f %f %f ",player_t[count].fName,player_t[count].atBats[0],player_t[count].atBats[1],player_t[count].atBats[2],player_t[count].avg);

sum_v=sum_v+(player_t[count].avg);
count++;
}
avg=sum_v/count;
printf("The average number of at bats for the last three game is %f",avg);


printf("Press any key to continue..");

system("pause");

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote