-
Notifications
You must be signed in to change notification settings - Fork 0
/
embeddedVasko_1-2lab.cpp
227 lines (196 loc) · 5.7 KB
/
embeddedVasko_1-2lab.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
/*
Simeon Trendov, [22.03.2024 15:38]
1. Write a program that will print the elements of a one-dimensional array separated by spaces.
Simeon Trendov, [22.03.2024 15:52]
2. Write a program that will calculate the sum and the product of the numbers of an array of n elements.
Simeon Trendov, [22.03.2024 16:07]
3. Write a program that calculates separately the sum of the positive and the sum of the negative numbers of a sequence of n elements.
Simeon Trendov, [22.03.2024 16:17]
4. Write a program that calculates the average mark of a student if the number of subjects is read, and then his grades.
Simeon Trendov, [22.03.2024 16:26]
5. Write a program that examines how many times the letter K occurs in the array.
Simeon Trendov, [22.03.2024 16:35]
Homework: Write a program that determines the largest element in an array with n elements and its location.
*/
using namespace std;
void arrayToSeparate(int *arr,int n)
{
for (size_t i = 0; i < n; i++)
{
arr[i] = rand() % 30 + 1;
cout << arr[i] << " ";
}
cout << "\n";
}
double calculateAverageMark(int *mas,int numSubjects) {
double totalMarks = 0;
for (int i = 0; i < numSubjects; ++i) {
double mark;
mark = rand() % 30 + 1;
totalMarks += mark;
}
double avg = totalMarks / numSubjects;
cout << "\nAverage marks" << avg<<'\n';
return avg;
}
void arraySumAndMultiply(int* arr, int n)
{
int sumResult, productResult;
sumResult = 0;productResult = 1;
for (size_t i = 0; i < n; i++)
{
arr[i] = rand() % 30 + 1;
sumResult += arr[i];
}
cout << "REsult sum`s is " << sumResult << "\n";
for (size_t i = 0; i < n; i++)
{
arr[i] = rand() % 30 + 1;
productResult *= arr[i];
}
cout << "REsult multiply`s is " << productResult;
cout << "\n";
}
void sumOfArr_MinusSign(int *arr,int n)
{
int sResult = 0;
for (size_t i = 0; i < n; i++)
{
arr[i] = (rand() % 30 + 1) * (rand() % 2 == 0 ? 1 : -1);
if(arr[i] < 0)
{
sResult += arr[i];
}
}
cout << "Sum`s minus numbers equal: " << sResult;
}
int countOccurrencesOfK(const string& text) {
int count = 0;
for (char c : text) {
if (c == 'K' || c == 'k') {
count++;
}
}
cout << "k occurences is " << count;
printf("K occurences is %d in word %s",count,text);
return count;
}
void findMaximumValue(int* arr, int n)
{
int maximum = arr[0]; // Initialize maximum with the first element of the array
// Iterate through the array to find the maximum value
for (int i = 1; i < n; i++) // Start from the second element
{
if (arr[i] > maximum) // Compare current element with maximum
{
maximum = arr[i]; // Update maximum if current element is greater
}
}
// Print the maximum value
cout << "Maximum value is: " << maximum << endl;
}
/*
1. Write a program that will print out all of the elements in a row in the matrix whose rows sum is
bigger then the sum of the elements in the diagonal.
2. Write a program that takes the dimensions of a matrix and the matrix as input. The p
rogram
should find the two columns where the sum of the elements is the largest. Then the elements in
those columns should be swapped and reversed (the first becoming the last, the second becoming
the second to last, and so on). Finally, print the newly obtained matrix on the screen.
*/
void findBiggerDiagonalElements()
{
int arr[5][5];
int diagonalSumm = 0,rawSum = 0;
for (size_t i = 0; i < 5; i++)
{
for (size_t j = 0; j< 5; j++)
{
arr[i][j] = rand() % 20 + 1;
if (i == j) {
diagonalSumm += arr[i][j];
cout << diagonalSumm << " \t";
}
}
cout << "\n";
}
}
void printRowsWithSumGreaterThanDiagonal(const vector<vector<int>>& matrix) {
int rows = matrix.size();
int cols = matrix[0].size();
int diagonalSum = 0;
for (int i = 0; i < rows; i++) {
diagonalSum += matrix[i][i];
}
for (int i = 0; i < rows; i++) {
int rowSum = 0;
for (int j = 0; j < cols; j++) {
rowSum += matrix[i][j];
}
if (rowSum > diagonalSum) {
cout << "Row " << i + 1 << ": ";
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
}
void swapAndReverseColumns(vector<vector<int>>& matrix) {
int rows = matrix.size();
int cols = matrix[0].size();
vector<int> colSums(cols, 0);
for (int j = 0; j < cols; j++) {
for (int i = 0; i < rows; i++) {
colSums[j] += matrix[i][j];
}
}
int maxIndex1 = max_element(colSums.begin(), colSums.end()) - colSums.begin();
colSums[maxIndex1] = 0;
int maxIndex2 = max_element(colSums.begin(), colSums.end()) - colSums.begin();
for (int i = 0; i < rows; i++) {
swap(matrix[i][maxIndex1], matrix[i][maxIndex2]);
}
for (int i = 0; i < rows; i++) {
reverse(matrix[i].begin() + maxIndex1, matrix[i].begin() + maxIndex1 + 1);
reverse(matrix[i].begin() + maxIndex2, matrix[i].begin() + maxIndex2 + 1);
}
}
int main()
{
const int length = 10;
string someText = "Holla, kicking wicking,bakery";
int *mas = new int[length];
// 1 Task
arrayToSeparate(mas, length);
// 2 Task
arraySumAndMultiply(mas, length);
// 3 Task
sumOfArr_MinusSign(mas,length);
// 4 Task
calculateAverageMark(mas, length);
// 5 Task
countOccurrencesOfK(someText);
// HOMEWORK 6 && 7 task completed
findMaximumValue(mas, length);
//findBiggerDiagonalElements();
vector<vector<int>> matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printRowsWithSumGreaterThanDiagonal(matrix);
swapAndReverseColumns(matrix);
for (const auto& row : matrix) {
for (int element : row) {
cout << element << " ";
}
cout << endl;
}
delete[] mas;
}