一维转二维:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(){
int a[8] = { 1, 2, 3, 4, 5,6,7,8};
int b[4][2]={0};//要转换成的二维数组
int i=0;//行数
int j=0;//列数
for (int m=0;m<8;m++)//m为一维数组的总长度
{
i=m/2; //除数为要转换的二维的数组的列数
j=m%2;
b[i][j]=a[m];//赋值
}
//随机取数验证
cout<<b[2][0];
system("pause");
return 0;
}
二维转一维:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(){
int a[2][3] = { {1,2,3},{4,5,6} };
int cnt=0;
int c[6];
for(int k = 0; k < 2; k++)//转换的二维数组行数
{
for(int l = 0; l<3; l++)//转换的二维数组列数
{
c[cnt]=a[k][l];
cnt++;
}
}
printf("zhuanhuanz:%5d", c[2]);
system("pause");
return 0;
}
二维行和列互换
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(){
int i, j, a[2][3] = { {1,2,3},{4,5,6} },b[3][2];
for (i = 0; i <2; i++)//处理a数组中的一行中各元素
{
for (j = 0; j <3; j++)//处理a数组中的某一列元素
{
b[j][i] = a[i][j];
}
}
system("pause");
return 0;
}