編寫程序,將二維數組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]); } }