要求用户在控制台输入整数“n”。您的程序将在0到1023(含)之间生成两个n x n个随机整数矩阵,然后将它们相乘,相加和相减。矩阵将表示为二维阵列。 您的程序应显示每个矩阵运算的结果(乘法,减法和加法)。
(对于减法部分,我是否需要第三个空矩阵来表示?)没有得到它。
以下是我的代码。运行不顺利。我的数组乘法似乎是正确的,然而,在我点击编译并运行后,它只是在用户输入输入后陷入困境。这是怎么用C编写矩阵的? 谢谢,
#include <stdlib.h>
#include <stdio.h>>
int main() {
int number;
//ask for interger
printf("\nEnter and interger: ");
scanf("%d", &number);
int Array1[number][number];
int Array2[number][number];
int Array3[number][number];
int i,j, r;
//populate array
for(i = 0; i < number; i++ ) {
for(i = 0; j < number; j++ ) {
r = rand() % 1023;
Array1[i][j] = r;
Array2[i][j] = r;
}
}
//calutate array
//multiplication
for(i = 0; i < number; i++ ) {
for(i = 0; j < number; j++ ) {
Array3[i][j] =
Array1[i][j] * Array2[i][j] + Array1[i][j+1] * Array2[i+1][j];
}
}
return 0;
}
答案 0 :(得分:0)
使用VLA简化内存管理时,至少应检查用户输入的大小是否有效。
您的代码中也存在一些错误,但从数学角度来看,最糟糕的是您如何计算两个矩阵的乘积。 考虑 C = AB ,正确的公式为:
C i,j = Σ k A i,k B k ,J 子> 的
尝试这段代码,我评论并修复了我发现的问题:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
// Declarations of functions which calculate the matrix product and print out
// the results. I assume square matrices stored as variable length arrays
void matrix_product( int width, int a[][width], int b[][width], int c[][width] );
void print_array( char *msg, int width, int arr[][width] );
int main() {
int number = 0, rs;
//ask for an integer
printf("\nEnter and interger: ");
// It's good practice to check the return value of scanf
while ( (rs = scanf("%d", &number)) != 1 || number < 1 ) {
if ( rs == EOF ) {
printf("\nFatal Error: No input\n");
return EXIT_FAILURE;
}
printf("\nPlease, enter a positive integer: ");
// clear input buffer reading whatever left on the line till a newline
// "[^\n]", without storing it anywhere "*"
scanf("%*[^\n]");
}
int Array1[number][number];
int Array2[number][number];
int Array3[number][number];
int i, j, r;
// populate array
srand(time(NULL));
for( i = 0; i < number; i++ ) {
for( j = 0; j < number; j++ ) {
// ^^^ it was i in your code
r = rand() % 1024;
// note that 1024 % 1024 = 0 while 1023 % 1024 = 1023
Array1[i][j] = r;
// I guess that the two matrices are intended to be different
r = rand() % 1024;
Array2[i][j] = r;
}
}
// print the original arrays
print_array("\nArray 1:", number, Array1);
print_array("\nArray 2:", number, Array2);
// print the result of calculations
matrix_product(number, Array1, Array2, Array3);
print_array("\nArray 3, result of multiplication:", number, Array3);
return EXIT_SUCCESS;
}
// print out the elements of a square matrix stored as a 2D VLA
// preceded by an informative string
void print_array( char *msg, int width, int arr[][width] ) {
int i, j;
printf("%s\n", msg);
for( i = 0; i < width; i++ ) {
for( j = 0; j < width; j++ ) {
printf("%8d", arr[i][j]);
}
printf("\n");
}
}
// Calculate the matrix product c = a * b assuming square matrices
// stored as 2D VLA
void matrix_product( int width, int a[][width], int b[][width], int c[][width] ) {
int i, j, k, sum;
for( i = 0; i < width; ++i ) {
for( j = 0; j < width; ++j ) {
sum = 0;
for ( k = 0; k < width; ++k ) {
sum += a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}
}