x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 209

Simple array conversion

Hi,

I have 3 2-d arrays and tried to convert them into 1-d array. The original arrays are a,b and c and the 1d arrays are a2,b2 and c2. My code segment:

k=0;
for (i=0, i<MAXI; i++)
for (j=0; j<MAXY; j++)
{
a2[k++]=a[i][j];
b2[k++]=b[i][j];
c2[k++]=c[i][j];
}

The output was incorrect and I can't figure out what causes the error. Please advise.
Thanks.
0
peiyoke
1 Solution

Commented:
In what way was the output incorrect?

Did you want a ++ on all of those [k]s?
0

Commented:
Hello !

You are incrementing k after every assignment, That is why you are o/p is incorrect.

It should be like this
k=0;
for (i=0, i<MAXI; i++)
for (j=0; j<MAXY; j++)
{
a2[k]=a[i][j];
b2[k]=b[i][j];
c2[k]=c[i][j];
k++;
}

OR

It can be like this
k=0;
for (i=0, i<MAXI; i++)
for (j=0; j<MAXY; j++)
{
a2[k]=a[i][j];
b2[k]=b[i][j];
c2[k++]=c[i][j];
}
0

Commented:
You know that all those 2D arrays are really 1D arrays already. They are just in column order instead of row order. Do you really need to convert them, even though it is very simple to do.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.