array contains zeros and ones as elements.we need to bring
zeros one side and one other side in single parse.
ex:a[]={0,0,1,0,1,1,0,0}
o/p={0,0,0,0,0,1,1,1}
Answer Posted / asdf
/* Here is a solution for the above problem using
* pointers.Please add on your comments.
*/
#include<stdio.h>
void swap(int *x,int *y);
void array_shift(int *a,int n);
int main() {
/* A sample array of 8 elements taken */
int a[] = {0,0,1,0,1,1,0,0};
int i;
printf("Before shift array\n");
/* Number of elements in the array is 8 */
for (i = 0;i < 8;++i) {
printf("%d\t",a[i]);
}
/* Number of elements in the array is 8 */
array_shift(a,8);
printf("\nafter shift array\n");
for (i = 0;i < 8;++i) {
printf("%d\t",a[i]);
}
return 0;
}
void swap(int *x,int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void array_shift(int *a,int n) {
int *ptr_beg = &a[0];
int *ptr_end = &a[n-1];
while(ptr_beg <= ptr_end) {
if(*ptr_beg) {
if(!(*ptr_end)) {
swap(ptr_beg,ptr_end);
} else {
ptr_end--;
}
} else {
ptr_beg++;
}
}
}
| Is This Answer Correct ? | 3 Yes | 0 No |
Post New Answer View All Answers
what do you mean by enumeration constant?
What is the difference between #include
the number of measuring units from a arbitrary starting point in a record area or control block to some other point a) branching b) recording pointer c) none d) offset
What does %d do?
How do c compilers work?
write a program to copy the string using switch case?
What is typedef?
How will you write a code for accessing the length of an array without assigning it to another variable?
which is an algorithm for sorting in a growing Lexicographic order
Explain how can I avoid the abort, retry, fail messages?
Why cant I open a file by its explicit path?
Write a program in c to replace any vowel in a string with z?
What is the code for 3 questions and answer check in VisualBasic.Net?
Which is better between malloc and calloc?
How is a pointer variable declared?