【问题标题】:"Enlarging" a 2D array by factor (m,n) [closed]按因子(m,n)“放大”二维数组[关闭]
【发布时间】:2018-09-15 05:28:18
【问题描述】:

假设我有一个大小为 h1 x w1 的二维数组
我想将它放大(就像我们放大图像一样)一个因子 m,n
这样我得到的数组 if 的大小为 h1*m , w1*n
例如,我有一个大小为 (2, 2)
的数组 arr1[2][2] 01
11

现在我将它放大了一个因子 (3, 3),所以我的新数组 arr2[6][6] 变为
000111
000111
000111
111111
111111
111111

您能否建议我一个可以根据给定信息生成 arr2 的算法/迭代循环?

【问题讨论】:

  • "我要放大" --> C 中的数组,一旦定义,就不能改变它们的大小。考虑改为为数据分配内存并使用指针。
  • 选择 C ​​或 C++
  • 这对你来说尝试解决自己并不太难。想一想你将如何对 1x1 -> 1x2 执行此操作,然后思考如何对 1x1 -> 2x2、2x2 -> 4x4 执行此操作,然后是可变输入。祝你好运!
  • 如果您自己尝试解决此问题并在必要时根据您的解决方案提出问题,您将获得更好的响应。如果您的尝试有效,但您认为可以改进,请考虑在Code Review 提问。注意我已经链接到帮助页面的询问部分。我强烈建议在发布之前通读一遍。在 Stack Overflow 上阅读 How to Ask 也是个好主意。
  • @GargYashit 在你说你想放大(即改变大小)二维矩阵的问题中。在评论中你说你想制作一个不同大小的新矩阵。那是两种不同的东西。请编辑您的问题以明确您想要做什么。最好是你可以添加一些代码来说明它。

标签: c++ c arrays algorithm


【解决方案1】:

我以一种智能的方式使用指向数组的指针将连续内存索引为二维数组和函数原型中的 VLA 声明符。

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

#define ARRAY_SIZE(x)  (sizeof(x)/sizeof(x[0]))
#define ERR_ON(expr, msg, ...) \
    do{ if(expr) { fprintf(stderr, "%d: error %s failed: " msg "\n", __LINE__, #expr, ##__VA_ARGS__); exit(-1); } }while(0)

/**
 * Input: array p with x columns and y rows
 * Output: int arr[y * factory][x * factorx]
 *          newly allocated array created from p resized by a factorx and factory
 */
void *arr2d_getenlarged(size_t x, size_t y, int p[y][x], size_t factorx, size_t factory)
{
    const size_t newx = x * factorx;
    const size_t newy = y * factory;

    int (* const ret)[newx] = malloc(sizeof(*p) * newx * newy);
    ERR_ON(ret == NULL, "");
    for (size_t i = 0; i < x; ++i) {
        for (size_t j = 0; j < y; ++j) {
            const int val = p[i][j];
            for (size_t m = 0; m < factorx; ++m) {
                for (size_t n = 0; n < factory; ++n) {
                    ret[i * factorx + m][j * factory + n] = val;
                }
            }
        }
    }

    return ret;
}

void arr2d_print(size_t x, size_t y, int (*arr)[x])
{
    printf("--- %p ---\n", (void*)arr);
    for (size_t i = 0; i < x; ++i) {
        for (size_t j = 0; j < y; ++j) {
            printf("[%d,%d]=%d   ", i, j, arr[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    int (*arr)[2] = malloc(sizeof(*arr) * 2);
    ERR_ON(arr == NULL, "");
    memcpy(arr, (int[2][2]){{0,1},{1,1}}, sizeof((int[2][2]){0}));

    arr2d_print(2, 2, arr);

    int (*arr3)[6] = (void*)arr2d_getenlarged(2, 2, &arr[0][0], 3, 3);

    arr2d_print(6, 6, arr3);

    free(arr);
    free(arr3);

    printf("Hello World");

    return 0;
}

示例输出:

--- 0x1203010 ---                                                                                                                                                                                                           
[0,0]=0   [0,1]=1                                                                                                                                                                                                           
[1,0]=1   [1,1]=1                                                                                                                                                                                                           
--- 0x1203030 ---                                                                                                                                                                                                           
[0,0]=0   [0,1]=0   [0,2]=0   [0,3]=1   [0,4]=1   [0,5]=1                                                                                                                                                                   
[1,0]=0   [1,1]=0   [1,2]=0   [1,3]=1   [1,4]=1   [1,5]=1                                                                                                                                                                   
[2,0]=0   [2,1]=0   [2,2]=0   [2,3]=1   [2,4]=1   [2,5]=1                                                                                                                                                                   
[3,0]=1   [3,1]=1   [3,2]=1   [3,3]=1   [3,4]=1   [3,5]=1                                                                                                                                                                   
[4,0]=1   [4,1]=1   [4,2]=1   [4,3]=1   [4,4]=1   [4,5]=1                                                                                                                                                                   
[5,0]=1   [5,1]=1   [5,2]=1   [5,3]=1   [5,4]=1   [5,5]=1                                                                                                                                                                   
Hello World       

onlinegdb 提供实时版本。

【讨论】:

    【解决方案2】:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define ENLARGEX 3
    int src[2][2] = {{0,1},{1,1}};
    
    int main(int argc, const char * argv[]) {
        // insert code here...
        int **dest;
        //int dest[6][6] ={0,};
        int i, j;
        int sizesrc = sizeof(src)/sizeof(int)/2;
    
        dest = (int **)malloc(sizeof(int*)*sizesrc*ENLARGEX);
        for (i = 0; i < sizesrc*ENLARGEX; i++) {
            dest[i] = (int *)malloc(sizeof(int)*sizesrc*ENLARGEX);
        }
    
        for (i = 0; i < sizesrc*ENLARGEX; i++){
            for(j = 0; j < sizesrc*ENLARGEX; j++) {
                dest[i][j] = src[i/ENLARGEX][j/ENLARGEX];
                printf("%d ", dest[i][j]);
            }
            printf("\n");
        }
    
        for (i = 0; i < sizesrc*ENLARGEX; i++) {
            free(dest[i]);
        }
        free(dest);
        return 0;
    }
    

    【讨论】:

    • sizeof (int)不一定和sizeof (int*)一样;即你在第一个malloc() 调用中有一个错误。另外你没有free() 的资源,忘记了#include &lt;stdlib.h&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 2012-04-16
    • 2018-08-18
    • 2011-04-13
    相关资源
    最近更新 更多