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 / rajasekaran

#include <stdio.h>

int main() {
int a[8] = {1,0,1,0,1,0,0,1};
int i = 0,j=0;
int sorted = 1;
for(i=0;i<8;i++) {
if (a[i]) continue;
/* Find the nearest one and swap */
for(j=i+1;j<8;j++) {
if (a[j]) {
a[j] = a[i] + a[j];
a[i] = a[j] - a[i];
a[j] = a[j] - a[i];
sorted = 0;
break;
}
}
if (sorted) { break;}
}
printf("\nSorted Array is { ");
for (i=0;i<8;i++) { printf("%d,",a[i]); }
printf("}\n");
}

Is This Answer Correct ?    0 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is function prototype in c with example?

570


application attempts to perform an operation?

1489


What is the purpose of the preprocessor directive error?

674


Can a function be forced to be inline? Also, give a comparison between inline function and the C macro?

656


Given a valid 24 hour format time find the combination of the value and write a program ,do not hard the value and if any other inputs provided should work with the logic implemented Input: 11:30 Output: 13:10 Input: 18:25 Output: 21:58

1115






Explain what does the characters 'r' and 'w' mean when writing programs that will make use of files?

749


What are header files in c programming?

651


What are the different types of errors?

635


How to explain the final year project as a fresher please answer with sample project

463


What are comments and how do you insert it in a C program?

737


How can I find out the size of a file, prior to reading it in?

616


what is a constant pointer in C

674


What is a scope resolution operator in c?

745


Compare and contrast compilers from interpreters.

679


What is an expression?

652