added weekly exercises of isp-ws25
This commit is contained in:
89
c/isp-ws25/weeklies/week07/w07ex01.c
Executable file
89
c/isp-ws25/weeklies/week07/w07ex01.c
Executable file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
In this task, you should get familiar with malloc() and free().
|
||||
In addition, you should implement the functions
|
||||
- fillArray(int size, int base_number)
|
||||
- printArray(int* numbers, int size).
|
||||
|
||||
main:
|
||||
- user can enter the size of the integer array
|
||||
- user can enter the base number that is used to fill the array
|
||||
- print "Memory allocation failed\n" if memory allocation in fillArray failed
|
||||
- immediately return exit code 1
|
||||
- free the allocated memory using free() at the end.
|
||||
|
||||
call createAndFillArray(int size, int base_number)
|
||||
- create array by allocating enough memory for it on the heap using malloc()
|
||||
- if memory allocation is not successful, return NULL
|
||||
- After allocating fill the array with integer values
|
||||
with multiples of base_number using pointer arithmetic
|
||||
- Return pointer to array at the end of the function
|
||||
|
||||
call printArray(int* numbers, int size)
|
||||
- print the array using pointer arithmetic
|
||||
|
||||
Examples:
|
||||
|
||||
Size: 5
|
||||
Fill numbers array with multiples of: 3
|
||||
3 6 9 12 15
|
||||
|
||||
Size: 4
|
||||
Fill numbers array with multiples of: 5
|
||||
5 10 15 20
|
||||
|
||||
Size: 12
|
||||
Fill numbers array with multiples of: 100
|
||||
100 200 300 400 500 600 700 800 900 1000 1100 1200
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // TODO: Include the required header file
|
||||
|
||||
int* createAndFillArray(int size, int base_number)
|
||||
{
|
||||
// TODO: Allocate memory
|
||||
int *numbers = (int*) malloc( size * sizeof(int) );
|
||||
// TODO: Check if memory could be allocated
|
||||
if ( numbers == NULL ) return NULL;
|
||||
|
||||
// TODO: Fill the array with multiples of base_number using pointer arithmetic and return it
|
||||
for ( int position = 0 ; position < size; position++ )
|
||||
{
|
||||
*(numbers + position) = base_number * ( position + 1 );
|
||||
}
|
||||
|
||||
return numbers;
|
||||
}
|
||||
|
||||
void printArray(int* numbers, int size)
|
||||
{
|
||||
for (int position = 0; position < size; position++)
|
||||
{
|
||||
printf("%d ", *(numbers + position));
|
||||
}
|
||||
// TODO: Print the array using pointer arithmetic
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int size = 0;
|
||||
printf("Size: ");
|
||||
scanf("%d", &size);
|
||||
|
||||
int base_number = 0;
|
||||
printf("Fill numbers array with multiples of: ");
|
||||
scanf("%d", &base_number);
|
||||
|
||||
int* numbers = createAndFillArray(size, base_number);
|
||||
if ( numbers == NULL )
|
||||
{
|
||||
printf("Memory allocation failed\n");
|
||||
return 1; // TODO: Return 1 if the memory allocation was not successful
|
||||
}
|
||||
|
||||
printArray(numbers, size);
|
||||
|
||||
free(numbers); // TODO: Free the allocated memory
|
||||
|
||||
return 0;
|
||||
}
|
||||
119
c/isp-ws25/weeklies/week07/w07ex02.c
Executable file
119
c/isp-ws25/weeklies/week07/w07ex02.c
Executable file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
In this task, you should get familiar with calloc() and free().
|
||||
The program tracks the attendance of students.
|
||||
The user can enter the number of students and then mark certain students as present.
|
||||
|
||||
Use calloc() to allocate memory on the heap for the required number of students.
|
||||
All students are marked as absent by default since calloc() initializes by default with 0.
|
||||
If the memory allocation is not successful, print Memory allocation failed\n and immediately return the exit code 1.
|
||||
|
||||
After allocating the memory, the user can enter student numbers (1-based, not 0-based) to mark them as present.
|
||||
When a student number is entered, the program should mark the corresponding student as present.
|
||||
Any student numbers that are not within the range of all student numbers
|
||||
(i.e., 1 to the entered number of students) should be ignored.
|
||||
If the user enters 0, the attendance check is done and the program prints the attendance record.
|
||||
For each student, print if the student is present or absent.
|
||||
|
||||
Don’t forget to free the allocated memory using free() at the end of the main() function.
|
||||
|
||||
Examples:
|
||||
|
||||
Number of students: 12
|
||||
Enter student numbers to mark as present (1 to 12, 0 to end):
|
||||
1
|
||||
8
|
||||
4
|
||||
5
|
||||
3
|
||||
0
|
||||
Attendance record:
|
||||
Student 1: Present
|
||||
Student 2: Absent
|
||||
Student 3: Present
|
||||
Student 4: Present
|
||||
Student 5: Present
|
||||
Student 6: Absent
|
||||
Student 7: Absent
|
||||
Student 8: Present
|
||||
Student 9: Absent
|
||||
Student 10: Absent
|
||||
Student 11: Absent
|
||||
Student 12: Absent
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // TODO: Include the required header file
|
||||
#include <stdbool.h>
|
||||
|
||||
#define ganzzahl int
|
||||
#define gleitkomma float
|
||||
#define leere void
|
||||
#define NICHTS ((void*)0)
|
||||
#define schreibf printf
|
||||
#define liesf scanf
|
||||
#define falls if
|
||||
#define fuer for
|
||||
#define solange while
|
||||
#define mach do
|
||||
#define mach_weiter continue
|
||||
#define gib_zurueck return
|
||||
#define haupt main
|
||||
#define befreie free
|
||||
#define unsigniert unsigned
|
||||
|
||||
|
||||
ganzzahl* erstelleFeld ( ganzzahl anzahl_an_schuelern )
|
||||
{
|
||||
ganzzahl *studenten = (ganzzahl*) calloc(anzahl_an_schuelern,sizeof(ganzzahl));
|
||||
falls(studenten == NICHTS) gib_zurueck NICHTS;
|
||||
gib_zurueck studenten;
|
||||
}
|
||||
|
||||
leere liesAnwesenheit ( ganzzahl* anwesenheit_der_studenten, ganzzahl anzahl_an_schuelern)
|
||||
{
|
||||
ganzzahl schueler = 0;
|
||||
schreibf("Enter student numbers to mark as present (1 to %d, 0 to end):\n", anzahl_an_schuelern); // TODO: Mark students as present
|
||||
ganzzahl benutzereingabe = 0;
|
||||
mach
|
||||
{
|
||||
liesf("%d", &benutzereingabe);
|
||||
schueler++;
|
||||
falls(benutzereingabe == 0) gib_zurueck;
|
||||
falls(benutzereingabe < 0) mach_weiter;
|
||||
falls(benutzereingabe > anzahl_an_schuelern - 1) mach_weiter;
|
||||
anwesenheit_der_studenten[benutzereingabe-1] = 1;
|
||||
}solange(benutzereingabe != 0 && schueler < anzahl_an_schuelern);
|
||||
}
|
||||
|
||||
leere schreibAnwesenheit ( ganzzahl* anwesenheit_der_studenten, ganzzahl anzahl_an_schuelern )
|
||||
{
|
||||
schreibf("Attendance record:\n");
|
||||
|
||||
fuer(ganzzahl schueler = 1; schueler <= anzahl_an_schuelern; schueler++)
|
||||
{
|
||||
schreibf("Student %d: %s\n", schueler, anwesenheit_der_studenten[schueler-1] == 0 ? "Absent" : "Present");
|
||||
}
|
||||
// TODO: Print the attendance record
|
||||
}
|
||||
|
||||
ganzzahl haupt(leere)
|
||||
{
|
||||
ganzzahl anzahl_an_schuelern = 0;
|
||||
|
||||
schreibf("Number of students: ");
|
||||
liesf("%d", &anzahl_an_schuelern);
|
||||
|
||||
ganzzahl* anwesenheit = erstelleFeld(anzahl_an_schuelern); // TODO: Allocate memory
|
||||
falls ( anwesenheit == NICHTS ) { // TODO: Check if memory could be allocated
|
||||
schreibf("Memory allocation failed\n");
|
||||
gib_zurueck 1;
|
||||
}
|
||||
|
||||
liesAnwesenheit(anwesenheit, anzahl_an_schuelern);
|
||||
|
||||
schreibAnwesenheit(anwesenheit, anzahl_an_schuelern);
|
||||
|
||||
befreie(anwesenheit); // TODO: Free the allocated memory
|
||||
|
||||
gib_zurueck 0;
|
||||
}
|
||||
Reference in New Issue
Block a user