如何在 C++ 中以特定形式将内容从 1D 数组传输到 2D 数组
how to transfer contents from 1d array to a 2d array in a specific form in c++?
结果必须看起来像
// Eg1: 1d array
b[]={1,2,3}
// 2d array
// 1 0 3
// 0 2 0
// 1 0 3
//Eg2: 1d array
b[]={1,2,3,4}
// 2d array
// 1 0 0 4
// 0 2 3 0
// 0 2 3 0
// 1 0 0 4
// a[] is 1d array contain input, b[][] is 2d array that will contain result, n is size of the array
//set all b[][] content to 0 first
for (int i = 0; i < n;i++)
{
for (int j = 0; j < n; j++) b[i][j] = 0;
}
//this is the process to move content from a[] to b[][]
for (int i = 0; i < n ;i++)
{
b[i][i] = a[i];
b[n-i-1][i] = a[i];
}
让我们调用数组 A,n 的大小为 A 和矩阵 M (n x n)
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
M[i][j] = 0;
}
}
for(int i = 0; i < n; i++){
M[i][i] = M[n-i-1][i] = A[i];
}
这样的东西应该有效
在运行时,C++中没有 2D 数组这样的东西,至少在没有 ADT 的情况下没有。 二维数组仅在编译时存在,在运行时,您必须线性寻址数组,自己映射索引。
相关文章:
- C++使用数组作为多维数组,尽管将其初始化为带有指针的 1D
- 为什么将指针重新分配给 1D 数组的 char 时显示错误,但 2D 数组工作正常?
- 如何使用指针C++将元素从2D数组复制到1D数组
- 借助功能将 2D 转换为 1D 数组
- 使用C++将1D数组转换为2D字符串数组
- C++ 矩阵乘法 2D 数组比 1D 更快
- 使用 std::transform 在 2d C 阵列上转换为 1d C 数组
- 在 C++ 中,用于将大型 2D 数组转换为 1D 数组的内存分配
- 将指向 1D Fortran 数组的指针传递给使用 C_LOC() 进行C++会导致垃圾值
- 表示为 1D 的多维数组(模板化为 n 维)
- 我的算法在 1D 数组中渲染矩形有什么问题
- 如何在C++中将 2d 矢量转换为 1d 数组?
- 在 C++ 中实现 1D 数组中的 2D 数组坐标
- 访问表示 2D 矩阵的 1D 存储数组
- C 将1D动态数组施放到2D数组以使用常规索引(例如ARR [i] [J])
- 如何将元素的索引从2D数组存储到1D数组中,然后交换这些值
- C++ 3D 数组到 1D 会导致堆缓冲区溢出
- 我在将 cv::Mat 保存到 1D 数组时遇到问题
- C ,将1D数组转换为2D数组
- 数组 1D 我需要要求用户检查索引和打印值