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;
|
||||
}
|
||||
70
c/isp-ws25/weeklies/week08/w08ex02.c
Executable file
70
c/isp-ws25/weeklies/week08/w08ex02.c
Executable file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
In this task, you should get familiar with
|
||||
malloc(), realloc() and free().
|
||||
|
||||
- user input: sequence of characters
|
||||
- store in array of characters
|
||||
- dynamically increase memory on heap for array
|
||||
- temporary pointer for return value of realloc()
|
||||
- add All characters to array until Q is entered
|
||||
- check if mem-alloc OK after each malloc() / realloc()
|
||||
- If mem-alloc NOK
|
||||
- print: "Memory allocation failed\n"
|
||||
- return 1
|
||||
- free allocated memory at the end of main()
|
||||
|
||||
Examples:
|
||||
|
||||
awe s o m e !Q
|
||||
Entered characters: a w e s o m e !
|
||||
|
||||
IS P ! :) Q
|
||||
Entered characters: I S P ! : )
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // TODO: Include the required header file
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int capacity = 2;
|
||||
int count = 0;
|
||||
char *characters = NULL;
|
||||
|
||||
characters = (char *)calloc(capacity, sizeof(char)); // TODO: Allocate memory (make use of the variable capacity)
|
||||
if (characters == NULL)
|
||||
{
|
||||
printf("Memory allocation failed\n");
|
||||
return 1;
|
||||
} // TODO: Check if memory could be allocated
|
||||
|
||||
char value = 0;
|
||||
while (scanf(" %c", &value) && value != 'Q')
|
||||
{
|
||||
if (count == capacity) // TODO: Check capacitiy and use realloc() to not run out of memory on the heap
|
||||
{
|
||||
capacity = capacity * count;
|
||||
char *temp_chars = (char*)realloc(characters, sizeof(char) * capacity);
|
||||
if (temp_chars == NULL)
|
||||
{
|
||||
printf("Memory allocation failed\n");
|
||||
return 1;
|
||||
}
|
||||
characters = temp_chars;
|
||||
} // TODO: Check if memory could be allocated
|
||||
characters[count] = value;
|
||||
count++;
|
||||
// TODO: Add the entered value to the characters array
|
||||
}
|
||||
|
||||
printf("Entered characters:");
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
printf(" %c", characters[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
free(characters); // TODO: Free the allocated memory
|
||||
|
||||
return 0;
|
||||
}
|
||||
79
c/isp-ws25/weeklies/week08/w08ex03.c
Executable file
79
c/isp-ws25/weeklies/week08/w08ex03.c
Executable file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
This program manages a list of product prices.
|
||||
The user can enter a product price and then decide,
|
||||
whether to continue or to to stop adding new product prices.
|
||||
After adding product prices, the average price is calculated and printed.
|
||||
|
||||
Unfortunately, there are some errors in the code.
|
||||
The compiler finds a few of them, others are logical
|
||||
errors and are not detected by the compiler.
|
||||
|
||||
This is an example:
|
||||
|
||||
Price for product 1: 6.50
|
||||
Add another price? (1 = yes, 0 = no): 1
|
||||
Price for product 2: 2.80
|
||||
Add another price? (1 = yes, 0 = no): 1
|
||||
Price for product 3: 9.90
|
||||
Add another price? (1 = yes, 0 = no): 1
|
||||
Price for product 4: 450
|
||||
Add another price? (1 = yes, 0 = no): 0
|
||||
Average price: 117.30
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void addPrice(float** prices, int* count, int* capacity)
|
||||
{
|
||||
if (*count >= *capacity)
|
||||
{
|
||||
*capacity *= 2;
|
||||
float *new_prices = realloc(*prices, *capacity * sizeof(float));
|
||||
if (new_prices == NULL)
|
||||
{
|
||||
printf("Memory allocation failed\n");
|
||||
return;
|
||||
}
|
||||
*prices = new_prices;
|
||||
}
|
||||
|
||||
printf("Price for product %d: ", *count + 1);
|
||||
scanf("%f", &(*prices)[*count]);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int capacity = 2;
|
||||
int count = 0;
|
||||
float* prices = (float*) calloc(capacity, sizeof(float));
|
||||
if (prices == NULL)
|
||||
{
|
||||
printf("Memory allocation failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int additional_price = 0;
|
||||
do
|
||||
{
|
||||
addPrice(&prices, &count, &capacity);
|
||||
count++;
|
||||
if (prices == NULL)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Add another price? (1 = yes, 0 = no): ");
|
||||
scanf("%d", &additional_price);
|
||||
} while (additional_price);
|
||||
|
||||
float total = 0;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
total += prices[i];
|
||||
}
|
||||
printf("Average price: %.2f\n", total / count);
|
||||
|
||||
free(prices);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user