1

after reading inputs to an array:

int * inputs;

the "inputs" is 1 dimensional array: inputs[6], then reading this array out, the values are:

inputs[0]=1
inputs[1]=2
inputs[2]=3
inputs[3]=4
inputs[4]=5
inputs[5]=6

I would like to read this array into another one dimensional array:

int counter=0;
int * allElements = new int[6];

for(int i=0; i<6; i++)
{
      allElements[counter++] = inputs[i];
}

That is a traditional way of reading all of the elements into one dimensional array and I believe if I read the elements of "allElements" this way:

for(int i=0; i<6; i++)
   printf("%d ", allElements[i]);

and it should be: 1 2 3 4 5 6

However, I would like to read all elements of that array into the 1 dimensional array such that when I do it like this:

for(int i=0; i<6; i++)
   printf("%d ", allElements[i]);

It should be: 1 3 5 2 4 6

How can I achieve this way?

11
  • You could write a function which calculates the index of allElements from the index of inputs. Commented Nov 14, 2011 at 23:33
  • 3
    I don't think any of this deserves to be called "C++"... why are you using pointers and new? Use the standard library containers for arrays (vector or array). Commented Nov 14, 2011 at 23:34
  • 1
    possible duplicate of read array 2 dimensions in another way C++ Commented Nov 14, 2011 at 23:35
  • @KerrekSB probably, some codes I wrote it in C# way, but I would like to do it in C++ Commented Nov 14, 2011 at 23:37
  • 3
    @JeremyFriesner: "C++" is as much about idiom as it is about language features. Commented Nov 14, 2011 at 23:41

3 Answers 3

3
int * allElements = new int[6];

for(int i=0; i<6; i+=2)
{
    allElements[i/2] = inputs[i];
    allElements[3+i/2] = inputs[i+1];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Might as well unroll it if it is going to be for only 1 case.
2

What about:

for(int i=0; i<6; i++)
{
    allElements[i] = inputs[2*(i%3) + (i/3)];
}

Imagine inputs is a two-dimension array, of 3x2, then i%3 is one coordinate and i/3 the other. Just transpose it into a 2x3 matrix, and done!

1 Comment

@JoeMcGrath Right, I made it backwards. Corrected.
0
const int size =6;
int inputs[size]={1,2,3,4,5,6};
int allElements[size];

...

  if (size % 2)
  {
      int middle=(size/2) +1;
      for(int i=0; i<size; i+=2)
      {   
          allElements[i/2] = inputs[i];
          if (i < size-2)
            allElements[middle+i/2] = inputs[i+1];          
      }      

  }
  else
  {
    int middle=size/2;
    for(int i=0; i<size; i+=2)
    { 
      allElements[i/2] = inputs[i];
      allElements[middle+i/2] = inputs[i+1];
    }      
  }

Logic can definitely be simplified. Just first attempt and left it alone.

If want it to work for the 1 case

allElements[0]=inputs[0];      
allElements[1]=inputs[2];
allElements[2]=inputs[4];
allElements[3]=inputs[1];
allElements[4]=inputs[3];
allElements[5]=inputs[5];      

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.