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

51,831 answers

573 users

How to move one-dimensional int array elements to left and delete one element in the process in C

3 Answers

0 votes
#include <stdio.h>

#define LEN 7

int main(void)
{
	int arr[LEN] = { 2, 234, 1, 800, 100, 9, 12237 };
	int i, idx;

	idx = 3; // from arr[3] = 800;
	
	while (idx < LEN) 
	{
		arr[idx - 1] = arr[idx];
		idx++;
	}
	
	// Print all elements 
	for (i = 0; i < LEN; i++) 
		printf("arr[%d] = %d\n", i, arr[i]);

    return 0;
}
  
    
/*
      
run:

arr[0] = 2
arr[1] = 234
arr[2] = 800
arr[3] = 100
arr[4] = 9
arr[5] = 12237
arr[6] = 12237

*/

 



answered Jan 31, 2016 by avibootz
edited Feb 2, 2016 by avibootz
0 votes
#include <stdio.h>
 
int main(void)
{
    int arr[] = { 2, 234, 1, 800, 100, 9, 12237 };
    int i, idx, arr_size;
 
     
    idx = 0; // from arr[0] = 2; (all array)
    arr_size = sizeof(arr)/sizeof(arr[0]);
     
    while (idx < arr_size) 
    {
        arr[idx - 1] = arr[idx];
        idx++;
    }
     
    //arr[arr_size - 1] = 0;
     
     
    // Print all elements 
    for (i = 0; i < arr_size; i++) 
        printf("arr[%d] = %d\n", i, arr[i]);
 
    return 0;
}
   
     
/*
       
run:
 
arr[0] = 234
arr[1] = 1
arr[2] = 800
arr[3] = 100
arr[4] = 9
arr[5] = 12237
arr[6] = 12237

*/

 



answered Jan 31, 2016 by avibootz
edited Feb 2, 2016 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

int main(void)
{
	int arr[] = { 2, 234, 1, 800, 100, 9, 12237 };
	int i, arr_size;
	
	arr_size = sizeof(arr)/sizeof(arr[0]);
	
	memmove(&arr[0], &arr[1], arr_size * sizeof(int)); 
	
	// Print all elements 
	for (i = 0; i < arr_size; i++) 
		printf("arr[%d] = %d\n", i, arr[i]);

    return 0;
}
  
    
/*
      
run:

arr[0] = 234
arr[1] = 1
arr[2] = 800
arr[3] = 100
arr[4] = 9
arr[5] = 12237
arr[6] = 0

*/

 



answered Jan 31, 2016 by avibootz
edited Feb 2, 2016 by avibootz
...