Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Report SDA Lab 3

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

MAXIM COSTOV STUDENT

Report
Laboratory Work No.3

of the "Data Structures and Algorithms" course

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

Chisinau – 2023
Figure 1. 1 - Task

Problem statement:

2
Figure 1. 2 - Problem statement

1. The program code, having relevant comments in it and the Block


diagram;

--------The version with the transmission of function parameters by value-------


#include <stdio.h>

void array_filling(int n, int m, int arr[n][m]);


void array_printing(int n, int m, int arr[n][m]);
int subarrays_searching(int n, int m, int arr[m][n], int *l, int *h);
void swap(int* a, int* b);
void heapSort(int N, int j, int m, int arr[N][j]);
void heapify(int N, int j, int m, int i, int arr[N][j]);
void countingSort(int n, int m, int j, int array[n][j]);

int main()
{
int n, m, l, h;

3
printf("Enter number of rows: ");
scanf("%d", &n);
printf("Enter number of columns: ");
scanf("%d", &m);
int arr[n][m];

array_filling(n, m, arr);
array_printing(n, m, arr);
printf("\n");
int c = subarrays_searching(n, m, arr, &l, &h);
printf("Total number of subattays = %i \n", c);
printf("Matrix with resorted column is: \n");
printf("\n");

if (c % 2 == 0)
{
heapSort(n, m, l, arr);
}
else
{
countingSort(n, h, m, arr);
}

array_printing(n, m, arr);
}

void array_filling(int n, int m, int arr[n][m])


{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("Enter the element: ");
scanf("%d", &arr[i][j]);
}
}
}

void array_printing(int n, int m, int arr[n][m])


{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}

int subarrays_searching(int n, int m, int arr[m][n], int *l, int *h)


{
int subarrs = 0;
int tamm = n;
int hamm = 0;
for (int j = 0; j < m; j++)

4
{
int d = 0;
int z = 0;
int init = 0;
for (int i = 0; i < n; i++)
{
if (arr[i][j] == 0 || i == (n-1))
{
if (d != 0)
{
int amm = i - init;
int subarr[amm];
for (int k = 0; k <= amm; k++)
{
subarr[k] = arr[init + k][j];
}
printf("C%i subarrs:\n", j);
printf("a[%i, %i] to a [%i, %i] --> {", j, init, j, i);
for (int k = 0; k < amm; k++)
{
printf("%i, ", subarr[k]);
}
printf("%i}\n", subarr[amm]);
subarrs++;
if (amm < tamm)
{
*l = j;
}
if (amm > hamm)
{
*h = j;
}
}
init = i+1;
d = 0;
}
if (arr[i][j] % 2 == 0)
{
d++;
}
}

}
return subarrs;
}

void swap(int* a, int* b)


{

int temp = *a;


*a = *b;
*b = temp;
}

// To heapify a subtree rooted with node i

5
// which is an index in arr[].
// n is size of heap
void heapify(int N, int j, int m, int i, int arr[N][j])
{
// Find largest among root,
// left child and right child

// Initialize largest as root


int largest = i;

// left = 2*i + 1
int left = 2 * i + 1;

// right = 2*i + 2
int right = 2 * i + 2;

// If left child is larger than root


if (left < N && arr[left][m] < arr[largest][m])

largest = left;

// If right child is larger than largest


// so far
if (right < N && arr[right][m] < arr[largest][m])

largest = right;

// Swap and continue heapifying


// if root is not largest
// If largest is not root
if (largest != i) {

swap(&arr[i][m], &arr[largest][m]);

// Recursively heapify the affected


// sub-tree
heapify(N, j, m, largest, arr);
}
}

// Main function to do heap sort


void heapSort(int N, int j, int m, int arr[N][j])
{
// Build max heap
for (int i = N / 2 - 1; i >= 0; i--)

heapify(N, j, m, i, arr);

// Heap sort
for (int i = N - 1; i >= 0; i--) {

swap(&arr[0][m], &arr[i][m]);

// Heapify root element


// to get highest element at
// root again

6
heapify(i, j, m, 0, arr);
}
}

void countingSort(int n, int m, int j, int array[n][j]) {


int output[100];

// Find the largest element of the array


int max = array[0][m];
for (int i = 1; i < n; i++) {
if (array[i][m] > max)
max = array[i][j];
}

// The size of count must be at least (max+1) but


// we cannot declare it as int count(max+1) in C as
// it does not support dynamic memory allocation.
// So, its size is provided statically.
int count[100];

// Initialize count array with all zeros.


for (int i = 0; i <= max; ++i) {
count[i] = 0;
}

// Store the count of each element


for (int i = 0; i < n; i++) {
count[array[i][m]]++;
}

// Store the cummulative count of each array


for (int i = 1; i <= max; i++) {
count[i] += count[i - 1];
}

// Find the index of each element of the original array in count array, and
// place the elements in output array
for (int i = n - 1; i >= 0; i--) {
output[count[array[i][m]] - 1] = array[i][m];
count[array[i][m]]--;
}

// Copy the sorted elements into original array


for (int i = 0; i < n; i++) {
array[i][m] = output[i];
}
}

--------------Version with passing function parameters via pointers---------------


c
#include <stdio.h>
#include <stdlib.h>

void array_filling(int n, int m, int **arr);

7
void array_printing(int n, int m, int **arr);
int subarrays_searching(int n, int m, int **arr, int *l, int *h);
void swap(int* a, int* b);
void heapSort(int N, int j, int m, int **arr);
void heapify(int N, int j, int m, int i, int **arr);
void countingSort(int n, int m, int j, int **array);

int main()
{
int n, m, l = 0, h = 0;
printf("Enter number of rows: ");
scanf("%d", &n);
printf("Enter number of columns: ");
scanf("%d", &m);

int **arr = malloc(n * sizeof(int*));


int **ptr = arr;
for (int i = 0; i < n; i++, ptr++)
{
*ptr = malloc(m * sizeof(int));
}

array_filling(n, m, arr);
array_printing(n, m, arr);
printf("\n");
int c = subarrays_searching(n, m, arr, &l, &h);
printf("Total number of subarrays = %i \n", c);
printf("Matrix with resorted column is: \n");
printf("\n");

if (c % 2 == 0)
{
heapSort(n, l, m, arr);
}
else
{
countingSort(n, m, h, arr);
}

array_printing(n, m, arr);

// Free allocated memory


ptr = arr;
for (int i = 0; i < n; i++, ptr++)
{
free(*ptr);
}
free(arr);
return 0;
}

void array_filling(int n, int m, int **arr)


{
int **ptr = arr;
for (int i = 0; i < n; i++, ptr++)
{

8
int *inner_ptr = *ptr;
for (int j = 0; j < m; j++, inner_ptr++)
{
printf("Enter the element: ");
scanf("%d", inner_ptr);
}
}
}

void array_printing(int n, int m, int **arr)


{
int **ptr = arr;
for (int i = 0; i < n; i++, ptr++)
{
int *inner_ptr = *ptr;
for (int j = 0; j < m; j++, inner_ptr++)
{
printf("%d ", *inner_ptr);
}
printf("\n");
}
}

int subarrays_searching(int n, int m, int **arr, int *l, int *h)


{
int subarrs = 0;
int tamm = n;
int hamm = 0;
for (int j = 0; j < m; j++)
{
int d = 0;
int z = 0;
int init = 0;
for (int i = 0; i < n; i++)
{
if (*(*(arr + i) + j) == 0 || i == (n-1))
{
if (d != 0)
{
int amm = i - init + 1;
int *subarr = malloc(amm * sizeof(int));
int *subarr_ptr = subarr;
for (int k = 0; k < amm; k++, subarr_ptr++)
{
*subarr_ptr = *(*(arr + init + k) + j);
}
printf("Column %i subarrays:\n", j);
printf("a[%i, %i] to a [%i, %i] --> {", init, j, i, j);
subarr_ptr = subarr;
for (int k = 0; k < amm; k++, subarr_ptr++)
{
printf("%i, ", *subarr_ptr);
}
printf("}\n");
subarrs++;
if (amm < tamm)

9
{
*l = j;
}
if (amm > hamm)
{
*h = j;
}
free(subarr);
}
init = i + 1;
d = 0;
}
if (*(*(arr + i) + j) % 2 == 0)
{
d++;
}
}
}
return subarrs;
}

void swap(int* a, int* b)


{
int temp = *a;
*a = *b;
*b = temp;
}

void heapify(int N, int j, int m, int i, int **arr)


{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < N && *(*(arr + left) + m) > *(*(arr + largest) + m))


largest = left;

if (right < N && *(*(arr + right) + m) > *(*(arr + largest) + m))


largest = right;

if (largest != i) {
swap(&*(*(arr + i) + m), &*(*(arr + largest) + m));
heapify(N, j, m, largest, arr);
}
}
void heapSort(int N, int j, int m, int **arr)
{
for (int i = N / 2 - 1; i >= 0; i--)
heapify(N, j, m, i, arr);

for (int i = N - 1; i >= 0; i--) {


swap(&*(*(arr + 0) + m), &*(*(arr + i) + m));
heapify(i, j, m, 0, arr);
}
}

10
void countingSort(int n, int m, int j, int **array) {
int max = *(*(array + 0) + j);
int **ptr = array;
for (int i = 1; i < n; i++, ptr++)
{
if (*(*ptr + j) > max)
max = *(*ptr + j);
}

int *count = malloc((max + 1) * sizeof(int));


int *output = malloc(n * sizeof(int));

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


count[i] = 0;
}

ptr = array;
for (int i = 0; i < n; i++, ptr++)
{
count[*(*ptr + j)]++;
}

for (int i = 1; i <= max; i++) {


count[i] += count[i - 1];
}

ptr = array;
for (int i = n - 1; i >= 0; i--, ptr++)

-----------------------------------Modified version---------------------------------------
#include <stdio.h>

void array_filling(int n, int m, int arr[n][m]);

void array_printing(int n, int m, int arr[n][m]);

void findSubarrays(int m, int n, int arr[m][n], int j, int i, int init, int
d, int tamm, int hamm, int *l, int *h, int *subarrs);

int subarrays_searching(int n, int m, int arr[m][n], int *l, int *h);

void swap(int* a, int* b);

void heapSort(int N, int j, int m, int arr[N][j]);

void heapify(int N, int j, int m, int i, int arr[N][j]);

void countingSort(int n, int m, int j, int array[n][j]);

11
int main()

int n, m, l, h;

printf("Enter number of rows: ");

scanf("%d", &n);

printf("Enter number of columns: ");

scanf("%d", &m);

int arr[n][m];

array_filling(n, m, arr);

array_printing(n, m, arr);

printf("\n");

int c = subarrays_searching(n, m, arr, &l, &h);

printf("Total number of subattays = %i \n", c);

printf("Matrix with resorted column is: \n");

printf("\n");

if (c % 2 == 0)

heapSort(n, m, l, arr);

else

countingSort(n, h, m, arr);

array_printing(n, m, arr);

12
void array_filling(int n, int m, int arr[n][m])

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

for (int j = 0; j < m; j++)

printf("Enter the element: ");

scanf("%d", &arr[i][j]);

void array_printing(int n, int m, int arr[n][m])

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

for (int j = 0; j < m; j++)

printf("%d ", arr[i][j]);

printf("\n");

void findSubarrays(int m, int n, int arr[m][n], int j, int i, int init, int
d, int tamm, int hamm, int *l, int *h, int *subarrs) {

if (i == n) {

if (d != 0) {

13
int amm = i - init;

int subarr[amm];

for (int k = 0; k < amm; k++) {

subarr[k] = arr[init + k][j];

printf("C%i subarrs:\n", j);

printf("a[%i, %i] to a [%i, %i] --> {", j, init, j, i - 1);

for (int k = 0; k < amm; k++) {

printf("%i, ", subarr[k]);

printf("%i}\n", subarr[amm - 1]);

(*subarrs)++;

if (amm < tamm) {

*l = j;

if (amm > hamm) {

*h = j;

return;

if (arr[i][j] == 0 || i == (n - 1)) {

if (d != 0) {

int amm = i - init;

int subarr[amm];

for (int k = 0; k < amm; k++) {

subarr[k] = arr[init + k][j];

14
printf("C%i subarrs:\n", j);

printf("a[%i, %i] to a [%i, %i] --> {", j, init, j, i - 1);

for (int k = 0; k < amm; k++) {

printf("%i, ", subarr[k]);

printf("%i}\n", subarr[amm - 1]);

(*subarrs)++;

if (amm < tamm) {

*l = j;

if (amm > hamm) {

*h = j;

init = i + 1;

d = 0;

if (arr[i][j] % 2 == 0) {

d++;

findSubarrays(m, n, arr, j, i + 1, init, d, tamm, hamm, l, h, subarrs);

int subarrays_searching(int n, int m, int arr[m][n], int *l, int *h) {

int subarrs = 0;

int tamm = n;

int hamm = 0;

15
for (int j = 0; j < m; j++) {

findSubarrays(m, n, arr, j, 0, 0, 0, tamm, hamm, l, h, &subarrs);

return subarrs;

void swap(int* a, int* b)

int temp = *a;

*a = *b;

*b = temp;

// To heapify a subtree rooted with node i

// which is an index in arr[].

// n is size of heap

void heapify(int N, int j, int m, int i, int arr[N][j])

// Find largest among root,

// left child and right child

// Initialize largest as root

int largest = i;

// left = 2*i + 1

int left = 2 * i + 1;

16
// right = 2*i + 2

int right = 2 * i + 2;

// If left child is larger than root

if (left < N && arr[left][m] < arr[largest][m])

largest = left;

// If right child is larger than largest

// so far

if (right < N && arr[right][m] < arr[largest][m])

largest = right;

// Swap and continue heapifying

// if root is not largest

// If largest is not root

if (largest != i) {

swap(&arr[i][m], &arr[largest][m]);

// Recursively heapify the affected

// sub-tree

heapify(N, j, m, largest, arr);

// Main function to do heap sort

void heapSort(int N, int j, int m, int arr[N][j])

17
{

// Build max heap

for (int i = N / 2 - 1; i >= 0; i--)

heapify(N, j, m, i, arr);

// Heap sort

for (int i = N - 1; i >= 0; i--) {

swap(&arr[0][m], &arr[i][m]);

// Heapify root element

// to get highest element at

// root again

heapify(i, j, m, 0, arr);

void countingSort(int n, int m, int j, int array[n][j]) {

int output[100];

// Find the largest element of the array

int max = array[0][m];

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

if (array[i][m] > max)

max = array[i][j];

// The size of count must be at least (max+1) but

18
// we cannot declare it as int count(max+1) in C as

// it does not support dynamic memory allocation.

// So, its size is provided statically.

int count[100];

// Initialize count array with all zeros.

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

count[i] = 0;

// Store the count of each element

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

count[array[i][m]]++;

// Store the cummulative count of each array

for (int i = 1; i <= max; i++) {

count[i] += count[i - 1];

// Find the index of each element of the original array in count array, and

// place the elements in output array

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

output[count[array[i][m]] - 1] = array[i][m];

count[array[i][m]]--;

// Copy the sorted elements into original array

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

19
array[i][m] = output[i];

Output first version:

Figure 2.1 – Output for the first version

Conclusion:

During the lab work, I applied the knowledge acquired from various courses
and seminars. This involved using different sorting techniques for organizing
subarrays within a 2-D matrix. The process started with basic methods like Heap
Sort, used when the total number of found subarrays was an even number. It then

20
progressed to more complex methods like Counting Sort, used when the total
number of found subarrays was an odd number.

In addition to sorting, I developed an algorithm to identify all subarrays in


each column of the matrix whose product of the elements is an even number. The
total number of such subarrays was counted and printed per column.

In case there were no subarrays that corresponded to the stipulated


condition, a message was displayed about the lack of elements that would satisfy
the problem’s statement. After each manipulation, the results were displayed as a
conclusion. If there were several rows/columns having smallest/largest numbers,
the program sorted the last row/column that corresponded to the given condition.

Furthermore, I devised an algorithm for displaying the matrix and the


identified subarrays in a well-structured and visually appealing format. This not
only made the output easier to understand but also highlighted the effectiveness of
the sorting techniques used.

Overall, this task provided a practical application of various sorting


techniques and array manipulation methods, reinforcing the concepts learned in the
classroom. It was a valuable exercise in problem-solving and algorithm
development.

21

You might also like