Website Summary Files Commits Branches @commit @diff @stats
path: root / pract_KN / pract. 09 / solutions.cpp
blob: f4f7521b5d560d4b8c438d57f1847b9098724c51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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] << " ";
}