2017-06-17 137 views
0

我在第一行有3個整數,前兩個是我的矩陣的N和M行和colomns,以及X是我必須放大矩陣的次數。 在接下來的N行我有M個元素,更確切地說是一個矩陣,我必須「放大」。增加一個矩陣的大小

這項工作是由四個for陳述完成的,但我不知道該怎麼做。

輸入

2 2 3 
1 2 
3 4 

輸出

1 1 1 2 2 2 
1 1 1 2 2 2 
1 1 1 2 2 2 
3 3 3 4 4 4 
3 3 3 4 4 4 
3 3 3 4 4 4 

輸出與它的大小給出的矩陣增加了十

我的代碼

void ZoomIn(int n, int m, int x, int a[][100], int aux[][100]) 
{ 
    int i, j, ind_i, I, J, ind_j; 


    if (x == 0) 
     return ; 
     I = J = 0; 
    for(i = 0; i < n; i++) 
     { 
     for(j = 0; j < n; j++) 
      { 

       for(ind_i = J; ind_i < x; ind_i++) 
       for(ind_j = I; ind_j < x; ind_j++) 
        aux[ind_i][ind_j] = a[i][j]; // the first element in the smallest matrix 
      I = I + x; 
      } 
} 

如何正確營養不良的人口增加e IJ的值,所以我可以創建較小的子矩陣? 我看到了過程的方式是, 我要創建

1 1 1 
1 1 1 
1 1 1 

,並與下一個小矩陣

2 2 2 
2 2 2 
2 2 2 
+0

現在好多了。使用返回類型'void'並且沒有傳遞指針來保存結果,當函數返回時,您在此函數中創建的任何矩陣將不再存在。這是不對的。 –

+0

您可以利用整數除法來完成兩個循環,例如'for(i = 0; i user3386109

+0

我現在修復了矩陣問題,並將其作爲參數傳遞給它,問題是我必須保存該矩陣中的元素 –

回答

2

這是迄今爲止最簡單的,如果你可以使用C99(或C11)的繼續可變長度數組。只要確保數組對於堆棧不太大,就可以在main()中直接分配局部變量,然後將數組傳遞給ZoomIn()進行處理。

你建議你應該使用四個嵌套循環。這當然有效。外部嵌套循環對迭代基底(未放入)數組中的元素;內部的一對嵌套循環將基本數組的當前元素複製到縮放數組的相關子部分。

此代碼使用在stderr.h中聲明的功能,並在stderr.c中定義 - 可從https://github.com/jleffler/soq/tree/master/src/libsoq獲得的代碼。這些功能極大地簡化了錯誤報告。

#include <stdio.h> 
#include <string.h> 
#include "stderr.h" 

static void dump_array(const char *tag, int n, int m, int array[n][m]) 
{ 
    printf("%s (%dx%d):\n", tag, n, m); 
    for (int i = 0; i < n; i++) 
    { 
     const char *pad = ""; 
     for (int j = 0; j < m; j++) 
     { 
      printf("%s%d", pad, array[i][j]); 
      pad = " "; 
     } 
     putchar('\n'); 
    } 
} 

static void ZoomIn(int r, int c, int base[r][c], int z, int zoom[z * r][z * c]) 
{ 
    for (int i = 0; i < r; i++) 
    { 
     for (int j = 0; j < c; j++) 
     { 
      for (int k = z * i; k < z * (i + 1); k++) 
      { 
       for (int l = z * j; l < z * (j + 1); l++) 
        zoom[k][l] = base[i][j]; 
      } 
     } 
    } 
} 

int main(int argc, char **argv) 
{ 
    err_setarg0(argv[0]); 
    if (argc > 1) 
     err_usage(""); 

    char buffer[4096]; 

    if (fgets(buffer, sizeof(buffer), stdin) == 0) 
     err_error("Unexpected EOF\n"); 
    int r, c, z; 
    buffer[strcspn(buffer, "\n")] = '\0'; 
    if (sscanf(buffer, "%d%d%d", &r, &c, &z) != 3) 
     err_error("Expected 3 numbers on first line, not [%s]\n", buffer); 
    if (r <= 0 || r > 100 || c <= 0 || c > 100 || z <= 0 || z > 100) 
     err_error("Matrix size out of control (r = %d, c = %d, z = %d)\n", r, c, z); 
    if (r * c * z * z > 1000000) 
     err_error("Zoomed matrix too big (r = %d, c = %d, z = %d)\n", r, c, z); 

    int base[r][c]; 
    for (int i = 0; i < r; i++) 
    { 
     if (fgets(buffer, sizeof(buffer), stdin) == 0) 
      err_error("Unexpected EOF 2\n"); 
     buffer[strcspn(buffer, "\n")] = '\0'; 
     int offset = 0; 
     for (int j = 0; j < c; j++) 
     { 
      int p; 
      if (sscanf(buffer + offset, "%d%n", &base[i][j], &p) != 1) 
       err_error("Format error on line [%s]\n", buffer); 
      offset += p; 
     } 
    } 
    dump_array("Base Array", r, c, base); 

    int zoom[r*z][c*z]; 

    ZoomIn(r, c, base, z, zoom); 

    dump_array("Zoomed Array", r * z, c * z, zoom); 

    return 0; 
} 

鑑於數據文件:

2 2 3 
1 2 
3 4 

輸出爲:

Base Array (2x2): 
1 2 
3 4 
Zoomed Array (6x6): 
1 1 1 2 2 2 
1 1 1 2 2 2 
1 1 1 2 2 2 
3 3 3 4 4 4 
3 3 3 4 4 4 
3 3 3 4 4 4 

鑑於數據文件:

4 5 6 
1 3 5 7 9 
2 4 6 8 0 
0 1 2 3 4 
5 6 7 8 9 

輸出爲:

Base Array (4x5): 
1 3 5 7 9 
2 4 6 8 0 
0 1 2 3 4 
5 6 7 8 9 
Zoomed Array (24x30): 
1 1 1 1 1 1 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 9 9 9 9 9 9 
1 1 1 1 1 1 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 9 9 9 9 9 9 
1 1 1 1 1 1 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 9 9 9 9 9 9 
1 1 1 1 1 1 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 9 9 9 9 9 9 
1 1 1 1 1 1 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 9 9 9 9 9 9 
1 1 1 1 1 1 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 9 9 9 9 9 9 
2 2 2 2 2 2 4 4 4 4 4 4 6 6 6 6 6 6 8 8 8 8 8 8 0 0 0 0 0 0 
2 2 2 2 2 2 4 4 4 4 4 4 6 6 6 6 6 6 8 8 8 8 8 8 0 0 0 0 0 0 
2 2 2 2 2 2 4 4 4 4 4 4 6 6 6 6 6 6 8 8 8 8 8 8 0 0 0 0 0 0 
2 2 2 2 2 2 4 4 4 4 4 4 6 6 6 6 6 6 8 8 8 8 8 8 0 0 0 0 0 0 
2 2 2 2 2 2 4 4 4 4 4 4 6 6 6 6 6 6 8 8 8 8 8 8 0 0 0 0 0 0 
2 2 2 2 2 2 4 4 4 4 4 4 6 6 6 6 6 6 8 8 8 8 8 8 0 0 0 0 0 0 
0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 
0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 
0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 
0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 
0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 
0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 

你也可以寫只用2個循環變焦代碼,在每次迭代兩個部門的費用:

static void ZoomIn(int r, int c, int base[r][c], int z, int zoom[z * r][z * c]) 
{ 
    for (int k = 0; k < z * r; k++) 
    { 
     for (int l = 0; l < z * c; l++) 
      zoom[k][l] = base[k/z][l/z]; 
    } 
} 

這將產生相同的輸出作爲四嵌套循環版本一樣。儘管代碼更簡潔,但並不清楚它會比四重嵌套循環版本更快。

+0

謝謝!現在我明白我的問題了。 –