C Program to Merge Two Arrays

C Program to Merge Two Arrays

A class stores five marks in their original order: 40, 35, 45, 35 and 50. The concatenate two arrays task needs a clear rule about order, duplicates and how many elements exist.

Concatenation copies one sequence after another into storage large enough for their combined lengths.

Before this exercise, review array elements and bounds and function parameters. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why concatenate two arrays uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Concatenate the other class first, then the original class.

Concatenate Two Arrays: follow the data

  1. Given data

    A class stores five marks in their original order: 40, 35, 45, 35 and 50. The concatenate two arrays task needs a clear rule about order, duplicates and how many elements exist.

  2. Rule

    Concatenation copies one sequence after another into storage large enough for their combined lengths.

  3. Observed result

    40 35 45 35 50 38 42

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

This lesson’s merge means concatenation, not an ordered two-way merge. A sorted merge requires two sorted inputs and a comparison that chooses their next heads.

The complete example knows its five-element bound. In a reusable function, pass an explicit element count with the array pointer; a pointer alone does not report that bound. This sample has two equal marks, which makes duplicate handling observable.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  int other[] = {38, 42};
  int combined[7];
  for (int i = 0; i < count; i++) combined[i] = marks[i];
  for (int i = 0; i < 2; i++) combined[count + i] = other[i];
  for (int i = 0; i < 7; i++) printf("%d%s", combined[i], i == 6 ? "\n" : " ");
  return 0;
}

Expected output

40 35 45 35 50 38 42
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  int other[] = {38, 42};
  int combined[7];
  for (int i = 0; i < count; i++) combined[i] = marks[i];
  for (int i = 0; i < 2; i++) combined[count + i] = other[i];
  for (int i = 0; i < 7; i++) printf("%d%s", combined[i], i == 6 ? "\n" : " ");
  return 0;
}

The first five output positions receive the marks, and the next two receive the other class’s values.

Compare the resulting output with the original sequence. A changed order and a changed set of values are different effects; decide which your operation promises.

Follow the worked calculation

  1. Start with the sample

    The first five output positions receive the marks, and the next two receive the other class’s values.

  2. Apply the rule

    Compare the resulting output with the original sequence. A changed order and a changed set of values are different effects; decide which your operation promises.

  3. Check the result

    40 35 45 35 50 38 42

    40 35 45 35 50 38 42
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  int other[] = {38, 42};
  int combined[7];
  for (int i = 0; i < count; i++) combined[i] = marks[i];
  for (int i = 0; i < 2; i++) combined[count + i] = other[i];
  for (int i = 0; i < 7; i++) printf("%d%s", combined[i], i == 6 ? "\n" : " ");
  return 0;
}

Concatenate the other class first, then the original class.

Worked answer

The complete answer keeps the five valid input elements and applies the stated alternative. Read the output against the original array rather than assume that every loop should sort, remove or overwrite values.

C17 · complete program

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  int other[] = {38, 42};
  int combined[7];
  for (int i = 0; i < 2; i++) combined[i] = other[i];
  for (int i = 0; i < count; i++) combined[2 + i] = marks[i];
  for (int i = 0; i < 7; i++) printf("%d%s", combined[i], i == 6 ? "\n" : " ");
  return 0;
}

Expected output

38 42 40 35 45 35 50
Run in C compiler →

Recognize the important boundaries

Check combined-length arithmetic and destination capacity. Overlapping sources/destinations need a defined copying strategy.

The sample marks and their sums fit int. General input requires both valid array bounds and an arithmetic-range contract.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Why is count passed separately to a reusable array function?
Choose one answer. You can try again.

Answer and explanation

The element pointer does not carry the caller’s element count.

Correct. The separate bound defines which accesses are permitted.

Use the idea independently

Concatenation copies one sequence after another into storage large enough for their combined lengths.

Continue with array indexes and bounds.

Continue with pointer and count contracts.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Find Common Array Elements →