SDA Lab 6
SDA Lab 6
SDA Lab 6
Report
Laboratory Work No.6
Checked:
Burlacu Natalia, PhD, associate professor
Department of Software and Automation Engineering,
Facultatea FCIM, UTM
Chisinau – 2024
1
The purpose of this laboratory work is to enhance my C programming skills. I'll be
solving problems using procedural programming and implementing solutions using
two parameter passing methods: by value and by address/pointers. Additionally, I'll
develop custom functions for specific tasks and modify problem statements to
introduce additional challenges.
1. To check if all the elements of the vector are different two by two.
#include <stdio.h>
2. If the elements of the array correspond to the requirement formulated in the item
to perform the ascending sorting of the elements for the copy of the original vector,
applying the Bubble Sort method.
2
#include <stdio.h>
// Function to check if all elements of the array are different two by two
int areElementsUnique(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
return 0; // Not unique
}
}
}
return 1; // All unique
}
#include <stdio.h>
#include <stdbool.h>
void selectionSort(int *arr, int size) {
for (int i = 0; i < size - 1; i++) {
int min_index = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] > arr[min_index]) {
min_index = j;
}
}
// Swap arr[i] and arr[min_index]
int temp = arr[i];
arr[i] = arr[min_index];
3
arr[min_index] = temp;
}
}
bool checkDistinct(int *arr, int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (arr[i] == arr[j]) {
return false;
}
}
}
return true;
}
#include <stdio.h>
#include <stdbool.h>
4
// Swap *(arr + i) and *(arr + max_index)
int temp = *(arr + i);
*(arr + i) = *(arr + max_index);
*(arr + max_index) = temp;
}
}
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements for the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
if (checkDistinct(arr, size)) {
bubbleSort(arr, size);
printf("Array sorted in ascending order using Bubble Sort (elements are
distinct):\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
} else {
selectionSort(arr, size);
printf("Array sorted in descending order using Selection Sort (elements
are not distinct):\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
return 0;
}
5
Version 2.
Version A - with the use of the method of transmitting the parametric functions by
value;
#include <stdio.h>
#include <stdbool.h>
6
k++;
}
7
// Copy back the sorted array to the original array
for (int i = 0; i < n; i++) {
arr[i] = arr_copy[i];
}
}
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements for the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
8
insertionSortDesc(arr, size);
printf("Array sorted in descending order using Insertion Sort:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
#include <stdbool.h>
9
// Copy data to temporary arrays L[] and R[]
for (i = 0; i < n1; i++)
L[i] = *(arr + l + i);
for (j = 0; j < n2; j++)
R[j] = *(arr + m + 1 + j);
10
void insertionSortDesc(int *arr, int n) {
for (int i = 1; i < n; i++) {
int key = *(arr + i);
int j = i - 1;
while (j >= 0 && *(arr + j) < key) {
*(arr + j + 1) = *(arr + j);
j = j - 1;
}
*(arr + j + 1) = key;
}
}
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements for the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
11
insertionSortDesc(arr, size);
printf("Array sorted in descending order using Insertion Sort:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Conclusion: With this lab, I was able to gain a practical understanding of basic
principles while being fully immersed in the complex realm of sorting algorithms
and array manipulation. As I experimented with various tactics, I encountered both
hurdles and moments of frustration along the way. It was a challenging journey to
optimize the code effectively while ensuring its accuracy. I often found myself
tirelessly testing and retesting, facing situations where the code fell short of my
intentions. Yet, through the process of creating thorough test cases and diligently
debugging issues, I not only deepened my comprehension of sorting algorithms
and array manipulation but also developed a newfound appreciation for the
importance of user experience.
12
13
14
15
16
17
18
19