In this blog post, we learn how to write a C program to double the first element and move zero to end for a given array? Here we are supposing that for a given array of n integers ‘0’ is an invalid number and all others as a valid number.
We need to convert the array in such a way that if both the current and next element is valid and same then double the current value and replace the next number with 0. After the modification, rearrange the array such that all 0’s shifted to the end.
Example,
Input array : int arr[] = {2, 2, 0, 4, 0, 8}; Output array: 4 4 8 0 0 0 Input array: int arr[] = {0, 2, 2, 2, 0, 6, 6, 0, 0, 8}; Output array: 4 2 12 8 0 0 0 0 0 0
Algorithm for double the first element and move zero to the end:
So let’s see the logic to double the first element and move zero to end for a given array. Suppose arr is a given input integer array of size N (arr[N] ).
1. Traverse the array from 0 to n-1(inclusively).
2. Check the current and next values using the if condition. If arr[i] is not equal to ‘0’ and (arr[i]==arr[i+1]), where arr[i] is current value.
- If the condition is true, then make the current value twice of the self.
- Update next element as 0 and do i++.
3. After converting the array you need to shift all zero to the end. We have already written an article on “how to shift all zero at the end”, you can check. Read the article.
4. In the last now print the array.
If you want to learn more about the C language, you can check this course, Free Trial Available.
C program to double the first element and move zero to the end
#include <stdio.h> //Calculate array size #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0]) //swap function not handle null pointer scenario //not much safe to use void swap(int *s1, int *s2) { int temp = *s1; *s1 = *s2; *s2 = temp; } // Function to move all zeros present in the array to the end void moveAllZeroesAtArrayEnd(int arr[], int n) { int i =0,j = 0; // each time we encounter a non-zero, j is incremented and // the element is placed before the pivot for (i = 0; i < n; i++) { if (arr[i] != 0)//non-zero { swap(&arr[i],&arr[j]); j++; } } } // function to rearrange the array elements // after modification int arrayModification(int arr[], int n) { int i =0; const int status = (n>1)? 0: -1; // traverse the array for (i = 0; i < n - 1; i++) { // if current element valid and equal to next if ((arr[i] != 0) && (arr[i] == arr[i + 1])) { // double current index value arr[i] = 2 * arr[i]; // put 0 in the next index arr[i + 1] = 0; // increment by 1 so as to move two // indexes ahead during loop iteration i++; } } //call function when input array is valid if(status ==0) { //move all the zeros at the end of the array moveAllZeroesAtArrayEnd(arr, n); } return status; } int main() { int arr[] = { 0, 2, 2, 2, 0, 6, 6, 0, 0, 8 }; int i = 0; //get array size int arr_size = ARRAY_SIZE(arr); //modify and rearrange the array const int status = arrayModification(arr, arr_size); if(status == 0) { //print array element for (i = 0; i < arr_size; i++) { printf("%d ",arr[i]); } } else { printf("Enter valid array !"); } return 0; }
Recommended Articles for you:
- Best gift for programmers.
- Best electronic kits for programmers.
- C program to find the Median of two sorted arrays of different sizes.
- C Program to find first and last position of element in sorted array
- Write C program to find the missing number in a given integer array of 1 to n
- C program to find the most popular element in an array
- Find the largest and smallest element in an array using C programming.
- C program to find even occurring elements in an array of limited range
- Find sum of all sub-array of a given array.
- C program to segregate even and odd numbers
- Find an element in array such that sum of left array is equal to sum of right array.
- C Program to find the count of even and odd elements in the array.
- Write C program to find the sum of array elements.
- C program to find odd occurring elements in an array of limited range
- Find the sum of array elements using recursion
- C Program to reverse the elements of an array
- C Program to find the maximum and minimum element in the array
- Calculate size of an array in without using sizeof in C
- How to create a dynamic array in C?