2017-06-17 53 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 *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 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

谢谢!现在我明白我的问题了。 –