Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

10. Study the code: void show() main() { show(); } void show (char *s) { printf("%sn",s); } What will happen if it is compiled & run on an ANSI C Compiler? A)It will compile & nothing will be printed when it is executed B)it will compile but not link C)the compiler will generate an error D)the compiler will generate a warning

5 Answers   Accenture,


How are structure passing and returning implemented?

0 Answers  


can we write a c program with out using main

3 Answers  


Explain what is the difference between the expression '++a' and 'a++'?

0 Answers  


what are the various memory handling mechanisms in C ?

4 Answers   HP,


long int size a) 4 bytes b) 2 bytes c) compiler dependent d) 8 bytes

18 Answers   Acropolis, HCL, Intel, TCS,


What is the difference b/w main() in C language and main() in C++.

7 Answers  


What does it mean when a pointer is used in an if statement?

0 Answers  


What is structure pointer in c?

0 Answers  


What are the usage of pointer in c?

0 Answers  


What is the diffences between Windows XP and Windows Visa

1 Answers   Aricent, FHF,


The operation of a stair case switch best explains the a) or operation b) and operation c)exclusive nor operation d)exclusive or operation Which of the following is/are syntactically correct? a) for(); b) for(;); c) for(,); d) for(;;);

1 Answers   HCL, Public Service Commission,


Categories