Я сделал эту программу для домашней работы, и она вылетает без ошибок при запуске.многомерные массивы переменной длины
Также после исправления, любые предложения по повышению эффективности моего подхода кодирования приветствуются.
Сначала я объявил m, n, p, q глобальными переменными и передал только массивы функциям, но программа вела себя странно.
Затем я включил размеры массивов в качестве аргументов в каждой функции и объявил их повсюду. CRASH
* VLA ПОДДЕРЖКЕ
//functions on matrices
#include<stdio.h>
int i, j;
void getMatrix(int m, int n, int values[m][n]);
void displayMatrix(int m, int n, int values[m][n]);
void transposeMatrix(int m, int n, int values[m][n]);
void addMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q]);
void multiplyMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q]);
int main()
{
int m, n, p, q, A[m][n], B[p][q];
printf("Enter the no. of Rows of the first Matrix : ");
scanf("%d", &m);
printf("Enter the no. of Columns of the first Matrix : ");
scanf("%d", &n);
printf("Enter the elements of the first matrix: \n");
getMatrix(m, n, A);
printf("The entered Matrix:\n");
displayMatrix(m, n, A);
printf("The transpose of the entered Matrix:\n");
transposeMatrix(m, n, A);
printf("Enter the no. of Rows of the second Matrix : ");
scanf("%d", &p);
printf("Enter the no. of Columns of the second Matrix : ");
scanf("%d", &q);
printf("Enter the elements of the secong matrix: \n");
getMatrix(p, q, B);
printf("The entered Matrix:\n");
displayMatrix(p, q, B);
printf("The transpose of the entered Matrix:\n");
transposeMatrix(p, q, B);
printf("Addition of the Matrices:\n");
addMatrices(m, n, p, q, A, B);
printf("Multiplication of the Matrices:\n");
multiplyMatrices(m, n, p, q, A, B);
return 0;
}
void getMatrix(int m, int n, int values[m][n])
{
for(i = 0; i < m; ++i)
for(j = 0; j < n; ++j)
scanf("%d", &values[i][j]);
}
void displayMatrix(int m, int n, int values[m][n])
{
for(i = 0; i < m; ++i)
{
for(j = 0; j < n; ++j)
printf("%3d ", values[i][j]);
printf("\n");
}
}
void transposeMatrix(int m, int n, int values[m][n])
{
int transpose[n][m];
for(i = 0; i < n; ++i)
for(j =0; j < m; ++j)
transpose[i][j] = values[j][i];
displayMatrix(n, m, transpose);
}
void addMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q])
{
int C[m][n];
if(m == p && n == q)
{
for(i = 0; i < m; ++i)
for(j = 0; j < n; ++j)
C[i][j] = A[i][j] + B[i][j];
displayMatrix(m, n, C);
}
else
printf("Cannot add these Matrices!\n");
}
void multiplyMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q])
{
int C[m][q], k, sum = 0;
if(n == p)
{
for(i = 0; i < m; ++i)
for(j = 0; j < q; ++j)
{
for(k = 0; k < n; ++k)
sum += A[i][j] * B[j][i];
C[i][j] = sum;
sum = 0;
}
displayMatrix(m, q, C);
}
else
printf("Cannot multiply these Matrices!\n");
}
@BLUEPIXY .: Вы так быстро ... но я ответил почти одновременно – coderredoc
Как что исходный код может скомпилировать с на той же строке 'Int т, п, р, д, А [м] [n], B [p] [q]; '? Без предупреждения тоже? –