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 difference between structure and union in c programming?
What are qualifiers and modifiers c?
What is the benefit of using const for declaring constants?
What are the different types of control structures?
Devise a program that inputs a 3 digit number n and finds out whether the number is prime or not. Find out its factors.
Explain what are bus errors, memory faults, and core dumps?
What is the use of printf() and scanf() functions?
Can math operations be performed on a void pointer?
what is recursion in C
How can I call system when parameters (filenames, etc.) Of the executed command arent known until run time?
What is static volatile in c?
How can I use a preprocessorif expression to ?
Write a C++ program to generate 10 integer numbers between - 1000 and 1000, then store the summation of the odd positive numbers in variable call it sum_pos, then find the maximum digit in this variable regardless of its digits length.
Are global variables static in c?
C language questions for civil engineering