#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <locale.h>
#include <math.h>
float ** create_matrix(float **, const size_t, const size_t);
float * create_array(float *, const size_t);
int random_matrix(float **, const size_t, const size_t, const int _left, int _rigth);
void fill_array_min_elements_of_lines_of_matrix(float *, float **, const size_t, const size_t);
void show_matrix(float **, const size_t, const size_t);
void show_array(float *, const size_t);
void destroy_matrix(float **, size_t);
void destroy_array(float *); //
float search_min_element(float *, size_t);
int main() {
size_t rows, cols;
float ** matrix = NULL, * arr = NULL;
setlocale(LC_CTYPE, "Russian_Russia.1251");
printf("\n Введите строки: "); scanf_s("%u", &rows);
printf(" Введите столбцы: "); scanf_s("%u", &cols);
matrix = create_matrix(matrix, rows, cols);
srand((unsigned)time(NULL));
if (random_matrix(matrix, rows, cols, 1, 100)) {
printf("\n\n\t\tИсходная матрица: \n\n");
show_matrix(matrix, rows, cols);
arr = create_array(arr, rows);
fill_array_min_elements_of_lines_of_matrix(arr, matrix, rows, cols);
printf("\n\n\t Массив из минимальных элементов строк матрицы: \n\n");
show_array(arr, rows);
destroy_array(arr);
} else printf("\n\nОшибка! \n\a");
destroy_matrix(matrix, rows);
_getch();
return 0;
}
int random_matrix(float ** _matrix, const size_t _rows, const size_t _cols, const int _left, int _rigth) {
size_t row, col;
float temp;
if (_left >= _rigth) return 0;
if (_left < 0 && _rigth >= 0) _rigth += abs(_left);
else if (_left < 0 && _rigth < 0) _rigth = abs(_left) - abs(_rigth);
else if (_left >= 0 && _rigth > 0) _rigth -= _left;
for (row = 0; row < _rows; row++)
for (col = 0; col < _cols; col++) {
temp = (float)(rand() % _rigth + _left) + (float)rand() / 100000.0f;
if (temp > _left + 1) temp -= 1.0f;
_matrix[row][col] = temp;
}
return 1;
}
void fill_array_min_elements_of_lines_of_matrix(float * _array, float ** _matrix, const size_t _rows, const size_t _cols) {
size_t row;
for (row = 0; row < _rows; row++) _array[row] = search_min_element(_matrix[row], _cols);
}
void show_matrix(float ** _matrix, const size_t _rows, const size_t _cols) {
size_t row, col;
if (_cols && _rows) for (row = 0; row < _rows; row++, printf("\n")) for (col = 0; col < _cols; col++) printf(".5f", _matrix[row][col]);
else printf("Нет данных! \n");
}
void show_array(float * _array, const size_t _size) {
size_t n;
for (n = 0; n < _size; n++) printf(".5f", _array[n]);
printf("\n");
}
float ** create_matrix(float ** _matrix, const size_t _rows, const size_t _cols) {
size_t row;
_matrix = (float **)calloc(_rows, sizeof(float *));
for (row = 0; row < _rows; row++) _matrix[row] = (float *)calloc(_cols, sizeof(float));
return _matrix;
}
float * create_array(float * _array, const size_t _size) {
_array = (float *)calloc(_size, sizeof(float));
return _array;
}
float search_min_element(float * _array, size_t _size) {
size_t n;
float min = _array[0];
for (n = 1; n < _size; n++) if (_array[n] < min) min = _array[n];
return min;
}
void destroy_matrix(float ** _matrix, size_t _rows) {
size_t row;
for (row = 0; row < _rows; row++) free(_matrix[row]);
free(_matrix);
}
void destroy_array(float * _array) { free(_array); }
Другие языки программирования и технологии
Нужна помощь в Си++
Похожие вопросы
- Нужна помощь)) Есть условие задачи для создания программы на СИ++
- Си работа со строками. Нужна помощь
- Нужна помощь в понимание выражения return в Си на данном примере
- Нужна помощь. На языке Си написать программку.
- оч срочно нужна помощь!!!!надо написать программу на си для задачи
- С#(си шарп) нужна помощь! Пока что чайник, но не совсем!)
- Нужна помощь в решении данной задачи на Си.
- Нужна помощь по информатике.
- Нужна помощь с паскалем!
- Программисты,нужна помощь,пожалуйста Для вас легко