Масло Масляное
Мыслитель
(9979)
2 недели назад
#include
#include
#include
#include
#define BUFFER (size_t)0x40
#define RAND(min, max) (rand() % ((max) - (min)) + (min))
typedef struct {
int max_negative_value;
size_t position;
} Data;
int matrix[BUFFER][BUFFER] = { 0 };
size_t N = 0;
size_t set_rank(void) {
while (!N || BUFFER < N) {
printf("Set N: ");
scanf("%zu", &N);
}
}
Data max_negative_element_position(void) {
size_t i, j;
Data data;
data.position = 0;
data.max_negative_value = INT_MIN;
size_t position = 0;
for (i = 0; i < N; ++i) {
for (j = 0; j < N; ++j) {
++position;
if (matrix[i][j] < 0 && data.max_negative_value < matrix[i][j]) {
data.max_negative_value = matrix[i][j];
data.position = position;
}
if (data.max_negative_value == -1) break;
}
if (data.max_negative_value == -1) break;
}
return data;
}
int max_element(void) {
size_t i, j;
int value = INT_MIN;
for (i = 0; i < N; ++i) {
for (j = 0; j < N; ++j) {
if (value < matrix[i][j]) {
value = matrix[i][j];
}
}
}
return value;
}
size_t count(int value) {
size_t i, j;
size_t quantity = 0;
for (i = 0; i < N; ++i) {
for (j = 0; j < N; ++j) {
if (matrix[i][j] == value) {
++quantity;
}
}
}
return quantity;
}
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
void create_matrix(int a, int b) {
size_t i, j;
if (b < a) swap(&a, &b);
srand((unsigned)time(NULL));
for (i = 0; i < N; ++i) {
for (j = 0; j < N; ++j) {
matrix[i][j] = RAND(a, b);
}
}
}
void show_matrix(int width) {
size_t i, j;
for (i = 0; i < N; ++i) {
for (j = 0; j < N; ++j) {
printf(" %*d ", width, matrix[i][j]);
}
puts("");
}
puts("");
}
int main(void) {
int a, b, max;
Data data;
size_t quantity;
set_rank();
create_matrix(-10, 11);
show_matrix(5);
data = max_negative_element_position();
printf("Value: %d\nPosition: %zu\n", data.max_negative_value, data.position);
max = max_element();
printf("Max element: %d\n", max);
quantity = count(max);
printf("Count max elements: %zu\n", quantity);
return 0;
}
А он есть! Кто сможет найти первым?