Professional Documents
Culture Documents
FAF233 Lab1 PostoroncaDumitru
FAF233 Lab1 PostoroncaDumitru
FAF233 Lab1 PostoroncaDumitru
Postoronca Dumitru
Report
Laboratory Work No1
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:
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
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]);
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
}
4
print_result("Selection_sort", arr, n);
return 0;
}
int main(){
int *arr=NULL;
int n=array_input(&arr);
free(arr);
return 0;
}
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]);
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++;
}
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);
free(arr);
return 0;
}
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");
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
}
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);
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:
12
Figure 1.3 – function counting()
13
Figure 1.4 – function main()
14
Flowcharts of sorting functions in first part:
15
Flowcharts of sorting functions in second part:
16
Figure 1.8 – function
merge_sort_recursion()
17
Flowchart for additional function in modified version:
18
Outputs for the first version:
19
Outcome 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