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,895 questions

51,826 answers

573 users

How to move element to end of array in C

1 Answer

0 votes
#include <stdio.h>

void ShiftArrayToLeft(int arr[], int size, int start) {
	for (int i = start; i < size - 1; i++) {
		arr[i] = arr[i + 1];
	}
}

void MoveElementToEndOfArray(int arr[], int size, int index) {
	int n = arr[index];

	ShiftArrayToLeft(arr, size, index);

	arr[size - 1] = n;
}

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

int main(void) {
	int arr[] = { 4, 9, 12, 90, 13, 0, 3, 97 };

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

	PrintArray(arr, size);

	int index = 2;

	printf("\nelement value = %d\n", arr[index]);
	
	MoveElementToEndOfArray(arr, size, index);
	
	PrintArray(arr, size);

	return 0;
}



/*
run:
  
4 9 12 90 13 0 3 97
element value = 12
4 9 90 13 0 3 97 12
  
*/

 



answered Nov 22, 2022 by avibootz
edited Nov 23, 2022 by avibootz

Related questions

1 answer 85 views
1 answer 108 views
108 views asked Nov 27, 2021 by avibootz
2 answers 150 views
1 answer 250 views
2 answers 153 views
...