c一维和二维数组相转和二维数组行列互换

一维转二维:

#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;
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值