Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,955 questions

51,897 answers

573 users

How to split an array and add the first part to end in C

1 Answer

0 votes
#include <stdio.h>

void reverse(int arr[], int start, int end) {
  int temp;

  for (int i = start, j = end; i <= end && j > i; i++, j--) {
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;

  }
}

void print(int arr[],int size) {
    for (int i = 0; i < size; ++i) {
        printf("%2d", arr[i]);
    }
    printf("\n");
}

void split(int arr[], int size, int split_point) {
  if (size <= 1 && split_point < 1 && split_point >= size) {
    return;
  }
  
  // reverse first part
  reverse(arr, 0, split_point - 1);

  // reverse second part
  reverse(arr, split_point, size - 1);

  // reverse all array 
  reverse(arr, 0, size - 1);

}

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

  int size = sizeof(arr)/sizeof(arr[0]);
  int split_point = 3;

  split(arr, size, split_point); 
  
  print(arr, size);
  
  return 0;
}




/*
run:

 4 5 6 7 8 9 0 1 2 3

*/

 



answered Nov 29, 2021 by avibootz
edited Nov 29, 2021 by avibootz

Related questions

1 answer 159 views
1 answer 225 views
1 answer 214 views
1 answer 179 views
1 answer 200 views
...