2013-10-18 21 views
-4

我必須製作一個程序,該程序必須向用戶提供C中3個3的數組,然後在每個位置打印包含總和的sum_matrix。C程序中的多個錯誤

我還必須將它分成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]; 
    } 
} 
+1

有什麼錯誤? –

+1

這是否編譯?我沒有看到我和j的定義。 –

+4

**從不**說你的程序有錯誤而沒有發佈錯誤在這裏。 – abelenky

回答

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); 

當調用沒有返回值的函數,做在它前面放一個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通過長時間的拉伸。但我不會以指針,風格,結構等來講授他。這有助於沿途的學生;這不是目的地。

+1

嘿Abelenky!非常感謝你的迴應,瞭解一切!只是有一些東西,即使你的貼吧不打印包含總和的最後一個二維數組的最後一個代碼,爲什麼不將它打印?它提供了隨機數:/ –

+0

你能告訴我,爲什麼不將它打印理論上應該是內部mat_somma值是多少? –

+0

因爲在'外輪山()',你在本地陣列中存儲的值和在該函數結束丟棄它們,然後在'stampa_matrice()'您從本地陣列打印出的值,你」我從未初始化過。你永遠不更新'mat_somma [] []'在'主()'中'外輪山的功能()',所以你傳遞一個未初始化數組'stampa_matrice()',然後繼續,甚至不使用它 - 你傳遞'MAT',但是你打印出一個名爲'mat_somma'的本地未初始化數組。如果你意外地得到隨機數字,那麼未初始化的變量幾乎總是會受到責備。 –

2

你是你for循環後到處添加;

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

是錯誤的。像這樣改變它。

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

由於此錯誤你不能在值和輸出他們閱讀。

另外,在嘗試使用它們之前,定義ij。它會給你一個編譯錯誤。試着理解它的含義。

如果您正在使用C99你可以宣佈他們這樣的循環中:

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

在你的函數的頂部,否則申報。

4
for (i = 0; i < nRighe; i++);  
for (j = 0; j < nColonne; j++); 
{ 
    printf ("%3d", mat_somma[i][j]; 
} 
  • 你結束了for環帶;並在局部範圍內,這些{},你會得到正在使用的未聲明的變量:ij
  • 您需要在for語句中定義ij的數據類型。
  • 正如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]); 
+0

不要忘記printf()調用時丟失的paren。 –

+1

@PaulGriffiths Hawkeyes,謝謝提及! – Houssni