Website Summary Files Commits Branches @commit @diff @stats
path: root / pract_KN / pract. 09 / solutions.cpp
diff options
context:
space:
mode:
Commit410a7ad0952cdbe70012a85e902232ec5438cb0d (patch)
Author
 Stoycho Kyosev <68247567+stoychoX@users.noreply.github.com>
2021-12-10 00:41:05 +0200
Committer
 GitHub <noreply@github.com>
2021-12-10 00:41:05 +0200
Files537ec3801c795fa600b6ef9dce945df2a5c60d1b /pract_KN/pract. 09/solutions.cpp
Introduction_to_programming_FMI-410a7ad0952cdbe70012a85e902232ec5438cb0d.tar
Introduction_to_programming_FMI-410a7ad0952cdbe70012a85e902232ec5438cb0d.tar.gz
Introduction_to_programming_FMI-410a7ad0952cdbe70012a85e902232ec5438cb0d.zip
Parent4feb4c14540206a166117f8ad14bdda133acfb86 (diff)
Add files via upload
1 files changed, 161 insertions, 0 deletions
-rw-r--r--pract_KN/pract. 09/solutions.cpp161
diff --git a/pract_KN/pract. 09/solutions.cpp b/pract_KN/pract. 09/solutions.cpp
new file mode 100644
index 0000000..f4f7521
--- /dev/null
+++ b/pract_KN/pract. 09/solutions.cpp
@@ -0,0 +1,161 @@
+#include<iostream>
+#include<cstring>
+#pragma warning (disable : 4996)
+
+const int MAXSIZE = 50;
+
+//helpers:
+
+//making every element of our matrix zero.
+void nullMatrix(int matrix[MAXSIZE][MAXSIZE], int length) {
+ for (int i = 0; i < length; i++)
+ for (int j = 0; j < length; j++)
+ matrix[i][j] = 0;
+}
+
+//printing our matrix.
+void printMatrix(const int matrix[MAXSIZE][MAXSIZE], int length, int width = 3) {
+ for (int i = 0; i < length; i++) {
+ for (int j = 0; j < length; j++) {
+ std::cout.width(2);
+ std::cout << matrix[i][j] << " ";
+ }
+ std::cout << std::endl;
+ }
+}
+
+//initializing our matrix
+void initMatrix(int matrix[MAXSIZE][MAXSIZE], int length) {
+ for (int i = 0; i < length; i++)
+ for (int j = 0; j < length; j++)
+ matrix[i][j] = (i + j) * (i + j + 1) / 2 + j;
+}
+
+//--> first:
+int getSumBelowMainDiagonal(const int matrix[MAXSIZE][MAXSIZE], int length) {
+ int res = 0;
+ for (int i = 0; i < length; i++) {
+ for (int j = 0; j < i; j++) {
+ //res += matrix[i][j] * (matrix[i][j] % 2 == 0);
+ if (matrix[i][j] % 2 == 0)
+ res += matrix[i][j];
+ }
+ }
+ return res;
+}
+
+
+//--> second:
+int getSumAboveMainDiag(const int matrix[MAXSIZE][MAXSIZE], int length) {
+ int res = 0;
+ for (int i = 0; i < length; i++) {
+ for (int j = length - 1; j >= i; j--) {
+ if (matrix[i][j] % 2 != 0)
+ res += matrix[i][j];
+ }
+ }
+ return res;
+}
+
+
+//--> third:
+void sqrtDiag(int matrix[MAXSIZE][MAXSIZE], int length) {
+ for (int i = 0, j = length - 1; i < length; i++, --j)
+ matrix[i][j] *= matrix[i][j];
+}
+
+
+//--> fourth:
+void flipRight(int matrix[MAXSIZE][MAXSIZE], int length) {
+ int copyMatrix[MAXSIZE][MAXSIZE];
+ for (int i = 0; i < length; i++)
+ for (int j = 0; j < length; j++)
+ copyMatrix[j][length - i - 1] = matrix[i][j];
+
+ for (int i = 0; i < length; i++)
+ for (int j = 0; j < length; j++)
+ matrix[i][j] = copyMatrix[i][j];
+}
+
+void flipLeft(int matrix[MAXSIZE][MAXSIZE], int length) {
+ int copyMatrix[MAXSIZE][MAXSIZE];
+
+ for (int i = 0; i < length; i++)
+ for (int j = 0; j < length; j++)
+ copyMatrix[length - j - 1][i] = matrix[i][j];
+
+ for (int i = 0; i < length; i++)
+ for (int j = 0; j < length; j++)
+ matrix[i][j] = copyMatrix[i][j];
+}
+
+void flipDiag(int matrix[MAXSIZE][MAXSIZE], int length) {
+ int copyMatrix[MAXSIZE][MAXSIZE];
+ for (int i = 0; i < length; i++)
+ for (int j = 0; j < length; j++)
+ copyMatrix[j][i] = matrix[i][j];
+
+ for (int i = 0; i < length; i++)
+ for (int j = 0; j < length; j++)
+ matrix[i][j] = copyMatrix[i][j];
+}
+
+//--> fifth
+void swapWords(char* fst, char* snd) {
+ char temp[64];
+ strcpy(temp, fst);
+ strcpy(fst, snd);
+ strcpy(snd, temp);
+}
+
+void sortWords(char words[64][64], int wordsCount) {
+ //bubble sort:
+ bool swapped = false;
+ for (int i = 0; i < wordsCount; i++) {
+ swapped = false;
+ for (int j = 0; j < wordsCount - i - 1; j++) {
+ if (strcmp(words[j], words[j + 1]) > 0) {
+ swapped = true;
+ swapWords(words[j], words[j + 1]);
+ }
+ }
+ if (!swapped) break;
+ }
+
+}
+
+bool isLetter(char c) {
+ return (c >= 'a' && c <= 'z') ||
+ (c >= 'A' && c <= 'Z');
+}
+
+void getSortedWords(const char* arg) {
+ char words[64][64];
+ unsigned length = strlen(arg);
+ int wordsCount = 0;
+ int currLetter = 0;
+
+ for (int i = 0; i < length; i++) {
+ if (arg[i] != ' ' && isLetter(arg[i])) {
+ words[wordsCount][currLetter++] = arg[i];
+ }
+ else {
+ words[wordsCount][currLetter] = '\0';
+ ++wordsCount;
+ currLetter = 0;
+ }
+ }
+ words[wordsCount][currLetter] = '\0';
+
+ ++wordsCount;
+
+ for (int i = 0; i < wordsCount; i++)
+ std::cout << words[i] << " ";
+
+ sortWords(words, wordsCount);
+
+ std::cout << std::endl;
+
+ for (int i = 0; i < wordsCount; i++)
+ std::cout << words[i] << " ";
+}