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}
Answers were Sorted based on User's Feedback
good morning sir,
in ur above program , u said u took then length as 8 and
then allocated ur memory using DMA... but ur way of
allocation find to be wrong sir.... as you allocated
int *ptr=(int*)malloc(sizeof(8));
the above statement will allocate only 2 bytes for u....
since you have given 8 inside sizeof operator.. this will
tell the compiler allocate 2 bytes of memory .. ur
instruction must be :
int *ptr=(int*)malloc(8*sizeof(int));
so, then it will allocate 8*2 bytes of memory sir.....
so only in my program i have given n*sizeof(int) , where
'n' can be any value that user gives........
thank u
| Is This Answer Correct ? | 5 Yes | 0 No |
Answer / rizwan
This program will work perfectly. I hope this is the exact
answer to the question.
#include<stdio.h>
void swap(int *a, int *b)
{
int temp;
temp = *b;
*b=*a;
*a=temp;
}
int main()
{
int a[]={0,0,1,0,1,1,0,0};
int i,j;
for(i=0;i<8;i++)
{
if(a[i])
{
j=i+1;
while(j<8)
{
j++;
if(!a[j])
{
swap(&a[i],&a[j]);
break;
}
}
}
}
for(i=0;i<8;i++)
printf("%d ",a[i]);
return;
}
| Is This Answer Correct ? | 6 Yes | 1 No |
#include<stdio.h>
#include<conio.h>
void main()
{
int *pointer,*pointer2,n;
printf("enter the no. of elements:");
scanf("%d",&n);
pointer=(int*)malloc(n*sizeof(n));
pointer2=(int*)malloc(n*sizeof(n));
for(int k=0,i=0,j=n-1;k<n;k++)
{
scanf("%d",(pointer+k));
if(*(pointer+k))
{
*(pointer2+(j))=*(pointer+k);
j--;
}
else
{
*(pointer2+i)=*(pointer+k);
i++;
}
}
for(i=0;i<n;i++)
printf("%d ",*(pointer2+i));
getch();
}
thaank u
| Is This Answer Correct ? | 6 Yes | 2 No |
Answer / 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 |
Answer / alex r.
Single parse, n/2 steps.
Swapping without 3rd tmp variable.
#include <stdio.h>
int main(void)
{
int i,j;
int stepscount = 0;
int length = 10;
int a[10] = {1,0,0,0,1,0,0,1};
for (i=0,j=length-1;i<j;stepscount++)
{
// dont need move 0 from the start
if (a[i] == 0) i++;
// dont need move 1 from the end
if (a[j] == 1) j--;
// swap 0 and 1
if (a[i] > a[j]) {
a[i] = a[i] ^ a[j];
a[j] = a[j] ^ a[i];
a[i] = a[i] ^ a[j];
i++;
j--;
}
}
for (i = 0; i < length; i++)
{
printf("%d",a[i]);
}
printf("\nSteps: %d\n", stepscount);
return 0;
}
| Is This Answer Correct ? | 4 Yes | 1 No |
Answer / hemavathi
in java this works jus fine:
public static void singlePass(int[] arr){
System.out.println("Orignal Array : " +
Arrays.toString(arr));
int first1index = -1;
for(int i=0; i<arr.length; i++) {
if(arr [i] == 1 && first1index == -1) {
first1index = i;
}
else if(arr [i] == 0 && first1index != -1) {
arr[i] = 1; arr[first1index] = 0;
first1index++;
}
}
System.out.println("Modified Array : " +
Arrays.toString(arr));
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / anon
import java.util.Arrays;
public class Exps {
public static void array_0s_1_seprator(int[] arr){
System.out.println("Orignal Array : " + Arrays.toString(arr));
for(int i = 0, j =arr.length ; i< j ;++i ){
if(arr[i]==0) continue;
while(arr[--j]==1 && i<j)
continue;
if(i< j){
arr[i] = 0;
arr[j] = 1;
}
System.out.println("Modified Array : " + Arrays.toString(arr));
}
}
public static void main(String[] args) {
int arr[] = new int[15];
for(int i =0; i<arr.length;++i)
arr[i] = (int)(Math.random()*10) <5 ? 0 : 1;
array_0s_1_seprator(arr);
}
}
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / nitin garg
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
int num[100],num1[100],n,n1,i,i1=1;
printf("how many elements you enter
");
scanf("%d",&n);
n1=n;
printf("Enter %d elements
",n);
for(i=1;i<=n;i++)
{
scanf("%d",&num[i]);
}
for(i=1;i<=n;i++)
{
if(num[i]==0)
{
num1[i1]=num[i];
i1++;
}
else
{
num1[n1]=num[i];
n1--;
}
}
printf("
Seprate zeor one
");
for(i=1;i<=n;i++)
{
printf("%d ",num1[i]);
}
getch();
}
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / 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 |
Answer / ashok kannan
#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={0,0,1,0,1,1,0,0};
int t,j=0,k=0;
while(a[j]!='\0')
{
if(a[j]==1)
j++;
if(a[k]==0)
k++;
t=i;
a[i]=a[j];
a[j]=a[t];
printf("%d",a[j]);
}
}
| Is This Answer Correct ? | 1 Yes | 3 No |
Did c have any year 2000 problems?
What is a far pointer in c?
Does sprintf put null character?
Explain the difference between malloc() and calloc() in c?
What is pass by value in c?
What is static identifier?
What is the purpose of clrscr () printf () and getch ()?
how to find the kth smallest element in the given list of array elemnts.
Write a program that takes a 5 digit number and calculates 2 power that number and prints it.
why 'c' is called middle level language.
What are header files and explain what are its uses in c programming?
main() { int x=5; printf("%d %d %d\n",x,x<<2,x>>2); }
11 Answers CISOC, CitiGroup, College School Exams Tests,