Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 10 / example4.cpp
diff options
context:
space:
mode:
Commit4fb718f5fc6088641ecad2aeccd321b77b8bd356 (patch)
Author
 Justsvetoslavov <justsvetoslavov@gmail.com>
2021-12-26 02:42:24 +0200
Committer
 Justsvetoslavov <justsvetoslavov@gmail.com>
2021-12-26 02:42:24 +0200
Files7f0abd8e37eed78ccc0b45a65de80450d1c59eef /Sem. 10/example4.cpp
Introduction_to_programming_FMI-4fb718f5fc6088641ecad2aeccd321b77b8bd356.tar
Introduction_to_programming_FMI-4fb718f5fc6088641ecad2aeccd321b77b8bd356.tar.gz
Introduction_to_programming_FMI-4fb718f5fc6088641ecad2aeccd321b77b8bd356.zip
Parent158370669b24142a71ce7778b46f5a054cafed8a (diff)
Parentb3e29bba94da067984c841d7904887a4bd5995ea (diff)
Merge branch 'main' of https://github.com/Angeld55/Introduction_to_programming_FMI
1 files changed, 85 insertions, 0 deletions
-rw-r--r--Sem. 10/example4.cpp85
diff --git a/Sem. 10/example4.cpp b/Sem. 10/example4.cpp
new file mode 100644
index 0000000..8d2105e
--- /dev/null
+++ b/Sem. 10/example4.cpp
@@ -0,0 +1,85 @@
+#include <iostream>
+using namespace std;
+
+
+unsigned input(int**& matrix, int*& sizes)
+{
+ unsigned N;
+ cin >> N;
+
+ matrix = new int*[N];
+ sizes = new int[N];
+
+ for (unsigned i = 0; i < N; i++)
+ {
+ unsigned currentRowSize;
+ cin >> currentRowSize;
+
+ matrix[i] = new int[currentRowSize];
+ sizes[i] = currentRowSize;
+
+ for (unsigned j = 0; j < currentRowSize; j++)
+ cin >> matrix[i][j];
+ }
+
+ return N;
+}
+
+
+int* concat(const int* row1, unsigned row1Size, const int* row2, unsigned row2Size)
+{
+ int* result = new int[row1Size + row2Size];
+
+ for (unsigned i = 0; i < row2Size; i++)
+ result[i] = row2[i];
+
+ for (unsigned i = 0; i < row1Size; i++)
+ result[row2Size + i] = row1[i];
+
+ return result;
+}
+
+void freeMatrix(int** matrix, unsigned rowsSize)
+{
+ for (int i = 0; i < rowsSize; i++)
+ delete[] matrix[i];
+ delete[] matrix;
+}
+
+void task(int** matrix, unsigned N, int* sizes)
+{
+ unsigned resultRowsCount = (N + 1) / 2;
+ int** resultMatrix = new int*[resultRowsCount];
+
+ int* resultSizes = new int[resultRowsCount];
+
+ for (unsigned i = 0; i < resultRowsCount; i++)
+ {
+ resultSizes[i] = sizes[i] + sizes[N - i - 1];
+ resultMatrix[i] = concat(matrix[i], sizes[i], matrix[N - i - 1], sizes[N - i - 1]);
+ }
+
+ //print
+ for (unsigned i = 0; i < resultRowsCount; i++)
+ {
+ for (unsigned j = 0; j < resultSizes[i]; j++)
+ cout << resultMatrix[i][j] << " ";
+ cout << endl;
+ }
+
+ delete[] resultSizes;
+ freeMatrix(resultMatrix, resultRowsCount);
+
+ delete[] sizes;
+ freeMatrix(matrix, N);
+}
+
+int main()
+{
+ int** matrix;
+ int* sizes;
+
+ unsigned N = input(matrix, sizes); //not sure if I need this func
+
+ task(matrix, N, sizes);
+}