2015-04-17 98 views
2

剛纔我剛剛開始學習C語言。今天,我得到了一個問題,我必須爲2個矩陣(行和列的數量由用戶指定)進行輸入並添加它們。我很容易地做了添加和其他部分。但我正在考慮使它看起來更好的格式。 所以我的想法是: 假設用戶輸入一個3x3大小的矩陣。讓我們假設他選擇以下元素 - >一次打印兩個數組,並且每次只能打印特定數量的字符

Matrix 1-> 
1 2 3 
4 5 6 
7 8 9 

Matrix 2-> 
9 8 7 
6 5 4 
3 2 1 

我要顯示他們原樣>

| 1 2 3  +  9 8 7 | 
| 4 5 6  +  6 5 4 | 
| 7 8 9  +  3 2 1 | 

(不,不是實際添加,只是這種格式,然後在接下來的行我給答案補充)。 但問題是,我無法顯示右側的一半。

我的輸出來如下:

| 1 2 3  +  9 8 7 | 
| 4 5 6  +  | 
| 7 8 9  +  | 

我已經嘗試了很多事情,使剩餘的字符以正確的順序顯示,但有一些或其他類似的問題。到目前爲止,這裏是我的代碼(它不是最近的,我試過把它弄得更糟糕,但是最新的代碼甚至進一步混淆了它,引入了許多我認爲甚至不需要的變量。所以我會發布我的迄今爲止最好的進展)。

printf("| "); 
i = 0; //A global int loop variable defined somewhere 

width2 = width; //Another width variable in case I need it in second array, width is variable for number of elements in a row of array. In above example, width=3 

for (ii = 0; ii < width * height; ii++) { //ii is just like i, another global int loop variable. Height is number of characters in a row (in above example 3) 
    if (ii != 0) { 
     if (ii % width == 0) { 
      printf(" |\n| "); 
     } 
    } 

    printf("%d ", row1[ii]); //For printing out first (left) set of numbers. row1 is where my matrix 1 array values are stored. 

    if (((ii + 1)*(width - 1)) % (width * 2) == 0) { //I think this condition is where things are going wrong. 
     printf("  +  "); 
     for (i; i < width2; i++) { 
      if (i != 0) { 
       if (i % width2 == 0) { 
        printf(" |\n| "); 
       } 
      } 
      printf("%d ", row2[i]); //row2 has second matrix (right) array values 
     } 
    } 
    sec++; //Just another integer variable to have some control over loop process, didnt succeed much though 
} 
printf(" |\n\n"); 

從2天開始就嘗試過,這真的讓我很頭疼。我不介意如果更好的更小的代碼和整個代碼需要被替換(因爲我對C很新)。

回答

3

gcc警告了「聲明沒有效果「,說

for (i; i < width2; i++) 

- 而且,這樣做是對的!只更改第一組ii=0即可顯示第二組數字。 (你太親近了!)

但它只重複顯示前3個,因爲在那裏的i循環僅循環一次,超過width2。它可以哄正確的價值觀在那裏與數學等

printf("%d ", row2[width2*(ii/width) + i]); 

這工作,因爲ii在每個下一行的width步驟增加,並且希望行數倍右手設置寬度。

將整個例程分成三個循環可能更容易:外部只需要在height上運行,而內部第一個打印一行set1數字,然後打印一行set2數字。不過,我會把它作爲練習。

+0

它工作完美!非常感謝:D我看到很多關於將循環分解爲函數的建議,所以肯定會嘗試這樣做:) –

4

保持簡單,對於行有一個外部循環,對於列有兩個內部循環。如果矩陣的大小不同,只需在每個內部循環中添加一個簡單的檢查即可。

類似下面代碼:

size_t max_line_count = MAX(matrix1_line_count, matrix2_line_count); 
for (size_t line = 0; line < max_line_count; ++line) 
{ 
    size_t column; 

    printf(" | "); 

    // First matrix 
    for (column = 0; column < matrix1_column_count; ++column) 
    { 
     if (line < matrix1_line_count) 
      printf("%2d", matrix1[line][column]); 
     else 
      printf(" "); 
    } 

    printf(" + "); 

    // The second matrix 
    for (column = 0; column < matrix2_column_count; ++column) 
    { 
     if (line < matrix2_line_count) 
      printf("%2d", matrix2[line][column]); 
     else 
      printf(" "); 
    } 

    printf(" |\n"); 
} 

該內環可以分解成一個單獨的函數,爲了避免重複代碼。

如果矩陣不是數組的陣列,而是一個大的單個數組,然後使用例如matrix1[line * matrix1_column_count + column]

+1

感謝,但它給我上subcripted那些printf的命令」錯誤值既不是數組也不是指針也不是矢量「。我已經聲明瞭MAX宏,並在C99模式下運行它。仍然給出這個錯誤。如果你能幫我解決這個問題(因爲我還沒有在C中體驗過)?由於會喜歡嘗試新的代碼。 –

3

創建一個單獨的功能打印該行更多的可讀性 看到我的例子:

#include <stdio.h> 


void printRow(int len, int row[]) { 
    int i =0; 
    for (i = 0; i < len; i++) { 
     printf("%d\t", row[i]); 
    } 
} 

int main(void) { 
    int m1[3][3] = {{1,2,3}, {4,5,6}, {7, 8, 9}}; 
    int m2[3][3] = {{7, 8, 9}, {4,5,6}, {1,2,3}}; 
    int i = 0; 
    int width = 3; //3x3 

    for (i = 0; i < width; i++) { 
     printf("|\t"); 
     printRow(width, m1[i]); 
     printf("\t+\t"); 
     printRow(width, m2[i]); 
     printf("\t|\n"); 
    } 
    return 0; 
} 

輸出:

| 1 2 3  + 7 8 9  | 
| 4 5 6  + 4 5 6  | 
| 7 8 9  + 1 2 3  | 

工作實例: http://ideone.com/JfnnAS

1

在你沒有重新初始化i這樣的條件i<width2被失敗的第二個循環,所以初始化在i=0 for循環 更改爲:

if (((ii + 1)*(width - 1)) % (width * 2) == 0) { 
    printf("  +  "); 
    for (i=0; i < width2; i++) {//set i=0 here 
     if (i != 0) { 
      if (i % width2 == 0) { 
       printf(" |\n| "); 
      } 
     } 
     printf("%d ", row2[i]); 
    } 
} 
+0

謝謝,相當不錯的技巧xD,但在第二行(右側)它只是在所有3行。我想這是我的代碼中的一些更多的錯誤(我在上面的回答中得到了更正大聲笑)。謝謝你讓我進步:) –