Рандомное заполнение матрицы размер которой задан с клавиатуры - C (СИ)
Формулировка задачи:
Доброе время суток!
Помогите поправить под рандомное заполнение, программа работает, но в консоли непонятное значение.
Переполнение массива или что то в этом роде.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <clocale>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
int** Multiply(int**, int, int, int**, int, int**);
int** fill(int**, int, int);
void Print(int**, int, int);
int **rand_A, **rand_B;
int main()
{
int str_A, col_A, str_B, col_B;
setlocale(LC_ALL, "RUS");
printf("Размер матрицы A:\n");
printf("Введите число строк = ");
scanf("%d", &str_A);
printf("Введите число колонок = ");
scanf("%d", &col_A);
// выделение памяти под массивы
int **matr_A = (int**)malloc(str_A * sizeof(int*));
for (int i = 0; i < str_A; ++i)
matr_A[i] = (int *)malloc(col_A * sizeof(int));
/*
printf("\nПолученная матрицы A:\n");
fill(matr_A, str_A, col_A);
*/
str_B = col_A;
printf("\nРазмер матрицы B:\n");
printf("Введите число строк = %d\n", str_B);
printf("Введите число колонок = ");
scanf("%d", &col_B);
int **matr_B = (int**)malloc(str_B * sizeof(int*));
for (int i = 0; i < str_B; ++i)
matr_B[i] = (int *)malloc(col_B * sizeof(int));
/*
printf("\nПолученная матрица B:\n");
fill(matr_B, str_B, col_B);
*/
int str_C = str_A, col_C = col_B;
int **matr_C = (int**)malloc(str_C * sizeof(int*));
for (int i = 0; i < str_C; ++i)
matr_C[i] = (int *)malloc(col_C * sizeof(int));
Multiply(matr_A, str_A, col_A, matr_B, col_B, matr_C);
printf("\nМатрица A [%dx%d]:\n", str_A, col_A);
Print(matr_A, str_A, col_A);
printf("\nМатрица B [%dx%d]:\n", str_B, col_B);
Print(matr_B, str_B, col_B);
printf("\nФормула умножения C=A*B [%dx%d]:\n", str_C, col_C);
Print(matr_C, str_C, col_C);
// высвобождение памяти
for (int i = 0; i<str_A; i++)
free(matr_A[i]);
free(matr_A);
for (int i = 0; i<str_B; i++)
free(matr_B[i]);
free(matr_B);
for (int i = 0; i<str_C; i++)
free(matr_C[i]);
free(matr_C);
_getch();
return 0;
}
// вывод матриц на экран
void Print(int **matr, int str, int col)
{
for (int i = 0; i<str; i++)
{
printf("\n");
for (int j = 0; j<col; j++)
printf("%d\t", matr[i][j]);
}
printf("\n");
printf("\n");
}
// функции рандома
void random_A(const int col_B, const int col_A, int **matr_A)
{
for (int i = 0; i < col_B; i++)
for (int j = 0; j < col_A; j++)
matr_A[i][j] = 0 + rand() % 10;
}
void random_B(const int col_B, const int col_A, int **matr_B)
{
for (int i = 0; i < col_B; i++)
for (int j = 0; j < col_A; j++)
matr_B[i][j] = 0 + rand() % 10;
}
// умножение матрицы
int** Multiply(int **matr_A, const int str_A, const int col_A,
int **matr_B, const int col_B, int **matr_C)
{
int sum;
for (int i = 0; i < col_B; i++)
for (int j = 0; j<str_A; j++)
{
sum = 0;
for (int q = 0; q<col_A; q++)
{
sum += matr_A[j][q] * matr_B[q][i];
matr_C[j][i] = sum;
}
}
return matr_C;
}
// ввод элементов
/*
int** fill(int **matr, int str, int col)
{
for (int i = 0; i<str; i++)
for (int j = 0; j<col; j++)
//matr[i][j] = 0 + rand() % 100;
{
printf("Введите элемент [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matr[i][j]);
}
return matr;
}
*/Решение задачи: «Рандомное заполнение матрицы размер которой задан с клавиатуры»
textual
Листинг программы
void random_A(const int str_A, const int col_A, int **matr_A)
{
for (int i = 0; i <str_A; i++)
for (int j = 0; j <col_A; j++)
matr_A[i][j] = 0 + rand() % 10;
}
Объяснение кода листинга программы
- Вводится постановка задачи - рандомное заполнение матрицы.
- Вводные данные: размер строки матрицы (str_A), размер столбца матрицы (col_A), двумерный массив (матрица) matr_A.
- В функции используется два вложенных цикла:
- внешний цикл для прохода по строкам матрицы (0..str_A-1).
- внутренний цикл для прохода по столбцам матрицы (0..col_A-1).
- Внутренний цикл выполняется для каждого элемента матрицы matr_A[i][j].
- Присваивается случайное число от 0 до 9 (включительно) и записывается в элемент матрицы matr_A[i][j].
- Функция не возвращает значение, так как является процедурой (не выполняет каких-либо вычислений, а только заполняет матрицу).