Solved

Simple array conversion

Posted on 1998-08-26
3
191 Views
Last Modified: 2010-04-15
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
Comment
Question by:peiyoke
3 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
In what way was the output incorrect?

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

Accepted Solution

by:
sganta earned 10 total points
Comment Utility
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
 
LVL 3

Expert Comment

by:braveheart
Comment Utility
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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now