#include <stdio.h> #include <malloc.h> #define N 16 int value(int a[], int i, int j) { if (i >= j) return a[(i + (i + 1) )/ 2 + j]; else return a[(j * (j + 1)) / 2 + i]; } void madd(int a[], int b[], int c[][N]) { int i, j; for (i = 0; i < N; i++) for (j = 0; j < N; j++) c[i][j] = value(a, i, j) + value(b, i, j); } void disp1(int a[]) { int i, j; for (i = 0; i < N; i++) { for (j = 0; j < N; j++) printf("%4d", value(a, i, j)); printf("\n"); } } void disp2(int c[][N]) { int i, j; for (i = 0; i < N; i++) { for (j = 0; j <N; j++) printf("%4d", c[i][j]); printf("\n"); } } int main() { int a[16] = { 1,1,2,4,1,2,3,5,2,3,4,6,4,5,6,7 }; int b[16] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }; int c[N][N]; madd(a, b, c); printf("a矩阵:"); disp1(a); printf("b矩阵:"); disp1(b); printf("a+b"); disp2(c); return 1; }改bug
时间: 2024-03-17 20:46:26 浏览: 18
在value函数中,当计算a数组下标时,应该使用i和j的值来计算,而不是使用i和(j+1)的值来计算。修改后的value函数应该如下所示:
```
int value(int a[], int i, int j) {
if (i >= j)
return a[(i + j*(j+1)/2)]; //修正此处的数组下标计算
else
return a[(j * (j + 1)) / 2 + i];
}
```
此外,在disp1函数中,应该调用value函数来输出a数组的值。修改后的disp1函数应该如下所示:
```
void disp1(int a[]) {
int i, j;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
printf("%4d", value(a, i, j)); //修正此处的输出方式
printf("\n");
}
}
```
修正后的程序如下:
```
#include <stdio.h>
#include <malloc.h>
#define N 16
int value(int a[], int i, int j) {
if (i >= j)
return a[(i + j*(j+1)/2)];
else
return a[(j * (j + 1)) / 2 + i];
}
void madd(int a[], int b[], int c[][N]) {
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
c[i][j] = value(a, i, j) + value(b, i, j);
}
void disp1(int a[]) {
int i, j;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
printf("%4d", value(a, i, j));
printf("\n");
}
}
void disp2(int c[][N]) {
int i, j;
for (i = 0; i < N; i++)
{
for (j = 0; j <N; j++)
printf("%4d", c[i][j]);
printf("\n");
}
}
int main() {
int a[16] = { 1,1,2,4,1,2,3,5,2,3,4,6,4,5,6,7 };
int b[16] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };
int c[N][N];
madd(a, b, c);
printf("a矩阵:");
disp1(a);
printf("b矩阵:");
disp1(b);
printf("a+b");
disp2(c);
return 1;
}
```
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)