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



array contains zeros and ones as elements.we need to bring zeros one side and one other side in si..

Answer / vignesh1988i

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

array contains zeros and ones as elements.we need to bring zeros one side and one other side in si..

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

array contains zeros and ones as elements.we need to bring zeros one side and one other side in si..

Answer / vignesh1988i

#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

array contains zeros and ones as elements.we need to bring zeros one side and one other side in si..

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

array contains zeros and ones as elements.we need to bring zeros one side and one other side in si..

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

array contains zeros and ones as elements.we need to bring zeros one side and one other side in si..

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

array contains zeros and ones as elements.we need to bring zeros one side and one other side in si..

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

array contains zeros and ones as elements.we need to bring zeros one side and one other side in si..

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

array contains zeros and ones as elements.we need to bring zeros one side and one other side in si..

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

array contains zeros and ones as elements.we need to bring zeros one side and one other side in si..

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

Post New Answer

More C Interview Questions

how we can make 3d venturing graphics on outer interface

1 Answers   Microsoft,


main() { int a=5; printf(?%d,%d,%d\n?,a,a< <2,a>>2); } Answer: 5,20,1 please explain this code in detail

6 Answers   TCS,


Explain threaded binary trees?

0 Answers  


Is c++ based on c?

0 Answers  


What is structure in c definition?

0 Answers  






Explain how do you convert strings to numbers in c?

0 Answers  


Where local variables are stored in c?

0 Answers  


Explain what is the difference between functions abs() and fabs()?

0 Answers  


Write a client and server program in C language using UDP, where client program interact with the Server as given below: i) The client begins by sending a request to send a string of 8 characters or series of 7 numbers, the server sends back a characters or numbers as per the request of the client. ii) In case of series of 7 numbers: The client sends a multiplication of numbers, to the server. iii) In case of a string of 8 characters: The client sends a reverse order of string to the server.. iv) Server will send an acknowledgment to the client after receiving the correct answer

0 Answers   Ignou, Microsoft,


fun(int x) { if(x > 0) fun(x/2); printf("%d", x); } above function is called as: fun(10); what will it print? }

17 Answers   NDS,


Every time i run a c-code in editor, getting some runtime error and editor is disposing, even after reinstalling the software what may be the problem?

2 Answers  


What is use of null pointer in c?

0 Answers  


Categories