MINISTRY OF EDUCATION, CULTURE AND RESEARCH
OF THE REPUBLIC OF MOLDOVA
Technical University of Moldova
Faculty of Computers, Informatics and Microelectronics
Department of Software and Automation Engineering
Rusnac Nichita student
Group: FAF-242
Report
Laboratory Work No.4
of the "Data Structures and Algorithms" course
Checked:
Burlacu Natalia, PhD, associate professor
Department of Software and Automatic Engineering,
FCIM Faculty, TUM
Chisinau – 2025
The Purpose:
Problem Condition:
Figure 1.1 - Problem Condition
1. The program code, including relevant comments within it.
Cod:
------------------------------The header file student.h----------------------------
#ifndef STUDENT_H
#define STUDENT_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Standard version using STRUCT
typedef struct {
char name[50];
char surname[50];
struct {
int day;
int month;
int year;
} date_of_birth;
char gender;
char address[100];
char specialty[10];
int group_no;
struct {
float pc_programming;
float philosophy;
float english;
} grades;
} StudentStd;
// Optimized version using UNION, typedef, and Bit Fields
typedef union {
struct {
int day : 5;//1..32
int month : 4;//1.16
int year : 12;//1..4096
} birth_date;
int date_as_int;
} DateOpt;
typedef struct {
char name[50];
char surname[50];
DateOpt date_of_birth;
char gender;
char address[100];
char specialty[10];
int group_no;
struct {
float pc_programming;
float philosophy;
float english;
} grades;
} StudentOpt;
// Function declarations for standard version
void inputStudentStd(StudentStd *s);
void displayStudentStd(StudentStd s);
void quickSortStd(StudentStd arr[], int low, int high, int field, int
ascending);
void combSortStd(StudentStd arr[], int n, int field, int ascending);
void modifyStudentStd(StudentStd *s);
// Function declarations for optimized version
void inputStudentOpt(StudentOpt *s);
void displayStudentOpt(StudentOpt s);
void quickSortOpt(StudentOpt arr[], int low, int high, int field, int
ascending);
void combSortOpt(StudentOpt arr[], int n, int field, int ascending);
void modifyStudentOpt(StudentOpt *s);
#endif // STUDENT_H
-----------------------------------The main function--------------------------------
#include "student.h"
// Function definitions for standard version
// Input student details
void inputStudentStd(StudentStd *s) {
printf("Enter name: ");
scanf("%s", s->name);
printf("Enter surname: ");
scanf("%s", s->surname);
printf("Enter date of birth (day month year): ");
scanf("%d %d %d", &s->date_of_birth.day, &s->date_of_birth.month, &s-
>date_of_birth.year);
printf("Enter gender (m/f): ");
scanf(" %c", &s->gender);
printf("Enter address: ");
scanf(" %[^\n]", s->address);
printf("Enter specialty: ");
scanf("%s", s->specialty);
printf("Enter group number: ");
scanf("%d", &s->group_no);
printf("Enter PC Programming grade: ");
scanf("%f", &s->grades.pc_programming);
printf("Enter Philosophy grade: ");
scanf("%f", &s->grades.philosophy);
printf("Enter English grade: ");
scanf("%f", &s->grades.english);
}
// Display student details
void displayStudentStd(StudentStd s) {
printf("Name: %s %s\n", s.name, s.surname);
printf("Date of Birth: %02d/%02d/%04d\n", s.date_of_birth.day,
s.date_of_birth.month, s.date_of_birth.year);
printf("Gender: %c\n", s.gender);
printf("Address: %s\n", s.address);
printf("Specialty: %s\n", s.specialty);
printf("Group No: %d\n", s.group_no);
printf("PC Programming Grade: %.2f\n", s.grades.pc_programming);
printf("Philosophy Grade: %.2f\n", s.grades.philosophy);
printf("English Grade: %.2f\n", s.grades.english);
}
// Swap two student records
void swapStd(StudentStd *a, StudentStd *b) {
StudentStd temp = *a;
*a = *b;
*b = temp;
}
// Compare students based on a specific field
int compareStudentsStd(const StudentStd *a, const StudentStd *b, int field, int
ascending) {
int result = 0;
switch (field) {
case 1: result = strcmp(a->name, b->name); break; // Compare names
case 2: result = strcmp(a->surname, b->surname); break; // Compare
surnames
case 3: // Compare dates of birth
if (a->date_of_birth.year != b->date_of_birth.year)
result = a->date_of_birth.year - b->date_of_birth.year;
else if (a->date_of_birth.month != b->date_of_birth.month)
result = a->date_of_birth.month - b->date_of_birth.month;
else
result = a->date_of_birth.day - b->date_of_birth.day;
break;
case 4: result = a->gender - b->gender; break; // Compare genders
case 5: result = strcmp(a->address, b->address); break; // Compare
addresses
case 6: result = strcmp(a->specialty, b->specialty); break; // Compare
specialties
case 7: result = a->group_no - b->group_no; break; // Compare group
numbers
case 8: result = (a->grades.pc_programming > b->grades.pc_programming) ?
1 : -1; break; // Compare PC grades
case 9: result = (a->grades.philosophy > b->grades.philosophy) ? 1 : -1;
break; // Compare Philosophy grades
case 10: result = (a->grades.english > b->grades.english) ? 1 : -1;
break; // Compare English grades
default: result = 0; // Invalid field
}
return ascending ? result : -result; // Adjust for ascending/descending
order
}
// QuickSort implementation for sorting students
void quickSortStd(StudentStd arr[], int low, int high, int field, int ascending)
{
if (low < high) {
StudentStd pivot = arr[high]; // Choose pivot
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (compareStudentsStd(&arr[j], &pivot, field, ascending) < 0) {
i++;
swapStd(&arr[i], &arr[j]); // Swap elements
}
}
swapStd(&arr[i + 1], &arr[high]); // Place pivot in correct position
int pi = i + 1;
quickSortStd(arr, low, pi - 1, field, ascending); // Sort left subarray
quickSortStd(arr, pi + 1, high, field, ascending); // Sort right
subarray
}
}
// CombSort implementation for sorting students
void combSortStd(StudentStd arr[], int n, int field, int ascending) {
int gap = n;
int swapped = 1;
while (gap != 1 || swapped == 1) {
gap = (gap * 10) / 13; // Reduce gap
if (gap < 1)
gap = 1;
swapped = 0;
for (int i = 0; i < n - gap; i++) {
if (compareStudentsStd(&arr[i], &arr[i + gap], field, ascending) >
0) {
swapStd(&arr[i], &arr[i + gap]); // Swap elements
swapped = 1;
}
}
}
}
// Modify student data based on user input
void modifyStudentStd(StudentStd *s) {
int choice;
printf("\nChoose field to modify:\n1. Name\n2. Surname\n3. Date of Birth\n4.
Gender\n5. Address\n6. Specialty\n7. Group No\n8. PC Programming Grade\n9.
Philosophy Grade\n10. English Grade\n");
scanf("%d", &choice);
switch (choice) {
case 1: printf("Enter new name: "); scanf("%s", s->name); break;
case 2: printf("Enter new surname: "); scanf("%s", s->surname); break;
case 3: printf("Enter new date of birth (day month year): "); scanf("%d
%d %d", &s->date_of_birth.day, &s->date_of_birth.month, &s->date_of_birth.year);
break;
case 4: printf("Enter new gender (m/f): "); scanf(" %c", &s->gender);
break;
case 5: printf("Enter new address: "); scanf(" %[^\n]", s->address);
break;
case 6: printf("Enter new specialty: "); scanf("%s", s->specialty);
break;
case 7: printf("Enter new group number: "); scanf("%d", &s->group_no);
break;
case 8: printf("Enter new PC Programming grade: "); scanf("%f", &s-
>grades.pc_programming); break;
case 9: printf("Enter new Philosophy grade: "); scanf("%f", &s-
>grades.philosophy); break;
case 10: printf("Enter new English grade: "); scanf("%f", &s-
>grades.english); break;
default: printf("Invalid choice.\n"); break;
}
printf("Student data updated.\n");
}
// Function definitions for optimized version
// Input student details (optimized version)
void inputStudentOpt(StudentOpt *s) {
printf("Enter name: ");
scanf("%s", s->name);
printf("Enter surname: ");
scanf("%s", s->surname);
printf("Enter date of birth (day month year): ");
int day, month, year;
scanf("%d %d %d", &day, &month, &year);
s->date_of_birth.birth_date.day = day;
s->date_of_birth.birth_date.month = month;
s->date_of_birth.birth_date.year = year;
printf("Enter gender (m/f): ");
scanf(" %c", &s->gender);
printf("Enter address: ");
scanf(" %[^\n]", s->address);
printf("Enter specialty: ");
scanf("%s", s->specialty);
printf("Enter group number: ");
scanf("%d", &s->group_no);
printf("Enter PC Programming grade: ");
scanf("%f", &s->grades.pc_programming);
printf("Enter Philosophy grade: ");
scanf("%f", &s->grades.philosophy);
printf("Enter English grade: ");
scanf("%f", &s->grades.english);
}
// Display student details (optimized version)
void displayStudentOpt(StudentOpt s) {
printf("Name: %s %s\n", s.name, s.surname);
printf("Date of Birth: %02d/%02d/%04d\n", s.date_of_birth.birth_date.day,
s.date_of_birth.birth_date.month, s.date_of_birth.birth_date.year);
printf("Gender: %c\n", s.gender);
printf("Address: %s\n", s.address);
printf("Specialty: %s\n", s.specialty);
printf("Group No: %d\n", s.group_no);
printf("PC Programming Grade: %.2f\n", s.grades.pc_programming);
printf("Philosophy Grade: %.2f\n", s.grades.philosophy);
printf("English Grade: %.2f\n", s.grades.english);
}
// Swap two student records (optimized version)
void swapOpt(StudentOpt *a, StudentOpt *b) {
StudentOpt temp = *a;
*a = *b;
*b = temp;
}
// Compare students based on a specific field (optimized version)
int compareStudentsOpt(const StudentOpt *a, const StudentOpt *b, int field, int
ascending) {
int result = 0;
switch (field) {
case 1: result = strcmp(a->name, b->name); break;
case 2: result = strcmp(a->surname, b->surname); break;
case 3:
if (a->date_of_birth.birth_date.year != b-
>date_of_birth.birth_date.year)
result = a->date_of_birth.birth_date.year - b-
>date_of_birth.birth_date.year;
else if (a->date_of_birth.birth_date.month != b-
>date_of_birth.birth_date.month)
result = a->date_of_birth.birth_date.month - b-
>date_of_birth.birth_date.month;
else
result = a->date_of_birth.birth_date.day - b-
>date_of_birth.birth_date.day;
break;
case 4: result = a->gender - b->gender; break;
case 5: result = strcmp(a->address, b->address); break;
case 6: result = strcmp(a->specialty, b->specialty); break;
case 7: result = a->group_no - b->group_no; break;
case 8: result = (a->grades.pc_programming > b->grades.pc_programming) ?
1 : -1; break;
case 9: result = (a->grades.philosophy > b->grades.philosophy) ? 1 : -1;
break;
case 10: result = (a->grades.english > b->grades.english) ? 1 : -1;
break;
default: result = 0;
}
return ascending ? result : -result;
}
// QuickSort implementation for sorting students (optimized version)
void quickSortOpt(StudentOpt arr[], int low, int high, int field, int ascending)
{
if (low < high) {
StudentOpt pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (compareStudentsOpt(&arr[j], &pivot, field, ascending) < 0) {
i++;
swapOpt(&arr[i], &arr[j]);
}
}
swapOpt(&arr[i + 1], &arr[high]);
int pi = i + 1;
quickSortOpt(arr, low, pi - 1, field, ascending);
quickSortOpt(arr, pi + 1, high, field, ascending);
}
}
// CombSort implementation for sorting students (optimized version)
void combSortOpt(StudentOpt arr[], int n, int field, int ascending) {
int gap = n;
int swapped = 1;
while (gap != 1 || swapped == 1) {
gap = (gap * 10) / 13;
if (gap < 1)
gap = 1;
swapped = 0;
for (int i = 0; i < n - gap; i++) {
if (compareStudentsOpt(&arr[i], &arr[i + gap], field, ascending) >
0) {
swapOpt(&arr[i], &arr[i + gap]);
swapped = 1;
}
}
}
}
// Modify student data (optimized version)
void modifyStudentOpt(StudentOpt *s) {
int choice;
printf("\nChoose field to modify:\n1. Name\n2. Surname\n3. Date of Birth\n4.
Gender\n5. Address\n6. Specialty\n7. Group No\n8. PC Programming Grade\n9.
Philosophy Grade\n10. English Grade\n");
scanf("%d", &choice);
switch (choice) {
case 1: printf("Enter new name: "); scanf("%s", s->name); break;
case 2: printf("Enter new surname: "); scanf("%s", s->surname); break;
case 3: printf("Enter new date of birth (day month year): "); int day,
month, year; scanf("%d %d %d", &day, &month, &year); s-
>date_of_birth.birth_date.day = day; s->date_of_birth.birth_date.month = month;
s->date_of_birth.birth_date.year = year; break;
case 4: printf("Enter new gender (m/f): "); scanf(" %c", &s->gender);
break;
case 5: printf("Enter new address: "); scanf(" %[^\n]", s->address);
break;
case 6: printf("Enter new specialty: "); scanf("%s", s->specialty);
break;
case 7: printf("Enter new group number: "); scanf("%d", &s->group_no);
break;
case 8: printf("Enter new PC Programming grade: "); scanf("%f", &s-
>grades.pc_programming); break;
case 9: printf("Enter new Philosophy grade: "); scanf("%f", &s-
>grades.philosophy); break;
case 10: printf("Enter new English grade: "); scanf("%f", &s-
>grades.english); break;
default: printf("Invalid choice.\n"); break;
}
printf("Student data updated.\n");
}
// Main function
int main() {
int versionChoice;
printf("Choose version:\n1. Standard version (STRUCT)\n2. Optimized version
(UNION, typedef, Bit Fields)\n");
scanf("%d", &versionChoice);
int n;
printf("Enter number of students: ");
scanf("%d", &n);
if (versionChoice == 1) {
StudentStd *students = (StudentStd *)malloc(n * sizeof(StudentStd));
for (int i = 0; i < n; i++) {
printf("\nEnter details for student %d:\n", i + 1);
inputStudentStd(&students[i]);
}
int choice;
do {
printf("\nChoose an operation:\n1. Display student details\n2. Sort
students\n3. Modify student data\n4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nStudent details:\n");
for (int i = 0; i < n; i++) {
displayStudentStd(students[i]);
}
break;
case 2: {
int sortChoice, field, ascending;
printf("\nChoose sorting method:\n1. Quick Sort (Ascending)\
n2. Comb Sort (Descending)\n");
scanf("%d", &sortChoice);
printf("\nChoose field to sort by:\n1. Name\n2. Surname\n3.
Date of Birth\n4. Gender\n5. Address\n6. Specialty\n7. Group No\n8. PC
Programming Grade\n9. Philosophy Grade\n10. English Grade\n");
scanf("%d", &field);
if (sortChoice == 1) {
ascending = 1;
quickSortStd(students, 0, n - 1, field, ascending);
} else {
ascending = 0;
combSortStd(students, n, field, ascending);
}
printf("\nSorted student details:\n");
for (int i = 0; i < n; i++) {
displayStudentStd(students[i]);
}
break;
}
case 3: {
char name[50], surname[50];
int birth_year;
printf("\nEnter name, surname, and birth year of student to
modify: ");
scanf("%s %s %d", name, surname, &birth_year);
for (int i = 0; i < n; i++) {
if (strcmp(students[i].name, name) == 0 &&
strcmp(students[i].surname, surname) == 0 && students[i].date_of_birth.year ==
birth_year) {
modifyStudentStd(&students[i]);
displayStudentStd(students[i]);
break;
}
}
break;
}
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice != 4);
free(students);
} else if (versionChoice == 2) {
StudentOpt *students = (StudentOpt *)malloc(n * sizeof(StudentOpt));
for (int i = 0; i < n; i++) {
printf("\nEnter details for student %d:\n", i + 1);
inputStudentOpt(&students[i]);
}
int choice;
do {
printf("\nChoose an operation:\n1. Display student details\n2. Sort
students\n3. Modify student data\n4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nStudent details:\n");
for (int i = 0; i < n; i++) {
displayStudentOpt(students[i]);
}
break;
case 2: {
int sortChoice, field, ascending;
printf("\nChoose sorting method:\n1. Quick Sort (Ascending)\
n2. Comb Sort (Descending)\n");
scanf("%d", &sortChoice);
printf("\nChoose field to sort by:\n1. Name\n2. Surname\n3.
Date of Birth\n4. Gender\n5. Address\n6. Specialty\n7. Group No\n8. PC
Programming Grade\n9. Philosophy Grade\n10. English Grade\n");
scanf("%d", &field);
if (sortChoice == 1) {
ascending = 1;
quickSortOpt(students, 0, n - 1, field, ascending);
} else {
ascending = 0;
combSortOpt(students, n, field, ascending);
}
printf("\nSorted student details:\n");
for (int i = 0; i < n; i++) {
displayStudentOpt(students[i]);
}
break;
}
case 3: {
char name[50], surname[50];
int birth_year;
printf("\nEnter name, surname, and birth year of student to
modify: ");
scanf("%s %s %d", name, surname, &birth_year);
for (int i = 0; i < n; i++) {
if (strcmp(students[i].name, name) == 0 &&
strcmp(students[i].surname, surname) == 0 &&
students[i].date_of_birth.birth_date.year == birth_year) {
modifyStudentOpt(&students[i]);
displayStudentOpt(students[i]);
break;
}
}
break;
}
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice != 4);
free(students);
} else {
printf("Invalid version choice.\n");
}
return 0;
}
------------------------------------------Block Diagrams-----------------------------------------
Fig 2.1 inputStudent
Fig 2.2 compareStudents
Fig 2.3 quickSort
Fig 2.6 modifyStudent
Fig 2.7 main
Output:
Figure 3.1 - The
output window
for
displaying students
Figure 3.2 - The
output window
for sorted students
Figure 3.3 - The output window for the modified student
Conclusion:
In this laboratory I started finally to understand the usage of the Structs, Unions
and Bit Fields operators in working in big complex projects with a lot of information
that should be handled as fast as possible. Moreover, I tried new tools in making
block diagrams that helped me do make them a bit more visually appealing without
losing important info. Overall it was a great laboratory that helped me to work with
big programs which will help me in the following Individual Work project that we
already started.