我必须编写一个程序,要求用户使用 C 语言提供 2 个 3x3 的数组,然后打印包含每个位置总和的 sum_matrix。
我还必须将它分成 3 个函数,我已经做到了,但它似乎充满了错误,我无法真正让它工作。
PS:righe
表示线条,colonne
列,inserisci
插入,somma
总和,stampa
打印。
#include <stdio.h>
#include <stdlib.h>
void leggi_matrice(int MAT[][], int nRighe, int nColonne);
void somma(int MAT1[][N], int MAT2[][], int nRighe, int nColonne);
void stampa_matrice(int MAT[][], int dim, int nRighe, int nColonne);
int main ()
{
int mat_somma[][];
int mat1[][];
int mat2[][];
int nRighe = 3;
int nColonne =3;
leggi_matrice( mat1[][], nRighe, nColonne);
leggi_matrice(mat2[][], nRighe, nColonne);
void somma(mat1[][], mat2[][], nRighe, nColonne);
void stampa_matrice(mat_somma[][], nRighe, nColonne);
printf("\n\n\n");
system("PAUSE");
return 0;
}
void leggi_matrice(int MAT[][], int nRighe, int nColonne)
{
for (i=0 ; i<nRighe ; i ++);
for (j=0 ; j<nColonne ; j ++);
{
printf("Inserisci elemento");
scanf("%d", & MAT[i][j]);
}
}
void somma(int MAT1[][], int MAT2[][], int nRighe, int nColonne);
{
int mat_somma[][];
for (i=0 ; i<nRighe ; i ++);
for (j=0 ; j<nColonne ; j ++);
{
mat_somma[i][j] = MAT1[i][j] + MAT2[i][j];
}
}
void stampa_matrice(int MAT[][], int dim, int nRighe, int nColonne)
{
int mat_somma[][];
for (i=0 ; i<nRighe ; i ++);
for (j=0 ; j<nColonne ; j ++);
{
printf ("%3d",mat_somma[i][j];
}
}
最佳答案
for (i = 0; i < nRighe; i++);
for (j = 0; j < nColonne; j++);
{
printf ("%3d", mat_somma[i][j];
}
- 您将使用
;
结束for
循环,并且在本地范围内,这些{}
,您将获得未声明的变量,这些变量正在使用:i
和j
。 - 您需要在
for
语句中定义i
和j
的数据类型。 - 正如 Paul Griffiths 提到的,
printf()
函数末尾缺少一个括号。
我认为你的意思是这样做:
for (int i = 0; i < nRighe; i++)
{
for (int j = 0; j < nColonne; j++)
{
printf ("%3d", mat_somma[i][j]);
}
}
或者简写版本:
for (int i = 0; i < nRighe; i++)
for (int j = 0; j < nColonne; j++)
printf ("%3d", mat_somma[i][j]);
关于c - C程序中出现多个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19453940/