C程序中的多个错误

时间:2013-10-18 15:57:17

标签: c

我必须制作一个程序,要求用户在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];
     }
}

3 个答案:

答案 0 :(得分:4)

for (i = 0; i < nRighe; i++);    
for (j = 0; j < nColonne; j++);
{
    printf ("%3d", mat_somma[i][j];
}
  • 您正在使用for结束;循环,在本地范围内,这些{},您将获得正在使用的未声明变量:ij
  • 您需要在i声明中定义jfor的数据类型。
  • 正如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]);

答案 1 :(得分:3)

在C中,必须声明数组的大小:

<强>为

int mat_somma[][];   
int mat1[][];
int mat2[][];

不可

int mat_somma[3][3];   
int mat1[3][3];
int mat2[3][3];

制作接收数组的函数时,必须指定数组的最内层维度。 在您的情况下,我建议指定数组的两个维度:

<强>为

void leggi_matrice(int MAT[][], int nRighe, int nColonne)

不可

void leggi_matrice(int MAT[3][3], int nRighe, int nColonne)

使用数组调用函数时,请勿使用括号:

<强>为

leggi_matrice( mat1[][], nRighe, nColonne);

不可

leggi_matrice( mat1, nRighe, nColonne);

调用没有返回值的函数时,请执行 NOT void放在其前面:

<强>为

void somma(mat1, mat2, nRighe, nColonne);

不可

somma(mat1, mat2, nRighe, nColonne);

在使用变量之前声明它们:

<强>为

void leggi_matrice(int MAT[3][3], int nRighe, int nColonne)
{
    for (i=0 ; i<nRighe ; i ++)
    {

不可

void leggi_matrice(int MAT[3][3], int nRighe, int nColonne)
{
    int i;
    int j;
    for (i=0 ; i<nRighe ; i ++)
    {

在完成所有这些(以及其他一些琐碎的错误)后,您的代码应该是:

#include <stdio.h>
#include <stdlib.h>

void leggi_matrice(int MAT[3][3], int nRighe, int nColonne);
void somma(int MAT1[3][3], int MAT2[3][3], int nRighe, int nColonne);
void stampa_matrice(int MAT[3][3], int nRighe, int nColonne);

int main ()
{
    int mat_somma[3][3];
    int mat1[3][3];
    int mat2[3][3];
    int nRighe = 3;
    int nColonne =3;

    leggi_matrice( mat1, nRighe, nColonne);
    leggi_matrice(mat2, nRighe, nColonne);
    somma(mat1, mat2, nRighe, nColonne);
    stampa_matrice(mat_somma, nRighe, nColonne);

    printf("\n\n\n");
    system("PAUSE");
    return 0;
}


void leggi_matrice(int MAT[3][3], int nRighe, int nColonne)
{
    int i;
    int j;
    for (i=0 ; i<nRighe ; i ++)
    {
        for (j=0 ; j<nColonne ; j ++)
        {
            printf("Inserisci elemento");
            scanf("%d", & MAT[i][j]);
        }
   }
}


void somma(int MAT1[3][3], int MAT2[3][3], int nRighe, int nColonne)
{
    int i;
    int j;
     int mat_somma[3][3];
     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[3][3], int nRighe, int nColonne)
{
     int mat_somma[3][3];
     int i;
     int j;
     for (i=0 ; i<nRighe ; i ++)
     {
        for (j=0 ; j<nColonne ; j ++)
        {
            printf ("%3d",mat_somma[i][j]);
        }
     }
}

NitPickers的注意事项 : 通过长时间的拉伸,这不是完美的C.但是我不会在指针,风格,结构等方面向他讲课。这有助于学生沿着这条道路前进;这不是目的地。

答案 2 :(得分:2)

您在;循环后随处可添加for

for (i=0 ; i<nRighe ; i ++);

错了。像这样改变它。

for (i=0 ; i<nRighe ; i ++)

由于此错误,您无法读入值并输出它们。

此外,在尝试使用它们之前,请先定义ij。它会给你一个编译错误。试着理解它的含义。

如果你使用的是C99,你可以在循环中声明它们:

for (int i=0 ; i<nRighe ; i ++)

否则在函数顶部声明它们。