placement training problem quetion
Q1 find hamming distance between two arrays
// find hamming distance between two arrays
#include <iostream>
using namespace std;
int HDist(int A[],int B[],int size);
int main()
{
int A[]={1,0,0,1,0,1};
int B[]={0,1,0,0,1,1};
int size=sizeof(A)/sizeof(int);
cout<<HDist(A,B,size);
return 0;
}
int HDist(int A[],int B[],int size)
{
int i,sumc=0;
for(i=0; i<size; i++)
{sumc+=A[i]^B[i];}
return sumc;
}
Output 4
Q2 find leaders in c++
leaders in an array -->those elements which are greater than elements on their right
#include <iostream>
using namespace std;
void print_leader(int A[],int size);
int main()
{
int A[]={6,7,4,3,5};
int size=sizeof(A)/sizeof(int);
print_leader(A,size);
}
void print_leader(int A[],int size)
{
int i,leader=A[size-1]-1;
for(i=size-1;i>=0;i--)
{
if(A[i]>leader)
{
cout<<A[i]<<",";
leader=A[i];
}
}
}
Output 5,7
Q3 write a program in c++ to demonstrate passing of array to functions
#include <iostream>
using namespace std;
void display(int ar[],int size)
{
for(int i=0;i<size;i++)
cout<<ar[i]<<" ";
}
int main()
{
int ar[5]={1,2,3,4,5};
display(ar,5);
}
Output 1,2,3,4,5
Q4 how to make pointer and how to assign value to them
// using pointer
#include <iostream>
using namespace std;
int main()
{
int x=10;
int *ptr;//declaration
ptr=&x;//refrencing
cout<<*ptr;//derefrencing
}
Output 10
Q5 more example of pointer to under stand more
#include <iostream>
using namespace std;
int main()
{
int x=10,y=20;
int *ptr;
ptr=&x;
cout<<*ptr<<" ";
ptr=&y;
cout<<*ptr<<" ";
(*ptr)++;
cout<<y<<" ";
y=y+10;
cout<<*ptr;
}
Output 10 20 21 31
Q6 other pointer example for practice
#include <iostream>
using namespace std;
int main()
{
int x=10,y=20;
int *ptr1=&x,*ptr2=&y;
int temp;
temp=*ptr1;//temp=10
*ptr1=*ptr2;//ptr1=20
*ptr2=temp;//ptr2=10
cout<<x<<" "<<y<<" "<<*ptr1<<" "<<*ptr2;
}
Output 20 10 20 10
Q7 other example for understanding
#include <iostream>
using namespace std;
int main()
{
int x=10, y=20;
int*ptr1=&x, *ptr2=&y;
int *temp;
temp=ptr1;//temp=10
ptr1=ptr2;//ptr1=20
ptr2=temp;//ptr2=10
cout<<x<<" "<<y<<" "<<*ptr1<<" "<<*ptr2;
}
Output 10 20 20 10
Q8 other example for more understanding
#include <iostream>
using namespace std;
int main()
{
int x=10,y=20, z=30;
int*ptr1=&x,*ptr2=&y,*ptr3=&z;
*ptr1=*ptr2;//ptr1=20
ptr2=ptr3;//ptr2=30
cout<<x<<" "<<y<<" "<<z<<" "<<*ptr1<<" "<<*ptr2<<" "<<*ptr3;
}
Output 20 20 30 20 30 30
Q9more example for pointer understanding
#include <iostream>
using namespace std;
int main()
{
int x=10, y=20,z=30;
int *ptr1=&x, *ptr2=&y, *ptr3=&z;
*ptr1=*ptr2+*ptr3;
ptr2=ptr3;
*ptr3=*ptr1+*ptr2;
cout<<x<<" "<<y<<" "<<z;
}
Output 50 20 80
Q10 more example more understanding
#include <iostream>
using namespace std;
int main()
{
int A[5]={-10,7,8,15,20};
cout<<*(A+2)<<" ";// 8
cout<<*(A+3)<<" ";// 15
*(A+2)=*(A+3)-8;//*(A+2)=7
cout<<*(A+2)<<" ";
int*ptr;
ptr=A;//ptr=-10
cout<<*ptr<<" ";
ptr++;//ptr=*(A+1)
cout<<*ptr<<" ";
ptr=ptr+2;//ptr=(*A+3)
cout<<*ptr;//15
}
Output 8 15 7 -10 7 15
Q11 more example for understanding
#include <iostream>
using namespace std;
int main()
{
int A[5]={-10,9,8,16,20};
int *ptr=&A[2];
cout<<ptr[0]<<" ";//8
cout<<ptr[-1]<<" ";//9
cout<<ptr[1];//16
}
Output 8 9 16
Q12 more example poiner point array value array
#include <iostream>
using namespace std;
int main()
{
int A[6]={10,20,30,40,50,60};
int *ptr=A+3;//40
cout<<ptr[0]<<" "<<ptr[-2]<<" "<<ptr[2];
}
Output 40 20 60
Q13 more example for understanding
#include <iostream>
using namespace std;
int main()
{
int A[6]={10,20,30,40,50,60};
int sumc=0,i=0;
int *ptr=A;
//cout<<*ptr;
while (i<6)
{
sumc+=*ptr;
ptr++; i++;
}
cout<<sumc<<" ";
}
Output 210
Q14 write a program in c++ to allocate a dynamic array using malloc and print it
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"enter size of array =";
cin>>n;
int *ptr=(int*)malloc(n*sizeof(int));
cout<<"enter array value";
for(int i; i<n; i++)
cin>>ptr[i];
cout<<"array=";
for(int i;i<n;i++)
cout<<ptr[i]<<" ";
}
Output enter size of array =5
enter array value1
2
3
4
5
array=1 2 3 4 5
Q15 write a program in c++ to demonstrate passing of array to functions
#include <iostream>
using namespace std;
void display(int ar[],int size)
{
for(int i;i<5;i++)
cout<<ar[i]<<" ";
}
int main()
{
int ar[5]={1,2,3,4,5};
display(ar,5);
}
Output 1 2 3 4 5
Q16 write a program in c++ to find the index of the element in an array
#include <iostream>
using namespace std;
int main()
{ //take the number of array as input from user
int n;
cout<<"enter number of array= ";
cin>>n;
//declare an integer array
int ar[n];
//take the elements of array from user
cout<<"enter array value ";
for(int i=0;i<n;i++)
{ cin>>ar[i];}
//take element from the user whose index is to be selected
cout<<"enter element which index you want ";
int m;
cin>>m;
//traverse the array and if element is found print the corresponding index value
for(int i=0; i<n; i++)
if(ar[i]==m)
{cout<<"index of array=";
cout<<i;}
}
Output-> enter number of array= 5
enter array value 1
2
3
4
5
enter element which index you want 4
index of array=3
*Note-> Now you are done finding with index of the element in an array
Q17 modify the code (Q16) to print the found index only if it is even
#include <iostream>
using namespace std;
int main()
{ //take the number of array as input from user
int n;
cout<<"enter number of array= ";
cin>>n;
//declare an integer array
int ar[n];
//take the elements of array from user
cout<<"enter array value ";
for(int i=0;i<n;i++)
{ cin>>ar[i];}
//take element from the user whose index is to be selected
cout<<"enter element which index you want ";
int m;
cin>>m;
//traverse the array and if element is found print the corresponding index value
for(int i=0; i<n; i++)
if(ar[i]==m)
if(m%2==0)//check index is odd or even
{cout<<"index of array=";
cout<<i;}
else
cout<<"index is odd";
}
Q18
Comments
Post a Comment