C语言学习--练习5(二维指针)

本文探讨了C++中的矩阵操作,如图像翻转、转置、重塑以及一维到二维的转换,还包括网格移动和平滑功能。
摘要由CSDN通过智能技术生成

目录

1.翻转图像

2.转置矩阵

3.重塑矩阵

4.一维变二维

5.二维网格迁移

6.图像平滑器


1.翻转图像

//申请一个二维数组
int **myMalloc(int r,int c){
    int **a=(int **)malloc(sizeof(int*)*r);
    for(int i=0;i<r;i++){
        a[i]=(int*)malloc(sizeof(int)*c);
        
    }
    return a;
}
void swap(int*a,int*b){
     int c=*a;
     *a=*b;
     *b=c;
 }
int** flipAndInvertImage(int** image, int imageSize, int* imageColSize, int* returnSize, int** returnColumnSizes) {
    int n = imageSize;
    int m=imageColSize[0];
    int **a=myMalloc(n,m);
    a=image;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m/2; j++){
            swap(&a[i][j], &a[i][n-j-1]);
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            a[i][j] = (a[i][j] + 1) % 2;
        }
    }
    *returnColumnSizes=imageColSize;
    *returnSize = n;
    return a;
}

2.转置矩阵

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
 //申请一个二维数组m*n
int **myArray(int m,int n){
    int** a=(int **)malloc(sizeof(int*)*m);
    for(int i=0;i<m;i++){
        a[i]=(int*)malloc(sizeof(int)*n);
    }
    return a;
}
int** transpose(int** matrix, int matrixSize, int* matrixColSize, int* returnSize, int** returnColumnSizes) {
    int m=matrixSize;
    int n=matrixColSize[0];
    int **a=myArray(n,m);//转置行列交换n*m
    
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            a[i][j]=matrix[j][i];
        }
    }
    * returnSize=n;//返回二维数组的行数
    *returnColumnSizes = (int*)malloc(sizeof(int) * n);
    for (int i = 0; i < n; i++) {
        (*returnColumnSizes)[i] = m; // 返回转置矩阵的列数
    }
    return a;


}

3.重塑矩阵

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
 //思路:1.如果m*n=r*c,表示可以重构
 //2.如果不等于则没法重构
int **myArray(int m,int n){
    int** a=(int **)malloc(sizeof(int*)*m);
    for(int i=0;i<m;i++){
        a[i]=(int*)malloc(sizeof(int)*n);
    }
    return a;
}
int** matrixReshape(int** mat, int matSize, int* matColSize, int r, int c, int* returnSize, int** returnColumnSizes) {
    int m=matSize;
    int n=matColSize[0];
    if(m*n!=r*c){
        * returnSize=m;
        * returnColumnSizes=(int* )malloc(sizeof(int)*m);//要穿一个数组回去
        for(int i=0;i<m;i++){
            (*returnColumnSizes)[i]=n;
        }
        return mat;

    }else{
        int **a=myArray(r,c);//构造r*c的矩阵
        //x=(行)*c+列,列数=x%c,行数=(x-列数)/c+
        for(int i=0;i<m*n;i++){
            a[(i-i%c)/c][i%c]=mat[i/n][i%n];
        }
        * returnSize=r;
        * returnColumnSizes=(int* )malloc(sizeof(int)*r);//要穿一个数组回去
        for(int i=0;i<r;i++){
            (*returnColumnSizes)[i]=c;
        }
        return a;
    }
}

4.一维变二维

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** construct2DArray(int* original, int originalSize, int m, int n, int* returnSize, int** returnColumnSizes) {
    int num=originalSize;
    if(num!=m*n){
        * returnSize=0;
        * returnColumnSizes=0;
        int **a=0;
        return a;
    }
    //构造一个二维数组m*n
    int **a=(int**)malloc(sizeof(int*)*m);
    * returnSize=m;
    * returnColumnSizes=(int*)malloc(sizeof(int)*m);
    //a={{},{}...{}}m个
    for(int i=0;i<m;i++){
        a[i]=(int*)malloc(sizeof(int)*n);
        (*returnColumnSizes)[i]=n;
    }
    //i=行数*n+列数
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            a[i][j]=original[i*n+j];
        }
    }
    return a;

}

5.二维网格迁移



/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
 //就像是一个一维数组向后移了一位
int** shiftGrid(int** grid, int gridSize, int* gridColSize, int k, int* returnSize, int** returnColumnSizes){
    int m = gridSize, n = gridColSize[0];
    int **ret = (int **)malloc(sizeof(int *) * m);
    *returnColumnSizes = (int *)malloc(sizeof(int) * m);
    for (int i = 0; i < m; i++) {
        ret[i] = (int *)malloc(sizeof(int) * n);
        (*returnColumnSizes)[i] = n;
    }//申请二维数组ret
    //x=(行数*n+列数+k)%(m*n)
    //行数=x-列数
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            int index1 = (i * n + j + k) % (m * n);
            ret[index1 / n][index1 % n] = grid[i][j];
        }
    }
    *returnSize = m;
    return ret;
}

6.图像平滑器

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** imageSmoother(int** img, int imgSize, int* imgColSize, int* returnSize, int** returnColumnSizes){
    int m = imgSize, n = imgColSize[0];
    int ** ret = (int **)malloc(sizeof(int *) * m);
    *returnColumnSizes = (int *)malloc(sizeof(int) * m);
    for (int i = 0; i < m; i ++) {
        ret[i] = (int *)malloc(sizeof(int) * n);
        memset(ret[i], 0, sizeof(int) * n);
        (*returnColumnSizes)[i] = n;
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {//遍历所有元素
            int num = 0, sum = 0;
            for (int x = i - 1; x <= i + 1; x++) {
                for (int y = j - 1; y <= j + 1; y++) {//遍历每个元素的左中右
                    if (x >= 0 && x < m && y >= 0 && y < n) {//只有当下标在范围内时候才加
                        num++;
                        sum += img[x][y];
                    }
                }
            }
            ret[i][j] = sum / num;
        }
    }
    *returnSize = m;
    return ret;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西柚小萌新吖(●ˇ∀ˇ●)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值