Q:

C program to split an array and add the first half after the second half of the array

0

C program to split an array and add the first half after the second half of the array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Here, we will create an integer array then split the array based on entered position and shift the first half of the array after the second half and print the updated array on the console screen.

Program:

The source code to split an array and add the first half after the second half of the array is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to split an array and add the first half
// after the second half of the array

#include <stdio.h>

int main()
{
    int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    int i = 0;
    int j = 0;
    int p = 0;

    printf("Enter the position of the item to split the array: ");
    scanf("%d", &p);

    //Split the array and shift first half to the end of second half.
    for (int i = 0; i < p; i++) {
        int x = arr[0];
        for (j = 0; j < 9; ++j)
            arr[j] = arr[j + 1];
        arr[9] = x;
    }

    printf("Result is: ");
    for (i = 0; i <= 9; ++i)
        printf("%d ", arr[i]);

    printf("\n");

    return 0;
}

Output:

Enter the position of the item to split the array: 3
Result is: 3 4 5 6 7 8 9 0 1 2

Explanation:

Here, we created an array arr with 10 elements, and then we entered the position of the element to split the array. After that, we split the array and add the first half of the array after the second half of the array, and printed the updated array on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

One Dimensional Array Programs / Examples in C programming language

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to generate pascal triangle using the ar... >>
<< C program to find the sum of the largest contiguou...