Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Practice

Dr. Shikha Magotra


1. Create an object of MyClass called obj

Ans- class MyClass{


public :
int x;
};
int main(){
MyClass Obj;
}
2. Use an access specifier to make members of MyClass accessible from outside the

class.

Ans- public
3.Create an object of MyClass called myObj, and use it to set the value of myNum to 15.

Ans-

int main(){
MyClass myObj;
myobj.myNum=15;
}
4. Create a function named myMethod with no return value. Then Use myObj to call myMethod inside main.
Ans-
Class MyClass{
public:
int myNum;
void myMethod(void);
};
int main(){
MyClass myObj;
myObj.myMethod;
}

5. Create a constructor of MyClass, and call it:


Ans-
class MyClass{
public:
MyClass{
cout <<”Hello World”;
}
};

int main(){
MyClass myObj;
}
1. Write a function which will be given as input an array, its size and an integer p. The function will then
cyclically shift the array p positions to the right: each element is moved p positions to the right, while the
last p elements are moved
to the beginning of the array. For example: if we have the array [ 1 2 3 4 5 6], shifting 2 positions to the right
should give the array [ 5 6 1 2 3 4 ]. Your function should work correctly for negative values of p.
Ans-

# include<iostream>
using namespace std;
void shift(int pr[],int n,int p){
int temp,k=n;
for(int i=0;i<p;i++){
temp=pr[k-1];
n=k;
while(n>1){
pr[n-1]=pr[n-2];
n--;
}
pr[0]=temp;
}
}
int main(){
int z,k;
cout<<"Enter size of Array ";
cin>>z;
int arr[z];
cout<<"Enter elements of array ";
for(int i=0;i<z;i++){
cin>>arr[i];
}
cout<<"Enter How Many times you want to rotate ";
cin>>k;
shift(arr,z,k);
cout<<"Rotated Array ";
for(int i=0;i<z;i++){
cout<<arr[i]<<" ";
}

}
2. Write a function which, given an array of integers, returns the integer that appears most frequently in the
array. E.g. for the array [ 1 2 3 2 3 4 2 5 ] your function should return 2.
int count(int pr[],int n){
int freq=0;
int Mfreq=0;
for(int i=0;i<n;i++){
int coun=1;
for(int j=i+1;j<n;j++){
if(pr[i]==pr[j]){
coun++;
}
}
if(coun>freq){
freq=coun;
Mfreq=pr[i];
}
}
return Mfreq;

You might also like