作业:数组位置右移一位

编写程序,将二维数组a[N][M]中每个元素向右一移一列,最右一列换到最左一列,移动后的数组存到另外一个二维数组b中,原数组保存不变。例如

1 2 3移动后变成 3 1 2

#include <stdio.h>
#define N 2
#define M 3

void main(void)
{
    int a[N][M] = {{1,2,3},{4,5,6}};
    int b[N][M];
    int col,cell = 0,x = 0;
    for(col = 0 ; col < N ; col++)
    {
        
        b[col][cell++] = a[col][M -1];

        do 
        {
            if(cell+1 == M)
                b[col][M-1] = a[col][M-2];

            b[col][cell]= a[col][x++];            

        } while (cell++ < M);

        x = cell = 0;

    }

    for( col = 0 ; col < N; col++)
        for (cell = 0 ; cell < M; cell++)
        {
            printf("b[%d][%d] = %d\n",col, cell, b[col][cell]);
        }

}

猜你喜欢

转载自www.cnblogs.com/passedbylove/p/9156526.html