FAF233 Lab1 PostoroncaDumitru

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

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

Postoronca Dumitru

Report
Laboratory Work No1

of the "Data Structures and Algorithms" course

Checked:
Burlacu Natalia, PhD, associate professor
Department of Software and Automation Engineering,
Faculty CIM, UTM

Chisinau – 2023

1
Task:

1. Solve the following problems in C, writing your own functions according to the given
statements. Write the solution of the problem by procedural approach in two versions:

A. with the use of the method of transmitting the parametric functions by


value;
B. with the use of the method of passing parameters of functions by
address/pointers (the formal parameter will be a pointer to the value of the
corresponding object).
C. To draw the block diagram corresponding to the solved problem.

2. Modify the content of your problems emerging from the possibilities that are
missing, but which can be brought as added value in the condition of the existing
problem. Formulate and present in writing the modified condition; to solve in C your
problem in the modified version, using functions developed by you

3. Executable code, with relevant comments and flow charts


Version 1 with parameters passed as values(inverse Bubble and Selection sort)
#include<stdio.h>
#include<malloc.h>
#include<string.h>
//function to swap 2 values by their addresses
void swap(int *x, int *y){
int temp=*x;
*x=*y;
*y=temp;
}
//functions to set and reset displaying color in output
2
void green(){
printf("\033[0;32m");
}
void red(){
printf("\033[0;31m");
}
void reset(){
printf(" \033[0;37m");
}

//function to print the sorted array and used algorithm


void print_result(char *sort_type, int* arr, int n){
int size = strlen(sort_type);

green();
for (int i = 0; i < size; i++)//prints used algorithm
printf("%c", sort_type[i]);
reset();
for (int i = 0; i < n; i++)//prints sorted array
printf("%i ", arr[i]);

//function to get the array


int array_input(int** arr){
int size = 0; //current size of the array
int capacity = 0; //capacity of the array (allocated memory)
int input;

printf("Enter integers (enter a letter to finish):\n");


while (1) {
int result = scanf("%d", &input);
if (result == 0) break;//check if input is not an integer

//check if the array needs to be resized


if (size == capacity) {
capacity = (capacity == 0) ? 1 : capacity + 10;//add capacity

int *temp = (int*)realloc(*arr, capacity * sizeof(int));//memory


realoc

if (temp == NULL) {
printf("Memory reallocation failed.\n");
free(*arr);
return 1;
}
*arr = temp;
}

(*arr)[size] = input;

3
size++;
}

return size;
}
//function to count the corresponding integers
int counting(int arr[], int n){
int big, small, n_in_range=0;
if(arr[0] > arr[n-1]){//establishes the upper and lower bound of array
big = arr[0]; small = arr[n-1];
}else{
big = arr[n-1]; small = arr[0];
}
//pass entire array and shows if value is between lower and upper bound
for(int i = 1; i < n-1;i++){
printf("\tindex %d: %d\t", i, arr[i]);
if(arr[i] > small && arr[i] < big){
n_in_range++;
green();printf("in range\n");reset();
} else{
red();printf("not in range\n");reset();
}
}
return n_in_range%2;//returns 1 if odd, 0 if even
}

int bubble_sort(int arr[], int n){

for (int i = 0; i < n; i++){


for (int j = 0; j < n-1; j++){
if (arr[j]<arr[j+1]) swap(&arr[j], &arr[j+1]);
}
}

print_result("Bubble_sort", arr, n);


return 0;
}

int selection_sort(int arr[], int n){

for (int i = 0; i < n-1; i++){


int iMin = i;
for (int j = i+1; j < n; j++){
if(arr[j]<arr[iMin])
iMin = j;
}
if (iMin!=i){
swap(&arr[i], &arr[iMin]);
}
}

4
print_result("Selection_sort", arr, n);
return 0;
}

int main(){
int *arr=NULL;
int n=array_input(&arr);

int odd=counting(arr, n);


printf("According to the number of elements (between %d and %d), program
used\n", arr[0], arr[n-1]);
(odd) ? bubble_sort(arr, n) : selection_sort(arr, n);

free(arr);
return 0;
}

Version 2 with parameters passed as pointers (inverse Insertion and Merge


sort)
#include<stdio.h>
#include<malloc.h>
#include<string.h>
//function to swap 2 values by their addresses
void swap(int *x, int *y){
int temp=*x;
*x=*y;
*y=temp;
}
//functions to set and reset displaying color in output
void green(){
printf("\033[0;32m");
}
void red(){
printf("\033[0;31m");
}
void reset(){
printf(" \033[0;37m");
}

//function to print the sorted array and used algorithm


void print_result(char *sort_type, int* arr, int n){
int size = strlen(sort_type);

green();
for (int i = 0; i < size; i++)//prints used algorithm
printf("%c", sort_type[i]);
reset();
5
for (int i = 0; i < n; i++)//prints sorted array
printf("%i ", arr[i]);

//function to get the array


int array_input(int** arr){
int size = 0; //current size of the array
int capacity = 0; //capacity of the array (allocated memory)
int input;

printf("Enter integers (enter a letter to finish):\n");


while (1) {
int result = scanf("%d", &input);
if (result == 0) break;//check if input is not an integer

//check if the array needs to be resized


if (size == capacity) {
capacity = (capacity == 0) ? 1 : capacity + 10;//add capacity

int *temp = (int*)realloc(*arr, capacity *


sizeof(int));//memory realoc

if (temp == NULL) {
printf("Memory reallocation failed.\n");
free(*arr);
return 1;
}
*arr = temp;
}

(*arr)[size] = input;
size++;
}

return size;
}
//function to count the corresponding integers
int counting(int* arr, int *n){
int big, small, n_in_range=0;
if(arr[0] > arr[*n-1]){//establishes the upper and lower bound of
array
big = arr[0]; small = arr[*n-1];
}else{
big = arr[*n-1]; small = arr[0];
}
//pass entire array and shows if value is between lower and upper
bound
for(int i = 1; i < *n-1;i++){
printf("\tindex %d: %d\t", i, arr[i]);

6
if(arr[i] > small && arr[i] < big){
n_in_range++;
green();printf("in range\n");reset();
} else{
red();printf("not in range\n");reset();
}
}
return n_in_range%2;//returns 1 if odd, 0 if even
}
//insertion sort
int insertion_sort(int* arr, int *n){
int j, temp;
for (int i = 1; i < *n; i++){
j=i;
while (j>0 && arr[j]>arr[j-1]){
swap(&arr[j], &arr[j-1]);
j--;
}
}
print_result("Insertion_sort", arr, *n);
return 0;
}
//function to merge the arrays
int merge(int* arr, int l, int m, int r){
int n1 = m-l+1;
int n2 = r-m;
int i=0, j=0, k=l;
int left_arr[n1], right_arr[n2];
for (int i = 0; i < n1; i++) left_arr[i]=arr[l+i];//copy left half in
left_arr
for (int i = 0; i < n2; i++) right_arr[i]=arr[m+1+i];//copy right half
in right_arr
while(i<n1 && j<n2){
if (left_arr[i]>right_arr[j]){
arr[k]=right_arr[j];
j++;
} else{
arr[k]=left_arr[i];
i++;
}
k++;
}

while (i < n1) {//introduce the remaining part in array


arr[k] = left_arr[i];
i++;
k++;
}

while (j < n2) {//introduce the remaining part in array

7
arr[k] = right_arr[j];
j++;
k++;
}

}
int merge_sort_recursion(int* arr, int* l, int* r){//l-left, r-right
if (*l < *r){
int m = *l + (*r - *l) / 2;
merge_sort_recursion(arr, l, &m);
merge_sort_recursion(arr, &m + 1, r);
merge(arr, *l, m, *r);
}

}
int merge_sort(int* arr, int *n){
int l = 0;
int r = *n - 1;
merge_sort_recursion(arr, &l, &r);
print_result("Merge_sort", arr, *n);
}

int main(){
int *arr=NULL;
int n=array_input(&arr);

int odd=counting(arr, &n);


printf("According to the number of elements (between %d and %d),
program used\n", arr[0], arr[n-1]);
(odd) ? insertion_sort(arr, &n) : merge_sort(arr, &n);

free(arr);
return 0;
}

Version with modified code


#include<stdio.h>
#include<malloc.h>
#include<string.h>
//function to swap 2 values by their adresses
void swap(int *x, int *y){
int temp=*x;
*x=*y;
*y=temp;
}
//functions to set and reset diplaying color in output
void green(){
printf("\033[0;32m");
}
8
void red(){
printf("\033[0;31m");
}
void reset(){
printf(" \033[0;37m");
}

//function to print the sorted array and used algorithm


void print_result(char *sort_type, int* arr, int n){
int size = strlen(sort_type);

green();
for (int i = 0; i < size; i++)//prints used algorithm
printf("%c", sort_type[i]);
reset();
for (int i = 0; i < n; i++)//prints sorted array
printf("%i ", arr[i]);
printf("\n");

//function to get the array


int array_input(int** arr){
int size = 0; // Current size of the array
int capacity = 0; // Capacity of the array (allocated memory)
int input;

printf("Enter integers (enter a letter to finish):\n");


while (1) {
int result = scanf("%d", &input);
if (result == 0) break;//check if input is not an integer

//check if the array needs to be resized


if (size == capacity) {
capacity = (capacity == 0) ? 1 : capacity + 10;//add capacity

int *temp = (int*)realloc(*arr, capacity *


sizeof(int));//memory realoc

if (temp == NULL) {
printf("Memory reallocation failed.\n");
free(*arr);
return 1;
}
*arr = temp;
}

(*arr)[size] = input;
size++;
}

9
return size;
}
//function to count the corresponding integers
int counting(int arr[], int n){
int big, small, n_in_range=0;
if(arr[0] > arr[n-1]){//establishes the upper and lower bound of array
big = arr[0]; small = arr[n-1];
}else{
big = arr[n-1]; small = arr[0];
}
//pass entire array and shows if value is between lower and upper
bound
for(int i = 1; i < n-1;i++){
printf("\tindex %d: %d\t", i, arr[i]);
if(arr[i] > small && arr[i] < big){
n_in_range++;
green();printf("in range\n");reset();
} else{
red();printf("not in range\n");reset();
}
}
return n_in_range%2;//returns 1 if odd, 0 if even
}

int bubble_sort(int arr[], int n){


for (int i = 0; i < n; i++){
for (int j = 0; j < n-1; j++){
if (arr[j]<arr[j+1]) swap(&arr[j], &arr[j+1]);
}
}
print_result("Bubble_sort", arr, n);
return 0;
}

int selection_sort(int arr[], int n){


for (int i = 0; i < n-1; i++){
int iMin = i;
for (int j = i+1; j < n; j++){
if(arr[j]<arr[iMin])
iMin = j;
}
if (iMin!=i){
swap(&arr[i], &arr[iMin]);
}
}
print_result("Selection_sort", arr, n);
return 0;
}

10
void odd_sort(int* arr, int n){
int *odd_arr, *even_arr;//create 2 arrays for even and odd numbers
int j = 0, k = 0;
odd_arr = (int*)malloc(sizeof(int) * n);
even_arr = (int*)malloc(sizeof(int) * n);
for(int i = 0; i < n; i++){
if(arr[i]%2) {
odd_arr[j]=arr[i];
j++;
}
else {
even_arr[k]=arr[i];
k++;
}
}
red();printf("Odd numbers sorted ascending:\n");reset();
selection_sort(odd_arr, j);
red();printf("Even numbers sorted descending:\n");reset();
bubble_sort(even_arr, k);
free(odd_arr);
free(even_arr);
}
int main(){
int *arr=NULL;
int n=array_input(&arr);

odd_sort(arr, n);

int odd=counting(arr, n);


printf("According to the number of elements (between %d and %d),
program used\n", arr[0], arr[n-1]);
(odd) ? bubble_sort(arr, n) : selection_sort(arr, n);

free(arr);
return 0;
}
In this version even numbers from original array are ordered in descending order
and odd numbers are ordered in ascending order.

11
Flowchart of shared functions in all versions:

Figure 1.1 - function


print_result()

Figure 1.2 – function


array_input()

12
Figure 1.3 – function counting()

13
Figure 1.4 – function main()

14
Flowcharts of sorting functions in first part:

Figure 1.5 – function


bubble_sort()

Figure 1.6 – function


selection_sort()

15
Flowcharts of sorting functions in second part:

Figure 1.7 – function insertion_sort()

16
Figure 1.8 – function
merge_sort_recursion()

Figure 1.9 – function merge()

17
Flowchart for additional function in modified version:

Figure 1.10 – function odd_sort()

18
Outputs for the first version:

Figure 2.1 – output for 2 scenarios

Outputs for the second version:

Figure 2.2 – output for 2 scenarios

19
Outcome for modified code:

Figure 2.3 – output for modified code

Conclusion:
During this laboratory work I managed to use both already used and some new
sorting algorithms as merge sort. Knowledge about sorting algorithms is crucial in
developing of a software engineer, because it highlights one of the most popular
tasks in programming which can be solved in several ways, either efficient one or
less. Also, I used Plant UML as an instrument to draw all flowcharts. Using
flowcharts, I can clearly show up the logic of the functions and how entire program
works. Plant UML is an instrument where by writing certain code you can adjust
the appearance of the flowchart (links are provided below).

20
Bibliography:
Figure 1.1 print_result() – PlantUML Web Server
Figure 1.2 array_input() - PlantUML Web Server
Figure 1.3 counting() - PlantUML Web Server
Figure 1.4 main() - PlantUML Web Server
Figure 1.5 bubble_sort() - PlantUML Web Server
Figure 1.6 selection_sort() - PlantUML Web Server
Figure 1.7 insertion_sort() - PlantUML Web Server
Figure 1.8 merge_sort_recursion() - PlantUML Web Server
Figure 1.9 merge() - PlantUML Web Server
Figure 1.10 odd_sort() - PlantUML Web Server

21

You might also like