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


Please Help Members By Posting Answers For Below Questions

How do you list a file’s date and time?

638


Explain how do you use a pointer to a function?

642


An organised method of depicting the use of an area of computer memory used to signify the uses for different parts of the memory a) swap b) extended memory c) memory map d) all of the above

710


Where we use clrscr in c?

714


Explain what are its uses in c programming?

600






What does c mean in standard form?

606


Why doesnt long int work?

617


What does %c mean in c?

658


What are reserved words?

662


What is uint8 in c?

650


How can this be legal c?

658


Write an algorithm for implementing insertion and deletion operations in a singly linked list using arrays ?

3062


Explain what is meant by 'bit masking'?

650


What are the advantages of using new operator as compared to the function malloc ()?

762


How many bytes is a struct in c?

732