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
What is function prototype in c with example?
application attempts to perform an operation?
What is the purpose of the preprocessor directive error?
Can a function be forced to be inline? Also, give a comparison between inline function and the C macro?
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
Explain what does the characters 'r' and 'w' mean when writing programs that will make use of files?
What are header files in c programming?
What are the different types of errors?
How to explain the final year project as a fresher please answer with sample project
What are comments and how do you insert it in a C program?
How can I find out the size of a file, prior to reading it in?
what is a constant pointer in C
What is a scope resolution operator in c?
Compare and contrast compilers from interpreters.
What is an expression?