Tuesday, April 17, 2012

Binary search tree c++ code

#include <iostream>

using namespace std;

void sortedarr(int *,int);
int bst(int,int, int, int *);

int main(){
    int size;
    int a[100];
    int data;
    cout<<"Enter the size of the array : ";
    cin>>size;
   
    cout<<"Enter array elements ";
    for(int i = 0; i<size; i++){
        cin>>a[i];
        }

    sortedarr(a,size);

    cout<<"Enter the Data : ";
    cin>>data;

    int b;
    b = bst(data,0,size-1,a);

    if(b == -1){
        cout<<"Not found"<<endl;
    }
    else{
        cout<<"Fount at index no : "<<b+1<<" place"<<endl;
    }

}

void sortedarr(int a[],int m){
    int k = 1;
    int temp,i;

    while(k<m){
        for(i=0; i<k && k<m; i++ ){
            if(i<m){
                if(a[i]>a[k]){
                    temp = a[i];
                    a[i] = a[k];
                    a[k] = temp;
                }

            }               
        }   
    k=k+1;
    i=0;       
    }
}

int bst(int data,int lb,int ub,int a[]){
    int mid;
    if((ub+lb)%2 ==0){
        mid=(ub+lb)/2;
    }
    else{
        mid = (ub+lb+1)/2;
    }

    while(lb<=ub){
        if(a[mid]==data){
            return mid;
        }   
        else if(a[mid]<data){
            lb = mid+1;
            return bst(data,lb,ub,a);
        }
        else if(a[mid]>data){
            ub = mid-1;
            return bst(data,lb,ub,a);
        }
    }

return -1;
}

Monday, April 16, 2012

100 C Aptitude Questions with Answers (Part 5)




81)          main(int argc, char **argv)

{

printf("enter the character"); 
getchar(); 
sum(argv[1],argv[2]);
}

sum(num1,num2) int num1,num2;
{

return num1+num2;

}

100 C Aptitude Questions with Answers (Part 4)




61)          main()

{

char *cptr,c;
void *vptr,v; c=10; v=0;
cptr=&c; vptr=&v;

printf("%c%v",c,v);

}

100 C Aptitude Questions with Answers (Part 3)




41)         
 #include<stdio.h>
main()
{
        struct xx
        {
                int x=3;
                char name[]="hello";
         };
         struct xx *s=malloc(sizeof(struct xx));
         printf("%d",s->x);
         printf("%s",s->name);
}

100 C Aptitude Questions with Answers (Part 2)




21.
#define square(x) x*x 
main()
{
        int i;
        i = 64/square(4);
        printf("%d",i);
}

100 C Aptitude Questions with Answers (Part 1)




It is assumed that,
# Programs run under DOS environment,
# The underlying machine is an x86 system,
# Program is compiled using Turbo C/C++ compiler.

# The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).


Microsoft Placement Question Paper (Algorithm And Programming) Set -1




1. Given a rectangular (cuboidal for the puritans) cake with a rectangular piece removed (any size or orientation), how would you cut the remainder of the cake into two equal halves with one straight cut of a knife ?