roy


{ City }
< Country > india
* Profession *
User No # 8945
Total Questions Posted # 0
Total Answers Posted # 5

Total Answers Posted for My Questions # 0
Total Views for My Questions # 0

Users Marked my Answers as Correct # 497
Users Marked my Answers as Wrong # 134
Questions / { roy }
Questions Answers Category Views Company eMail




Answers / { roy }

Question { Microsoft, 25342 }

Write a Binary Search program


Answer

#include
#include
#include

int binarysearch(int list[], int end, int target, int &locn)
{
int first=0, mid, last=end;
while(first<=last)
{
mid=(first+last)/2;
if(target>list[mid])
first=mid+1;
else if(target last=mid-1;
else
break;
}
locn=mid+1;
return(target==list[mid]);
}

void main()
{
int a[10],i,s=0,n,loc,flag=0;
clrscr();
cout<<"\n Enter the no. of element to store:\n";
cin>>n;
cout<<"Enter the Elements:\n";
for(i=0;i cin>>a[i];

cout<<"\n The Elements are:\n";
for(i=0;i cout<
cout<<"\n Enter the Element to search:\n";
cin>>s;

if(binarysearch(a,n,s,&loc))
cout<<"\nThe element "< "< else
cout<<"\nThe element "<

}

Is This Answer Correct ?    51 Yes 23 No

Question { Cognizant, 47204 }

Write programs for Bubble Sort, Quick sort


Answer

/*QUICK SORT*/
#include
#include

int split(int [],int,int);
void quicksort(int [],int,int);

void main()
{
int arr[20],n,i;
clrscr();
printf("\nQUICk SORT\n");
printf("Enter the no.of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i scanf("%d",&arr[i]);
printf("\nArray before sorting:\n");
for(i=0;i printf("%d\t",arr[i]);
quicksort(arr,0,n);
printf("\nArray after sorting:\n");
for(i=0;i printf("%d\t",arr[i]);
getch();
}


void quicksort(int a[],int lower,int upper)
{
int i;
if(upper>lower)
{
i=split(a,lower,upper);
quicksort(a,lower,i-1);
quicksort(a,i+1,upper);
}
}

int split(int a[],int lower,int upper)
{
int i,p,q,t;
p=lower+1;
q=upper;
i=a[lower];
while(q>=p)
{
while(a[p] p++;
while(a[q]>i)
q--;
if(q>p)
{
t=a[p];
a[p]=a[q];
a[q]=t;
}
}
t=a[lower];
a[lower]=a[q];
a[q]=t;
return(q);
}


Is This Answer Correct ?    114 Yes 46 No


Question { TCS, 72771 }

Write the programs for Linked List (Insertion and Deletion)
operations


Answer

/*OPERATIONS ON SINGLY LINKED LIST*/

#include
#include
struct link
{
int item;
struct link *next;
};
typedef struct link node;
void addfirst();
void addlast();
void addmid();
void delfirst();
void dellast();
void delmid();
void display();
node *head=NULL;


void main()
{
int ch;
clrscr();
do
{
printf("\nSINGLY LINKED LIST OPERATIONS\n");
printf("\n1.Addfirst\n2.AddMid\n3.AddLast\n4.DeleteFirst\n5.DeleteMiddle\n6.DeleteLast\n7.Display\n8.Exit\n");
printf("Enter your option:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
addfirst();
display();
break;
case 2:
addmid();
display();
break;
case 3:
addlast();
display();
break;
case 4:
delfirst();
display();
break;
case 5:
delmid();
display();
break;
case 6:
dellast();
display();
break;


case 7:
display();
break;
case 8:
exit(0);
break;
default:
printf("Invalid Choice\n");
}
}
while(ch<=8);
getch();
}


void addfirst()
{
node *temp;
temp=(node *)malloc(sizeof(node));
printf("Enter the data....\t");
scanf("%d",&temp->item);
temp->next=head;
head=temp;
}

void addmid()
{
int i=1,pos;
node *cur=head,*temp;
printf("\nEnter the position\t");
scanf("%d",&pos);
while(pos!=i+1&&cur!=NULL)
{
cur=cur->next;
i++;
}
if(pos==i+1)
{
temp=(node *)malloc(sizeof(node));
printf("Enter the data...");
scanf("%d",&temp->item);
temp->next=cur->next;
cur->next=temp;
}
}


void addlast()
{
node *temp,*cur=head;
temp=(node *)malloc(sizeof(node));
printf("\nEnter the data....");
scanf("%d",&temp->item);
while(cur->next!=NULL)
{
cur=cur->next;
}
temp->next=cur->next;
cur->next=temp;
}


void delfirst()
{
node *temp=head;
head=head->next;
printf("Deleted item is %d\n",temp->item);
free(temp);
}


void delmid()
{
int i=1,pos;
node *cur=head,*temp;
printf("Enter the position to be deleted\t");
scanf("%d",&pos);
while(pos!=i+1&&cur->next!=NULL)
{
cur=cur->next;
i++;
}
if(pos==i+1)
{
temp=cur->next;
cur->next=temp->next;
printf("Deleted item is %d\n",temp->item);
free(temp);
}
}


void dellast()
{
node *temp,*cur=head;
while(cur->next->next!=NULL)
{
cur=cur->next;
}
temp=cur->next;
cur->next=NULL;
printf("Deleted item is %d\n",temp->item);
free(temp);
}


void display()
{
node *cur=head;
printf("\nHead->");
while(cur!=NULL)
{
printf("\t%d",cur->item);
cur=cur->next;
}
printf("<-NULL\n");
}

Is This Answer Correct ?    247 Yes 53 No

Question { BPO, 48325 }

Parenthesis are never needed in prefix or postfix
expressions. Why?


Answer

Evaluation of postfix or prefix notations are done with the
help of stacks. So there is no need for brackets.

In other words, infix notation is human-readable format,
which need brackets based on BODMAS rule to understand the
order of execution. The postfix or prefix are
machine-readable format. The conversion and evaluation of
postfix and prefix are applications of stack model.

Is This Answer Correct ?    46 Yes 7 No

Question { BMC, 18093 }

What is binary tree?


Answer

A binary tree is a tree in which each node can have at most
two children. (i.e., left child and/or right child)

Is This Answer Correct ?    39 Yes 5 No