added weekly exercises of isp-ws25
This commit is contained in:
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