ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   To Refer this Site to Your Friends   Click Here
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
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}
 Question Submitted By :: Chan
I also faced this Question!!     Rank Answer Posted By  
 
  Re: 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
# 1
#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 ?    2 Yes 0 No
Vignesh1988i
 
  Re: 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
# 2
I Hope this will also work for this question.
I just took it for length of 8 but we can extend it to any
level.

void swap(int* p, int x, int y)
{
   int tmp;
   tmp  = *(p+x);
   *(p+x) = *(p+y);
   *(p+y) = tmp;
}
int main()
{
   int* ptr = (int*)malloc(sizeof(8));
   int c, i, j;

   for(c=0; c<8 ; c++) scanf("%d", ptr+c);

   for(i=0; i<8; i++)
   {
      for(j=0; j<8; j++)
      {
         if( *(ptr+j) > *(ptr+j+1) ) swap(ptr, j, j+1);
      }
   }

   for(c=0;c<8;c++) printf("%d", *(ptr+c));
   return 0;
}
 
Is This Answer Correct ?    1 Yes 2 No
Satinder Singh
 
 
 
  Re: 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
# 3
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 ?    3 Yes 0 No
Vignesh1988i
 
  Re: 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
# 4
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 ?    2 Yes 0 No
Rizwan
 
  Re: 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
# 5
#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 0 No
Rajasekaran
 
  Re: 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
# 6
#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 ?    0 Yes 0 No
Ashok Kannan
 
  Re: 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
# 7
#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={0,0,1,0,1,1,0,0};


int *arrayTemp = a;


int i, j_0 = 0, j_8 = 8;

for(int data = 0; *a != '\0'; *a++)
{

data = *a;
if(data == 1 )
{
   a[j_0++] = data;
}
else
{
   a[j_8--] = data;
} // if-else block

}//for loop

}//main function

for(i=0; i< 8; i ++)
{

 printf("array value is %d", a[i]);
 
}
 
Is This Answer Correct ?    0 Yes 0 No
Manoj
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
Consider a language that does not have arrays but does have stacks as a data type.and PUSH POP..are all defined .Show how a one dimensional array can be implemented by using two stacks. Google3
DIFFERNCE BETWEEN THE C++ AND C LANGUAGE? Wipro2
Write a program to find the given number is odd or even without using any loops(if,for,do,while)  2
if a five digit number is input through the keyboard, write a program to calculate the sum of its digits. (hint:-use the modulus operator.'%')  9
what is the difference between strcpy() and memcpy() function?  1
What is alloca() and why is its use discouraged?  1
What is the output of the program given below #include<stdio.h> main() { char i=0; for(;i>=0;i++) ; printf("%d\n",i); } ADITI14
What is a far pointer?What is the utility?  2
program for comparing 2 strings without strcmp()  3
simple c program for 12345 convert 54321 with out using string  5
Given an unsigned integer, find if the number is power of 2?  3
program to find the ASCII value of a number  5
what is call by value and call by reference  2
why java is called as a purely oops language.  2
read the folllowing code # define MAX 100 # define MIN 100 .... .... if(x>MAX) x=1; else if(x<MIN) x=-1; x=50; if the initial value of x=200,what is the vlaue after executing this code? a.200 b.1 c.-1 d.50 TCS2
will u give me old quesrion papers for aptitude for L & t info tech? L&T1
what is the output of the following program and explain the answer #include<stdio.h> exp() { main(5) } main(int a) { printf("%d",a); return; } Satyam3
WHY DO WE USE A TERMINATOR IN C LANGUAGE?  2
what is difference between ++(*p) and (*p)++ Accenture15
If 4 digits number is input through the keyboard, Write a program to calculate sum of its 1st & 4th digit.  4
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com