Sapient  Placement Paper   Candidate Experiences   SJCE, Mysore-31 Mar 2009

#### Sapient  Placement Paper   Candidate Experiences   SJCE, Mysore-31 Mar 2009

• Posted by  FreshersWorld
7 Jan, 2012

These are the questions with solutions which come in sapient  exam.I have to upload these 1 year before.But I believe that.its better to late than never.
Good luck

/*
Name : compress and decompress  the string .
One string is given like : aaabbbbbcddddeeee
You have to compress the string like  a3b5c1d4e4
And it may happen while transferring the compress string some non alphanumeric
characters can add in compressed string so compressed string can  become  a@3b%5c*1d\$4e4  like this you have  to remove this extra characters.
*/
#include
#include
#include
void compress(char c[],char res[])
{
int count=1;
int i=0,j=0;
while(i         {
if(c[i]!=c[i+1])
{
res[j++]=c[i];
if(count>=1)
res[j++]=count+'0';
count=1;
}
else
count++;
i++;
}
res[j-1]='\0';
}
void correction(char errormessage[],char correctmessage[])
{
int i=0,j=0;
while(i         {
if((errormessage[i]>='a'&&errormessage[i]<='z')||
(errormessage[i]>='A'&&errormessage[i]<='Z')||
(errormessage[i]>=0+'0'&&errormessage[i]<=9+'0'))
correctmessage[j++]=errormessage[i];
i++;
}
}
void main()
{
clrscr();
char c[20]="aaaabbbbjjja",res[20]=" ";
compress(c,res);
printf("%s",c);
printf("\n%s",res);
char errormessage[40]="a@3b%5c*1d\$4e4",correctmessage[40]=" ";
correction(errormessage,correctmessage);
printf("\n%s",errormessage);
printf("\n%s",correctmessage);
getch();
}

/*
The code was to merge the Danagrams of 2 words. The
danagram of a word is the letters of word arranfed in
sequential order. eg danagram of abhinav is aabhinv.
merging of danamagram is to merge the danagrams of 2
or more words such that the highest no of occurance
are coming for each alphabet.eg
merging of aabhinv and abbhhixz is aabbhhinvxz.
*/
#include
#include
#include
void danagram(char c[])
{
char temp;
for(int i=0;i         {
for(int j=i+1;j             {
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
}
void merge(char c[],char d[],char e[])
{
int i=0,j=0,k=0;
while(i!=strlen(c)-1 || j!=strlen(d)-1)
{
if(c[i]>=d[j])
e[k++]=d[j++];
else
e[k++]=c[i++];
}
while(c[i]!=NULL)
e[k++]=c[i++];
while(d[j]!=NULL)
e[k++]=d[j++];
}
void main()
{
clrscr();
char c[20]="manoj",d[20]="biranchi",e[40];
danagram(c);
danagram(d);
printf("%s",c);
printf("\n%s",d);
merge(c,d,e);
printf("\n After merge =%s",e);
getch();
}

/*there are 67 seats in train . there are only 5 seats in a row and in last
row there are only 2 seats.
One person can reseve only 5 seat at a time.
If person reserving seat , the care is atken that he may get all in row. if
seats are not available in row then the arrangement is so that person group
get nearby seats.
the following class is given
public class seat
{
char name;
int seat;
boolean isSeatempty
}
1.Draw require class digram and object diagram.
2.Write function seatallot(int noofperson)
to allocate seat with seat nuber printed for the each name.
*/
#include
#include
#include
enum boolean{TRUE,FALSE};
class seat
{
char name[50];
int seat;
boolean isSeatempty;
public:
seat()
{
strcpy(name," ");
isSeatempty=TRUE;
seat=0;
}
boolean isempty()
{
return isSeatempty;
}
void display()
{
cout<     seat=seatno;
isSeatempty=FALSE;
}
};
class berth
{
seat seat_number[67];
public:
void display()
{
for(int i=0;i<67;i++)
{
if(i%5==0)
cout<     cout<<"  "<       }
}
void reserve(int numberofseat)
{
int row;
// code if continous seats are available
for(int j=0;j<67;j++)
{
if(j%5==0 &&j>64)       //special case of seat no 66 and 67
row=2;                // here row contain only 2 seats
else if(j%5==0)         // simple case
row=5;                // here row contain 5 seats
if(seat_number[j].isempty()==TRUE && row>=numberofseat)
{
seat_number[j].allocate(j+1);
numberofseat--;
if(numberofseat==0)
break;
}
row--;
}
//////////////////////////////////////////
// code if continous seats are  not available
if(numberofseat!=0)
{
int i=0,j=0,k=0,index_empty=0,min;
int empty[40]={0};   //seats which are vacant know only list
int diffvalue[40]={0};
//logic of finding seats vacant
for(k=0;k<67;k++)
{
if(seat_number[k].isempty()==TRUE)
empty[i++]=k;
}
///////////////////////////////
//logic for finding diference between vacant seats
for(j=0;j<=(i-numberofseat);j++)
{
int index=1;
for(int n=numberofseat;n!=1;n--)
{
diffvalue[j]=diffvalue[j]+(empty[j+index]-empty[j+index-1]);
index++;
}
if(j==0)
min=diffvalue[0];
if(diffvalue[j]       {
min=diffvalue[j];
index_empty=j;  //gives the minumum value of particular index  in the empty list
}
}
//////////////////////////////////////////////////////
// final allocation to nearer seats
for( k=0;k     {
seat_number[empty[index_empty+k]].allocate(empty[index_empty+k]+1);
}
//////////////////////////////////
}
}
};
void main()
{
clrscr();
berth boggy;
boggy.reserve(4);
boggy.reserve(4);
boggy.reserve(3);
boggy.reserve(3);
boggy.reserve(3);
boggy.reserve(3);
boggy.reserve(3);
boggy.reserve(3);
boggy.reserve(5);
boggy.reserve(4);
boggy.reserve(4);
boggy.reserve(4);
boggy.reserve(4);
boggy.reserve(4);
boggy.display();
boggy.reserve(3);
boggy.display();
getch();
}

#include
#include
#include
char * ones(int X );
char * tens(int X , int Y );
char * copyone(char buff[50],int i)
{
char temp[50];
strcat(buff,ones(i));
strcpy(temp,buff);
return temp;
}
char * copyten(char buff[50],int i,int j)
{
char temp[50];
strcat(buff,tens(i,j));
strcpy(temp,buff);
return temp;
}
char * numbertotext(unsigned long int text)
{
int num,a[11],i ;
char temp[200]=" ",buff[50];
for(i=0;text!=0;text=text/10)
a[i++]=text%10;
for(int t=i-1;t>=0;t--)
cout<<" "<     i--;
for (;i >= 0;i--)
{
if (a[i] != 0)
{
strcpy(buff," ");
switch(i)
{
case 0: if (a[1] != 1)
strcat(temp,copyone(buff,a[i]));
break;
case 1: strcat(temp,copyten(buff,a[i],a[i-1]));
break;
case 2:  strcat(temp,copyone(buff,a[i]));
strcat(temp," hundred");
break;
case 3:  if (a[4] != 1)
{
strcat(temp,copyone(buff,a[i]));
strcat(temp,"  Thousand");
}
else
strcat(temp," Thousand");
break;
case 4:  strcat(temp,copyten(buff,a[i],a[i-1]));
if (a[3] == 0)
strcat(temp,"  Thousand");
break;
case 5: if (a[6] != 1)
{
strcat(temp,copyone(buff,a[i]));
strcat(temp,"  Lakhs");
}
else
strcat(temp," Lakhs");
break;
case 6: strcat(temp,copyten(buff,a[i],a[i-1]));
if (a[5] == 0)
strcat(temp,"  Lakhs");
break;
case 7: if (a[8] != 1)
{
strcat(temp,copyone(buff,a[i]));
strcat(temp," Croes");
}
else
strcat(temp," Croes");
break;
case 8:  strcat(temp,copyten(buff,a[i],a[i-1]));
if (a[7]== 0)
strcat(temp,"  Cores");
break;
case 9: if (a[10] != 1)
{
strcat(temp,copyone(buff,a[i]));
strcat(temp," Arabs");
}
else
strcat(temp," Arabs");
break;
case 10:strcat(temp,copyten(buff,a[i],a[i-1]));
if (a[9] == 0)
strcat(temp,"  Arabs");
break;
}
}
}
strcat(temp," only");
return temp;
}

char * ones(int X )
{
char  stmt[20] ;
switch( X )
{
case 1:strcpy(stmt , "One");break;
case 2:strcpy(stmt , "Two");break;
case 3:strcpy(stmt , "Three");break;
case 4:strcpy(stmt , "Four");break;
case 5:strcpy(stmt , "Five");break;
case 6:strcpy(stmt , "Six");break;
case 7:strcpy(stmt , "Seven");break;
case 8:strcpy(stmt , "Eight");break;
case 9:strcpy(stmt , "Nine");break;
}
return stmt;
}

char * tens(int X , int Y )
{
char stmt[50];
if (X == 1 )
{
switch( Y)
{
case 1:strcpy(stmt, "Eleven");break;
case 2:strcpy(stmt, "Twelve");break;
case 3:strcpy(stmt, "Thirteen");break;
case 4:strcpy(stmt, "Fourteen");break;
case 5:strcpy(stmt, "Fifteen");break;
case 6:strcpy(stmt, "Sixteen");break;
case 7:strcpy(stmt, "Seventeen");break;
case 8:strcpy(stmt, "Eighteen");break;
case 9:strcpy(stmt, "Nineteen");break;
case 0:strcpy(stmt, "Ten");break;
}
}
else
{
switch( X )
{
case 2:strcpy(stmt, "Twenty");break;
case 3:strcpy(stmt, "Thirty");break;
case 4:strcpy(stmt, "Fourty");break;
case 5:strcpy(stmt, "Fifty");break;
case 6:strcpy(stmt, "Sixty");break;
case 7:strcpy(stmt, "Seventy");break;
case 8:strcpy(stmt, "Eighty");break;
case 9:strcpy(stmt, "Ninety");break;
}
}
return  stmt;
}
void main()
{
clrscr();
unsigned long int text=2345;
cout<<"Enter the Number :";
cin>>text;
cout<<"\nNumber :"<     cout<<"\nText   :";
cout<     getch();
}

/*1) Write a program for the problem: the array of
inetegers indicating the marks of the students is
given, U have to calculate the percentile of the
students aaccording to this rule: the percentile of a
student is the %of no of student having marks less
then him. For eg:
suppose
Student        Marks
A              12
B              60
C              80
D              71
E              30
F              45
percentile of C =  5/5 *100 = 100 (out of 5 students 5
are having marks less then him)
percentile of B  = 3/5*100 = 60% (out of 5, 3 have
markses less then him)*/
#include
#include
# define MAX 6
/*class students
{
float marks[MAX];
float percentile[MAX];
public:
students()
{
for(int i=0;i             {
marks[i]=0.0;
percentile[i]=0.0;
}
}
students(float marks[MAX])
{
for(int i=0;i             {
this.marks[i]=marks[i];
percentile[i]=0.0;
}
}
{
for(int i=0;i             {
cout<<"Enter the marks of student["<             cin>>marks[i];
}
}
void display()
{
for(int i=0;i             {
cout.precision(2);
cout<<"\nMarks :"<             }
}
void calculate()
{
float count,same,total;
for(int i=0;i             {
count=0;
same=0;
total=MAX;
for(int j=0;j                 {
if(marks[i]>marks[j])
count++;
if(marks[i]==marks[j])
same++;
}
if(total!=same)
{
total=total-same;
percentile[i]=(count/total)*100;
}
else
percentile[i]=100;
}
}
};*/
void main()
{
clrscr();
float marks[MAX]={10,10,10,10,10,10};
float percentile[6]={0},count,same,total;
for(int i=0;i         {
cout<<"Enter the numbers :";
cin>>marks[i];
}
for(i=0;i         {
count=0;
same=0;
total=MAX;
for(int j=0;j             {
if(marks[i]>marks[j])
count++;
if(marks[i]==marks[j])
same++;
}
if(total!=same)
{
total=total-same;
percentile[i]=(count/total)*100;
}
else
percentile[i]=100;
cout.precision(2);
cout<<"Marks=  "<         cin>>name[i];
cout<<"Enter the marks of student["<         cin>>marks[i];
}
}
void students::display()
{
for(int i=0;i         {
cout.precision(2);
cout<<"\n\nName :"<         }
}
void students::calculate()
{
float count,same,total;
for(int i=0;i         {
count=0;
same=0;
total=MAX;
for(int j=0;j             {
if(marks[i]>marks[j])
count++;
if(marks[i]==marks[j])
same++;
}
if(total!=same)
{
total=total-same;
percentile[i]=(count/total)*100;
}
else
percentile[i]=100;
}
}
void main()
{
clrscr();
float marks[MAX]={20,20,30,10,10,5};
char name[MAX][40]={"a","b","c","d","e","f"};
students obj(marks,name);
obj.calculate();
obj.display();
getch();
}
/*1) Write a program for the problem: the array of
inetegers indicating the marks of the students is
given, U have to calculate the percentile of the
students aaccording to this rule: the percentile of a
student is the %of no of student having marks less
then him. For eg:
suppose
Student        Marks
A              12
B              60
C              80
D              71
E              30
F              45
percentile of C =  5/5 *100 = 100 (out of 5 students 5
are having marks less then him)
percentile of B  = 3/5*100 = 60% (out of 5, 3 have
markses less then him)*/
#include
#include
void main()
{
clrscr();
int marks[6]={12,60,80,71,30,45};
int percentile[6],more_students_having_marks=0;
int total_student=6-1;
for(int j=0;j<6;j++)
{
more_students_having_marks=0;
for(int i=0;i<6;i++)
{
if(marks[i]                 more_students_having_marks++;
}
percentile[j]=(more_students_having_marks*100)/total_student;
}
for(int i=0;i<6;i++)
cout<        getch();
}
#include
#include
#include
#include
enum boolean{FLASE,TURE};
class seat
{
char name[10];
int seatno;
boolean isSeatempty;
public:
void seatallocate(char n[10],int sn)
{
strcpy(name,n);
seatno=sn+1;
isSeatempty=FLASE;
}
seat()
{
strcpy(name,"\0");
seatno=-1;
isSeatempty=TURE;
}
boolean stateofseat()
{
return isSeatempty;
}
void display()
{
cout<               }
cout<        }
}
void displaywithname()
{

for(int i=0;i<67;i++)
{

if(i%5==0)
cout<<"\n\n";
else
cout<<"\t";
if( !b[i].stateofseat())
b[i].display();
}
}
int totalrev()
{
int no=0;
for(int i=0;i<67;i++)
{
if(b[i].stateofseat()==0)
{
no++;
}
}
return no;
}
void rev(int no,char name[5][10])
{
int row=5,totalno,emptyseat[35],sumof[30]={0},next=0;
totalno=totalrev();
if(totalno==67)
{
cout<<"\n NO SEAT IS EMPTY";
getch();
}
else if(totalno+no<=67)
{
for(int i=0;i<67;i++)
{
if(i%5==0)
{
if(i==65)
row=2;
else
row=5;
}
if(b[i].stateofseat()&&row>=no)
{
b[i].seatallocate(name[next++],i);
no--;
if(!no)
break;
}
row--;
}
int j=0;
if(no>0)
{
for(int i=0;i<67;i++)
{
if(b[i].stateofseat())
{
emptyseat[j++]=i;
}
}
int small,index;
for(i=0;i<=j-no;i++)
{
for(int k=1;k                        {
sumof[i]=sumof[i]+(emptyseat[i+k]-emptyseat[i+k-1]);
}
if(i==0)
{
small=sumof[i],index=0;
}
else if(small>sumof[i])
{
small=sumof[i];
index=i;
}
}
for(i=0;i                 {
b[emptyseat[index+i]].seatallocate(name[5-no+i],emptyseat[index+i]);
}
}

}
else
{
cout<<"\nLess Number of Seat Left";
getch();
}
}

};
void main()
{
boggy b;
char name[5][10]={"A12345678","B12345678","C12345678","D12345678","E12345678"};

/* b.rev(2,name);
b.rev(4,name);
b.rev(2,name);
b.rev(3,name);
b.rev(4,name);
b.rev(3,name);
b.rev(3,name);
b.rev(4,name);
b.rev(4,name);
b.rev(4,name);
b.rev(3,name);
b.rev(3,name);
b.rev(4,name);
b.rev(3,name);
b.rev(3,name);
b.rev(2,name);
b.rev(1,name);
b.rev(2,name);
b.rev(5,name);
b.rev(5,name);
b.rev(2,name);
b.display() ;
getch();
b.rev(1,name);
*/
char ch='y';
int no,i;

do{
clrscr();
b.display() ;
cout<<"\nEnter the Number of Seat(Max of 5 ) : ";
cin>>no;
if(no>5)
{
cout<<"\n U Have Enter Wrongh U want to Enter More(y/n) : ";
cin>>ch;
continue;
}
for(i=0;i        {
cout<<"\nName : " ;
cin>>name[i];
}
b.rev(no,name);
cout<<"\nDo U want to Enter More(y/n) : ";
cin>>ch;
} while(ch!='n');
getch();
b.display() ;
b.displaywithname();
getch();
}

/*Qs.1: A company has many employees & each employee is led by only 1 person
except for the CEO (who has no boss). The cost to the company of an employee
is the sum of  his salary plus the cost to the company of all the people
led by him. Given is the following structure :

Employee

Data members:
Name
Salary
isBoss
nameOfBoss

Methods:
getSalary
getName

a) Define the class/structure
b) Write a function getCostToCompany() to calculate the cost to the company
of an employee whose name is passed as a parameter to the function
getCostToCompany()

public float getCostToCompany(String name)

Note: Donn't write any input functions such as main() & assume that the data

#include
#include
#include
enum boolean{TRUE,FALSE};
class  Employee
{
char    Name[30];
float    Salary;
boolean    isBoss;
char     nameOfBoss[30];
public:
Employee()
{
strcpy(Name," ");
Salary=0.0;
isBoss=FALSE;
strcpy(nameOfBoss," ");
}
Employee(char name[30],float s,boolean b,char bossname[30])
{
strcpy(Name,name);
Salary=s;
isBoss=b;
strcpy(nameOfBoss,bossname);
}
Employee(Employee &t)
{
strcpy(Name,t.Name);
Salary=t.Salary;
isBoss=t.isBoss;
strcpy(nameOfBoss,t.nameOfBoss);
}
float    getSalary()
{ return Salary;}
char *    getName()
{ return Name;}
char *    getnameOfBoss()
{ return nameOfBoss;}
void     display();
};
{
cout<<"Enter name :" ;
cin>>Name;
cout<<"Enter salary :";
cin>>Salary;
cout<<"Enter isboss :" ;
cin>>isBoss;
cout<<"Enter boss name :";
cin>>nameOfBoss;
}
void Employee::display()
{
cout<<"Enter name :" ;
cout<         cout<<"\nEnter salary :";
cout<         cout<<"\nEnter isboss :" ;
cout<         cout<<"\nEnter boss name :";
cout<     }
class Company
{
Employee emp[10];
int count;
public:
Company()
{
count=0;
for(int i=0;i<10;i++)
emp[i];
}
{
char ch;
do{
cout<<"Continue :";
cin>>ch;
}while(ch!='n');
/*   Employee t("1",100.0f,TRUE,"manoj");
emp[0]=t;
Employee t1("2",200.0f,TRUE,"1");
emp[1]=t1;
Employee t2("3",1000.0f,TRUE,"1");
emp[2]=t2;
Employee t3("4",1000.0f,TRUE,"manoj");
emp[3]=t3;
count=4;*/
}
void display_company()
{
for(int j=0;j             {
emp[j].display();
cout<<"\n\n";
}
}
float getCostToCompany(char name[100]);
};
float Company::getCostToCompany(char *name)
{
float ctc=0;
for(int temp=0;temp         {
if(strcmp(emp[temp].getName(),name)==0)
ctc=ctc+emp[temp].getSalary();
if(strcmp(emp[temp].getnameOfBoss(),name)==0)
ctc=ctc+getCostToCompany(emp[temp].getName());
}
return ctc;
}
void main()
{
clrscr();
Company obj;
obj.display_company();
cout<     getch();
}

/*i'm wrirng in a simple programming style here : in a sentece we have to
compare each word from a list and replce it with "###" if it exists in th
list.*/
#include
#include
#include
void main()
{
clrscr();
char msg[200]="they are people who r stupid and dog who used slang which are sucker , fucker";
cout<<"enter message :";
cin>>
char slangs[10][50]={"stupid","dog","sucker","fucker"};
char words[40][50];
int i=0,j=0,k=0;
while(msg[i]!=NULL)
{
if(msg[i]!=' '&&msg[i]!=',')
words[j][k++]=msg[i];
if(msg[i]==' '&&msg[i+1]!=' ')
{
words[j][k]='\0';
j++;
k=0;
}
i++;
}
for(i=0;i<=j;i++)
cout<             }
}
cout<     for(i=0;i<=j;i++)
cout<<words[i]<<" ";

getch();
}