将二维数组转换为一维数组
Transfering a 2-dimensional array into a single dimension array
我正在尝试用 C 编写一个程序,将二维数组(特别是矩阵)转换为一维数组。例如,如果我们有一个包含 L 行和 C 列的矩阵,它应该变成单行 newL=L*C。因此,如果矩阵有 3 行和 4 列,则新数组的大小为 3*4=12。
The matrix should turn to this:
1 2
--> 1 2 3 4
3 4
我现在面临的问题是如何在没有随机值或重复值的情况下将矩阵分配给数组。
The piece of code I'm concerned with, goes like this:
for(k=1;k<=t;k++)
{
for(i=1;i<=l;i++)
{
for(j=1;j<=c;j++)
{
v[k]=m[i][j];
}
}
}
k,i 和 j 是矩阵(二维数组)和数组的计数器。其中两个; i 和 j 是矩阵的计数器,k 是数组的计数器。请注意,它们中的每一个都从 1 开始并达到其大小,在这个大小中,我将使用 2 行和 2 列作为矩阵,因此数组的大小为 4(2*2)。
- l 是数组中的行数。
- c 是数组中的列数。
- t是数组的大小。 t=l*c
Executing the code gives me this as a return:
1 2
--> 4 4 4 4
3 4
Simply said, the piece of code will ALWAYS give the last value of the matrix to the array. So if I replace 4 with 5 in the matrix, the array will have 5 5 5 5.
编辑:
这里是完整的代码,用于理解我正在尝试做的事情:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c,i,j,l,k,t;
printf("Donner le nombres des lignes: ");
scanf("%d",&l);
printf("Donner le nombres des colonnes: ");
scanf("%d",&c);
int m[l][c];
t=l*c;
int v[t];
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("Donner m[%d][%d]: ",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
printf("\n\n\n\n");
for(k=1;k<=t;k++)
{
for(i=1;i<=l;i++)
{
for(j=1;j<=c;j++)
{
v[k]=m[i][j];
}
}
}
for(k=0;k<t;k++)
{
printf("%d\t",v[k]);
}
system("pause");
}
谢谢大家的帮助,我找到了正确的方法。
这个:
for(k=1;k<=t;k++)
for(i=1;i<=l;i++)
for(j=1;j<=c;j++)
v[k]=m[i][j];
没有按照你的想法去做。想想当你第一次循环遍历 j
部分时,你将一直设置 v 的所有 0th
元素,最后你设置的最后一个值将保留(即位置 1, 1
恰好是 4
)。然后你将 k
递增到 1
并再次重复,导致所有 4's
。你想要这个:
for(i = 0; i < l; i++)
for(j = 0; j < c; j++)
v[i*l+j] = m[i][j]; // i*l + j gives you the equivelent position in a 1D vector.
确保您的 v
矢量大小正确,即。 int v[l*c];
。还要记住,在 c
中,零索引是 used.If 你确实需要基于 1 的索引(你不需要......)然后这样做:
int k = 1;
for(i = 1; i <= l; i++)
for(j = 1; j <= c; j++)
v[k++]=m[i][j];
但请记住,这将对该向量进行任何进一步操作 Gross。所以不要这样做....
- 你不需要外循环
- 数组索引在 C 中是从零开始的
因此,我们有:
for(k = 0, i = 0; i < o; i++)
{
for(j = 0; j < p; j++)
{
v[k++] = m[i][j];
}
}
其中 o
和 p
- 矩阵的维度 m
只要新数组的大小正确,像下面这样的东西就应该起作用:
k=0;
for(i=0;i<l;i++){
for(j=0;j<c;j++){
v[k]=m[i][j];
k++;
}
}
本质上,您正在遍历矩阵(您的行和列 - 正如您所说的那样),同时增加您希望放置该值的新数组中的位置 (k)。
如果您只想将矩阵元素作为一维数组访问,您可以声明一个 int
指针 v
:
int m[3][4];
int *v = (int*)m;
// then access for example m[1][1] as v[5]
或者,要实际复制数组,请使用双 for
(如其他答案),单个 for
,如下所示
int vv[12];
for(i = 0; i < 12; i++)
vv[i] = m[i/4][i%4];
或者只使用 memcpy
:
memcpy(vv, m, 12*sizeof(int));
如果我们有这样一个多维数组:
int nums[3][3];
我们有:
int all[9];
我们有:
int a, b;
我们将像这样引用每个数字:
nums[a][b];
现在想想 a 和 b 的实际值是什么:
for (a = 0; a < 3; a++) {
for (b = 0; b < 3; b++)
all[((a * 3) + b)] = nums[a][b];
}
只要将 a 乘以它将迭代的元素数,这就会起作用:
int nums[5][5];
int all[25];
int a, b;
for (a = 0; a < 5; a++) {
for (b = 0; b < 5; b++)
all[((a * 5) + b)] = nums[a][b];
}
你提到你的问题是 "how to I fix the code?" 我想很多人都给了你正确的答案。这是您的代码以及更正后的代码。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c,i,j,l,k,t;
printf("Donner le nombres des lignes: ");
scanf("%d",&l);
printf("Donner le nombres des colonnes: ");
scanf("%d",&c);
int m[l][c];
t=l*c;
int v[t];
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("Donner m[%d][%d]: ",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
printf("\n\n\n\n");
/* corrected code below */
k = 0;
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
v[k]=m[i][j];
k++;
}
}
/* corrected code above */
for(k=0;k<t;k++)
{
printf("%d\t",v[k]);
}
system("pause");
}
我正在尝试用 C 编写一个程序,将二维数组(特别是矩阵)转换为一维数组。例如,如果我们有一个包含 L 行和 C 列的矩阵,它应该变成单行 newL=L*C。因此,如果矩阵有 3 行和 4 列,则新数组的大小为 3*4=12。
The matrix should turn to this:
1 2
--> 1 2 3 4
3 4
我现在面临的问题是如何在没有随机值或重复值的情况下将矩阵分配给数组。
The piece of code I'm concerned with, goes like this:
for(k=1;k<=t;k++)
{
for(i=1;i<=l;i++)
{
for(j=1;j<=c;j++)
{
v[k]=m[i][j];
}
}
}
k,i 和 j 是矩阵(二维数组)和数组的计数器。其中两个; i 和 j 是矩阵的计数器,k 是数组的计数器。请注意,它们中的每一个都从 1 开始并达到其大小,在这个大小中,我将使用 2 行和 2 列作为矩阵,因此数组的大小为 4(2*2)。
- l 是数组中的行数。
- c 是数组中的列数。
- t是数组的大小。 t=l*c
Executing the code gives me this as a return:
1 2
--> 4 4 4 4
3 4
Simply said, the piece of code will ALWAYS give the last value of the matrix to the array. So if I replace 4 with 5 in the matrix, the array will have 5 5 5 5.
编辑:
这里是完整的代码,用于理解我正在尝试做的事情:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c,i,j,l,k,t;
printf("Donner le nombres des lignes: ");
scanf("%d",&l);
printf("Donner le nombres des colonnes: ");
scanf("%d",&c);
int m[l][c];
t=l*c;
int v[t];
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("Donner m[%d][%d]: ",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
printf("\n\n\n\n");
for(k=1;k<=t;k++)
{
for(i=1;i<=l;i++)
{
for(j=1;j<=c;j++)
{
v[k]=m[i][j];
}
}
}
for(k=0;k<t;k++)
{
printf("%d\t",v[k]);
}
system("pause");
}
谢谢大家的帮助,我找到了正确的方法。
这个:
for(k=1;k<=t;k++)
for(i=1;i<=l;i++)
for(j=1;j<=c;j++)
v[k]=m[i][j];
没有按照你的想法去做。想想当你第一次循环遍历 j
部分时,你将一直设置 v 的所有 0th
元素,最后你设置的最后一个值将保留(即位置 1, 1
恰好是 4
)。然后你将 k
递增到 1
并再次重复,导致所有 4's
。你想要这个:
for(i = 0; i < l; i++)
for(j = 0; j < c; j++)
v[i*l+j] = m[i][j]; // i*l + j gives you the equivelent position in a 1D vector.
确保您的 v
矢量大小正确,即。 int v[l*c];
。还要记住,在 c
中,零索引是 used.If 你确实需要基于 1 的索引(你不需要......)然后这样做:
int k = 1;
for(i = 1; i <= l; i++)
for(j = 1; j <= c; j++)
v[k++]=m[i][j];
但请记住,这将对该向量进行任何进一步操作 Gross。所以不要这样做....
- 你不需要外循环
- 数组索引在 C 中是从零开始的
因此,我们有:
for(k = 0, i = 0; i < o; i++)
{
for(j = 0; j < p; j++)
{
v[k++] = m[i][j];
}
}
其中 o
和 p
- 矩阵的维度 m
只要新数组的大小正确,像下面这样的东西就应该起作用:
k=0;
for(i=0;i<l;i++){
for(j=0;j<c;j++){
v[k]=m[i][j];
k++;
}
}
本质上,您正在遍历矩阵(您的行和列 - 正如您所说的那样),同时增加您希望放置该值的新数组中的位置 (k)。
如果您只想将矩阵元素作为一维数组访问,您可以声明一个 int
指针 v
:
int m[3][4];
int *v = (int*)m;
// then access for example m[1][1] as v[5]
或者,要实际复制数组,请使用双 for
(如其他答案),单个 for
,如下所示
int vv[12];
for(i = 0; i < 12; i++)
vv[i] = m[i/4][i%4];
或者只使用 memcpy
:
memcpy(vv, m, 12*sizeof(int));
如果我们有这样一个多维数组:
int nums[3][3];
我们有:
int all[9];
我们有:
int a, b;
我们将像这样引用每个数字:
nums[a][b];
现在想想 a 和 b 的实际值是什么:
for (a = 0; a < 3; a++) {
for (b = 0; b < 3; b++)
all[((a * 3) + b)] = nums[a][b];
}
只要将 a 乘以它将迭代的元素数,这就会起作用:
int nums[5][5];
int all[25];
int a, b;
for (a = 0; a < 5; a++) {
for (b = 0; b < 5; b++)
all[((a * 5) + b)] = nums[a][b];
}
你提到你的问题是 "how to I fix the code?" 我想很多人都给了你正确的答案。这是您的代码以及更正后的代码。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c,i,j,l,k,t;
printf("Donner le nombres des lignes: ");
scanf("%d",&l);
printf("Donner le nombres des colonnes: ");
scanf("%d",&c);
int m[l][c];
t=l*c;
int v[t];
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("Donner m[%d][%d]: ",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
printf("\n\n\n\n");
/* corrected code below */
k = 0;
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
v[k]=m[i][j];
k++;
}
}
/* corrected code above */
for(k=0;k<t;k++)
{
printf("%d\t",v[k]);
}
system("pause");
}