使用c++完成(1) 从键盘输入两个矩阵,并输出结果矩阵。矩阵的输入输出要求行列对齐。 (2) 把矩阵相乘写成函数形式,在主函数中完成矩阵的输入输出。 (3) 对下标越界作出异常处理。
时间: 2023-05-30 19:01:23 浏览: 71
注:由于机器学习等领域需要使用大量矩阵计算,本题仅考察基本算法实现,不考虑使用库函数。
1. 完成从键盘输入两个矩阵,并输出结果矩阵。矩阵的输入输出要求行列对齐。
```c
#include <stdio.h>
#define MAX_ROW 10
#define MAX_COL 10
int main() {
int a[MAX_ROW][MAX_COL], b[MAX_ROW][MAX_COL], c[MAX_ROW][MAX_COL];
int m, n, p, q;
printf("Input the dimensions of matrix A (m, n): ");
scanf("%d, %d", &m, &n);
printf("Input the elements of matrix A:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Input the dimensions of matrix B (p, q): ");
scanf("%d, %d", &p, &q);
printf("Input the elements of matrix B:\n");
for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
scanf("%d", &b[i][j]);
}
}
if (n != p) {
printf("Error: the number of columns in A must equal the number of rows in B.\n");
return 1;
}
printf("A:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%5d ", a[i][j]);
}
printf("\n");
}
printf("B:\n");
for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
printf("%5d ", b[i][j]);
}
printf("\n");
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < q; j++) {
int sum = 0;
for (int k = 0; k < n; k++) {
sum += a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}
printf("C = A * B:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < q; j++) {
printf("%5d ", c[i][j]);
}
printf("\n");
}
return 0;
}
```
2. 把矩阵相乘写成函数形式,在主函数中完成矩阵的输入输出。
```c
#include <stdio.h>
#define MAX_ROW 10
#define MAX_COL 10
void matrix_multiply(int a[][MAX_COL], int b[][MAX_COL], int c[][MAX_COL], int m, int n, int q) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < q; j++) {
int sum = 0;
for (int k = 0; k < n; k++) {
sum += a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}
}
int main() {
int a[MAX_ROW][MAX_COL], b[MAX_ROW][MAX_COL], c[MAX_ROW][MAX_COL];
int m, n, p, q;
printf("Input the dimensions of matrix A (m, n): ");
scanf("%d, %d", &m, &n);
printf("Input the elements of matrix A:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Input the dimensions of matrix B (p, q): ");
scanf("%d, %d", &p, &q);
printf("Input the elements of matrix B:\n");
for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
scanf("%d", &b[i][j]);
}
}
if (n != p) {
printf("Error: the number of columns in A must equal the number of rows in B.\n");
return 1;
}
printf("A:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%5d ", a[i][j]);
}
printf("\n");
}
printf("B:\n");
for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
printf("%5d ", b[i][j]);
}
printf("\n");
}
matrix_multiply(a, b, c, m, n, q);
printf("C = A * B:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < q; j++) {
printf("%5d ", c[i][j]);
}
printf("\n");
}
return 0;
}
```
3. 对下标越界作出异常处理。
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_ROW 10
#define MAX_COL 10
void matrix_multiply(int a[][MAX_COL], int b[][MAX_COL], int c[][MAX_COL], int m, int n, int q) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < q; j++) {
int sum = 0;
for (int k = 0; k < n; k++) {
sum += a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}
}
int main() {
int a[MAX_ROW][MAX_COL], b[MAX_ROW][MAX_COL], c[MAX_ROW][MAX_COL];
int m, n, p, q;
printf("Input the dimensions of matrix A (m, n): ");
scanf("%d, %d", &m, &n);
printf("Input the elements of matrix A:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (scanf("%d", &a[i][j]) != 1) {
printf("Error: invalid input.\n");
return 1;
}
}
}
printf("Input the dimensions of matrix B (p, q): ");
scanf("%d, %d", &p, &q);
printf("Input the elements of matrix B:\n");
for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
if (scanf("%d", &b[i][j]) != 1) {
printf("Error: invalid input.\n");
return 1;
}
}
}
if (n != p) {
printf("Error: the number of columns in A must equal the number of rows in B.\n");
return 1;
}
printf("A:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%5d ", a[i][j]);
}
printf("\n");
}
printf("B:\n");
for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
printf("%5d ", b[i][j]);
}
printf("\n");
}
matrix_multiply(a, b, c, m, n, q);
printf("C = A * B:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < q; j++) {
printf("%5d ", c[i][j]);
}
printf("\n");
}
return 0;
}
```
阅读全文