简体   繁体   中英

How do I merge two arrays into a singular array in an alternating order in C?

I think I'm close to being able to complete this program, but I'm not entirely sure how to continue.

Basically, I have three arrays. Array 3 is empty, and Array 1 and Array 2 have values that are up to the users discretion. I want to merge Array 1 and Array 2 into Array 3 in such a way that they're alternating between even and odd positions in Array 3.

So, for example:

Array 1 = [ 1,2,3,4,5 ]

Array 2 = [ 10,20,30,40,50 ]

And Array 3 = [0,0,0,0,0,0,0,0,0,0]

I want the end result to look like so:

Array 3 = [ 1 , 10 , 2 , 20 , 3 , 30 , 4 , 40 , 5 , 50 ]

That's what I mean when I say I want Array 1 to fill odd values , and Array 2 to fill even values .

This is what the relevant piece of code I have so far looks like:

void EvenOdd(int n, int *ap1, int *ap2, int *ap3) {
  // "n" is set to 5.
  ap1 = arr1;
  ap2 = arr2;
  ap3 = arr3;
  int i;
  int j;
  int k;
  for (i = 0; i < n * 2; i++) {
    for (j = 0; j < n; j++) {
      if ((i + 1) % 2 != 0)
        ap3[i] = ap1[j];
    }
    for (k = 0; k < n; k++) {
      if ((i + 1) % 2 == 0)
        ap3[i] = ap2[k];
    }
  }
}

arr1, arr2, and arr3 are all global arrays.

Every time I run the program, it only assigns the last values of Array 1 and Array 2 to the positions in Array 3, like so:

Array 1 = [ 1,2,3,4,5 ]

Array 2 = [ 6,7,8,9,10 ]

And Array 3 = [ 5 , 10 , 5 , 10 , 5 , 10 , 5 , 10 , 5 , 10 ]

This all suggests to me that the two for loops inside of the first one keep running for "i" until they reach the end of their array every time, which is why the last values are consistently assigned, but I'm not sure how to fix this to have the intended result.

I would go for something like this

void EvenOdd(int n, int*ap1, int*ap2, int*ap3){
// "n" is set to 5.
    ap1=arr1;
    ap2=arr2;
    ap3=arr3;
    int i;
    for(int i = 0; i < 2 * n; i++)
    {
        if(i%2)
            {
                *ap3= *ap2;
                ap1++;
            }
        else
            {
                *ap3= *ap1;
                ap2++;
            }
        ap3++;
    }
}

Problem:

That's what I mean when I say I want Array 1 to fill odd values, and Array 2 to fill even values

Actually, in the case of indices, array1 fills the even indices and array2 fills the odd indices. Luckily, your code works in this regard because you check the parity of i + 1 , and not i .

Every time I run the program, it only assigns the last values of Array 1 and Array 2 to the positions in Array 3, like so:

The i-loop is responsible to fill the destination array. But you have two other loops nested inside it, which rewrite ap[i] over and over with the value in even and odd indices of ap1 and ap2 . The last value that remains in ap[i] is when the j-loop and k-loop ends. That is why, you see repeated values filled inside ap3 from the end of ap1 and ap2 .

Solution:

You simply need a single i-loop that fills the destination array after checking the parity of i .

for (int i = 0; i < 2 * n; ++i)
    ap3[i] = i % 2 ? ap2[i / 2] : ap1[i / 2];

An other way would be

#include <stdio.h>

void EvenOdd(int n, int *ap1, int *ap2, int *ap3) {
  for (int i = 0; i < 2 * n; i++)
    if(i % 2 == 0)
      ap3[i] = ap1[i / 2];
    else
      ap3[i] = ap2[i / 2];
}

int main() {
  int arr1[5] = {1, 2, 3, 4, 5};
  int arr2[5] = {10, 20, 30, 40, 50};

  int arr[10];

  EvenOdd(5, arr1, arr2, arr);

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

  return 0;
}

Output

1 10 2 20 3 30 4 40 5 50 

You could do something like this:

int ap1[5] = {1, 2, 3, 4, 5};
int ap2[5] = {10, 20, 30, 40, 50};
int ap3[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

int j = 0;
int k = 0;
for (int i = 0; i < 10; i++) {
    if (i % 2 != 0) { // if i is odd
        ap3[i] = ap2[j++]; // set ap3 to jth value in ap2; j becomes j+1
    } else { // if i is even
        ap3[i] = ap1[k++]; // likewise, but ap1 and k becomes k+1
    }
}

for (int i = 0; i < 10; i++) {
    printf("%d ", ap3[i]);
}

Output is 1 10 2 20 3 30 4 40 5 50 .

Your issue is that the nested loops are unnecessary and infact rewriting values when you don't want them to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM