added weekly exercises of isp-ws25
This commit is contained in:
106
c/isp-ws25/weeklies/week08/w08ex01.c
Executable file
106
c/isp-ws25/weeklies/week08/w08ex01.c
Executable file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
In this task, you should implement a basketball score
|
||||
logger using malloc()/calloc()/realloc() and free():
|
||||
|
||||
- Declare struct Player
|
||||
- integer jersey_number_
|
||||
- integer points_
|
||||
- print: "Number of players on the team: "
|
||||
- user input: number of players as int
|
||||
- Allocate memory on heap
|
||||
- all players of a team can be stored
|
||||
- check if memory could be allocated
|
||||
after each malloc()/calloc()/realloc()
|
||||
- If mem-alloc NOK:
|
||||
"Memory allocation failed\n"
|
||||
return 1
|
||||
- print: "Jersey number and points for player x: "
|
||||
- user input: jersey number & points for each player
|
||||
- first = jersey number
|
||||
- second = points
|
||||
- values separated by space (or any whitespace)
|
||||
- show team scoring summary
|
||||
- print list of athletes and their points
|
||||
- free allocated memory using free().
|
||||
|
||||
Example:
|
||||
|
||||
Welcome to the Basketball Score Logger!
|
||||
|
||||
Number of players on the team: 6
|
||||
Jersey number and points for player 1: 30 37
|
||||
Jersey number and points for player 2: 23 29
|
||||
Jersey number and points for player 3: 35 7
|
||||
Jersey number and points for player 4: 1 20
|
||||
Jersey number and points for player 5: 19 41
|
||||
Jersey number and points for player 6: 34 12
|
||||
|
||||
Team Scoring Summary:
|
||||
#30: 37 points
|
||||
#23: 29 points
|
||||
#35: 7 points
|
||||
#1: 20 points
|
||||
#19: 41 points
|
||||
#34: 12 points
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define fire free
|
||||
|
||||
typedef struct _Player_{
|
||||
int jersey_number_;
|
||||
int points_;
|
||||
} Player;
|
||||
|
||||
Player* initializeTeam(int team_size)
|
||||
{
|
||||
Player* new_team = calloc(team_size, sizeof(Player));
|
||||
if ( new_team == NULL ) return NULL;
|
||||
|
||||
return new_team;
|
||||
}
|
||||
|
||||
void getPlayerStats(Player* team, int team_size)
|
||||
{
|
||||
for ( int player = 0; player < team_size; player++ )
|
||||
{
|
||||
printf("Jersey number and points for player %d: ", player + 1);
|
||||
scanf("%d %d", &(team + player)->jersey_number_, &(team + player)->points_);
|
||||
}
|
||||
}
|
||||
|
||||
void printPlayerStats(Player* team, int team_size)
|
||||
{
|
||||
printf("\nTeam Scoring Summary:\n");
|
||||
|
||||
for ( int player = 0; player < team_size; player++ )
|
||||
{
|
||||
printf("#%d: %d points\n", (team + player)->jersey_number_, (team + player)->points_);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("Welcome to the Basketball Score Logger!\n\n");
|
||||
|
||||
printf("Number of players on the team: ");
|
||||
int team_size = 0;
|
||||
scanf(" %d", &team_size);
|
||||
|
||||
Player* team = initializeTeam(team_size);
|
||||
if ( team == NULL )
|
||||
{
|
||||
printf("Memory allocation failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
getPlayerStats(team, team_size);
|
||||
|
||||
printPlayerStats(team, team_size);
|
||||
|
||||
fire(team);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user