K
K
kate2018-07-04 01:33:16
C++ / C#
kate, 2018-07-04 01:33:16

How to write 2 ordered arrays into one common?

We are given 2 arrays
mass1[n]
mass2[m] as input
in the process, we fill them with numbers, but this is not so important.
how to write the resulting array consisting of these two?
for example:
mass1[4] = {2, 4, 5, 6}
mass2[6] = {2, 4, 5, 6}
result[10] = {2, 4, 5, 6, 2, 4, 5, 6}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wundarshular, 2018-07-04
@Wundarshular

You know that the length of the resulting array is n+m. Based on this, in the most primitive way, you can fill the resulting array like this:

#include <stdio.h>

int main(void)
{
  short int n,m,i,k;
  short int mass1[n],mass2[m],massR[n+m];

  n=4;m=5;
  k=0; // для заполнения результирующего массива вторым.
  for(i=0;i<n;i++)
    mass1[i]=i;
  for(i=0;i<m;i++)
    mass2[i]=(i+5)*2;
  printf("mass1\n", mass1[i]);
  for(i=0;i<n;i++)
    printf("%d ", mass1[i]);
  printf("\nmass2\n", mass1[i]);
  for(i=0;i<m;i++)
    printf("%d ", mass2[i]);
  printf("\n");

  for(i=0;i<n;i++) // заполнение результирующего массива первым
    massR[i]=mass1[i];
  for(i=n;i<m+n;i++) // и вторым
  {
    massR[i]=mass2[k];
    k++;
  }
  printf("\nResult: ");
  for(i=0;i<n+m;i++)
    printf("%d ", massR[i]);

  getchar();
  return 0;
}

The output looks like this:
mass1
0 1 2 3
mass2
10 12 14 16 18
Result: 0 1 2 3 10 12 14 16 18

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question