C++一维数组传送到二维数组

c++要把一维数组inta[n*n]的元素传送到二维数组int b [n][n]中,即在程序中要执行 b[i][j]=a[k]写作kij的下标变换公式即可,若用程序验证了,本人另外加分!

#include <iostream>
using namespace std;

void trap(int *arrayA,int lenthA,int *arrayB,int lengthBx,int lengthBy)
{
int *a=arrayA;
int *b=arrayB;
if (lenthA != lengthBx*lengthBy)
{
cout<<"数组1和数组2元素数目不一样!"<<endl;
return;
}
for (int i=0;i<lengthBx;i++)
{
for (int j=0;j<lengthBy;j++)
{
*(b+i*lengthBy+j) = *(a+i*lengthBy+j);
}
}
}

void main()
{
int a[9] = {1,2,3,5,4,11,65,8,85};
int b[3][3];
trap(a,9,b[0],3,3);
cout<<"AAAAAAAAAA:"<<endl;
for (int k=0;k<9;k++)
{
cout<<a[k]<<endl;
}
cout<<"BBBBBBBBBB:"<<endl;
for (int i =0;i<3;i++)
{
for (int j=0;j<3;j++)
{
cout<<b[i][j]<<endl;
}
}
int te;
cin>>te;
}
温馨提示:答案为网友推荐,仅供参考
相似回答
大家正在搜