LAB3
LAB3
GRIGORAȘ DUMITRU
Report
Laboratory Work No.3
Checked:
Burlacu Natalia, PhD, associate professor
Department of Software and Automation Engineering,
FCIM Faculty, UTM
Chisinau – 2023
Task:
1. Solve the following problems in C, writing your own functions according to thegiven sta
tements. Write the solution of the problem by procedural approach in twoversions:
a. with the use of the method of transmitting the parametric functions byvalue;
b. with the use of the method of passing parameters of functions byaddress/pointer
s (the formal parameter will be a pointer to the value of thecorresponding object)
.
c. To draw the block diagram corresponding to the solved problem.
2. For the second version of the presented solution, you should re-write subtask 1of your pr
oblem using the recursive approach.
The second solution should be integrated with the other functions required underyour pr
oblem statement (with pointers).
— Due to the fact that in each problem in version 1, you should use two specifiedsorting
methods, in version 2, of the (modified) problem, you should use the sortingmethods as
Combo Sort & Radix Sort.
Problem 15:
2
#include<iostream>
using namespace std;
int n;
int m;
int length;
int indexI;
int indexJstart;
int indexJend;
//Function for condition 1
void cond1(int *arr, int target_sum, int *shortest, int c) {
int arr2[n];
for(int i =0; i < m; i ++){
int l = 0;
for(int j = i; j < m; j++){
arr2[l] = arr[j];
l++;
}
int index = m;
while(index >1){
int sum = 0;
for(int k = 0; k < index; k++){
sum += arr2[k];
}
if(sum == target_sum){
cout<<"Subarray: ";
for(int k = 0; k <index; k++){
cout<<arr2[k]<<" ";
if(index <= length){
shortest[k] = arr2[k];
length = index;
}
}
if(index <= length){
indexJstart = i;
indexJend = length;
indexI = c;
}
cout<<endl;
}
index--;
}
3
}
}
//Counting sort
void countingSort(int *arr, int n) {
int maxElement = arr[0];
int minElement = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > maxElement) {
maxElement = arr[i];
}
if (arr[i] < minElement) {
minElement = arr[i];
}
}
int count[maxElement - minElement + 1] = {0};
for (int i = 0; i < n; i++) {
count[arr[i] - minElement]++;
}
for (int i = 1; i <= maxElement - minElement; i++) {
count[i] += count[i - 1];
}
int output[n];
for (int i = n - 1; i >= 0; i--) {
output[count[arr[i] - minElement] - 1] = arr[i];
count[arr[i] - minElement]--;
}
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}
//Swap elements
void swap(int *arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void minHeapify(int *arr, int n, int i) {
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
int main(){
cout<<"Enter nr of rows: ";
cin>>n;
cout<<"Enter nr of columns: ";
cin>>m;
length = m+1;
int **matrix = new int*[n];
int *shortest = new int[n];
for(int i = 0; i < n; i++){
matrix[i] = new int[m];
}
cout << "Enter the elements of the matrix:" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> matrix[i][j];
}
}
int S;
6
cout<<"Enter sum: ";
cin>>S;
int var;
//Menu
while(var!= 3){
cout<<endl<<endl;
cout<<"-------------------------------------------Menu---------------
-------------------------- \n";
cout<<"1. If nr of elements forming the subarrays found is an
even: \n";
cout<<" TRUE) Counting Sort ASC\n";
cout<<" FALSE) Heap Sort DES\n";
cout<<"\n2. If nr of elements forming the subarrays found is
an even: \n ";
cout<<" TRUE) Combo Sort ASC\n";
cout<<" FALSE) Radix Sort DES\n";
cout<<"3. Exit";
cout<<endl<<endl;
cout<<"--------------------------------------------------------------
--------------------------- \n";
cout<<"Your choice: ";
cin>>var;
switch(var){
case 1:
if(length%2==0){
countingSort(shortest, length);
}
else{
heapSortDescending(shortest, length);
}
break;
case 2:
if(length%2==0){
comboSort(shortest, length);
}
else{
radixSortDescending(shortest, length);
}
break;
case 3: break;
}
int i = 0;
7
for(int j = indexJstart; j <= indexJend; j++){
matrix[indexI][j] = shortest[i];
i++;
}
cout<<"Modified matrix: \n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout<<matrix[i][j]<< " ";
}
cout<<endl;
}
}
}
8
FlowChart:
9
10
11
12
13
14
15
Outputs:
16
Conclusion
During this lab work, I had the opportunity to work with matrices using
pointers, which gave me valuable experience in memory allocation and data
manipulation. I also explored different sorting algorithms such as radix sort, combo
sort, counting sort, and heap sort, which helped me gain a deeper understanding of
how to efficiently sort large datasets.
Overall, this lab work was a valuable learning experience and helped me
develop my skills in data manipulation and sorting algorithms.
17