#include<iostream>
using namespace std;
class EmptyClass
{};
class PolymorphicVF
{
virtual void vf(){}
};
class PolymorphicDesstructor
{
virtual ~PolymorphicDesstructor()
{
}
};
class PolymorphicPureDestructor
{
virtual ~PolymorphicPureDestructor()=0;
};
class AbstractClassWithOnePureVF
{
virtual void pvf()=0;
};
class classWithOnepureVFAnd4vf
{
virtual void vf1(){};
virtual void vf2(){};
virtual void pvf()=0;
virtual void vf3(){};
virtual void vf4(){};
};
class Base
{
public:
virtual void vf(){}
};
class SingleDerivedNoOverridevf: public Base
{
};
class SingleDerivedNoOverridevf1: public Base
{
};
class SingleDerivedNoOverridevf2AndOneDM: public Base
{
public:
char c;
};
class SingleDerivedWithOverridevf: virtual public Base
{
public:
void vf(){}
};
class SingleDerivedWithOverridevf1: virtual public Base
{
public:
void vf(){}
};;
class DiamiondProblemWithNoFinalOverrider:public SingleDerivedNoOverridevf, public SingleDerivedNoOverridevf1
{
};
class DiamiondProblemWithFinalOverrider:public SingleDerivedWithOverridevf, public SingleDerivedWithOverridevf1
{
void vf(){}
};
class DiamondDerivedWithThree:public SingleDerivedWithOverridevf, public SingleDerivedWithOverridevf1, public SingleDerivedNoOverridevf1
{
void vf(){}
};
class DerivedWithThreeDifferent:public EmptyClass,public PolymorphicVF,public PolymorphicDesstructor
{};
int main()
{
cout<<"sizeof(EmptyClass)="<<sizeof(EmptyClass)<<endl;//1
cout<<"sizeof(PolymorphicVF)="<<sizeof(PolymorphicVF)<<endl;//8
cout<<"sizeof(PolymorphicDesstructor)="<<sizeof(PolymorphicDesstructor)<<endl;//8
cout<<"sizeof(PolymorphicPureDestructor)="<<sizeof(PolymorphicPureDestructor)<<endl;//8
cout<<"sizeof(AbstractClassWithOnePureVF)="<<sizeof(AbstractClassWithOnePureVF)<<endl;//8
cout<<"sizeof(classWithOnepureVFAnd4vf)="<<sizeof(classWithOnepureVFAnd4vf)<<endl;//8
cout<<"sizeof(Base with one vf)="<<sizeof(Base)<<endl;//8
cout<<"sizeof(SingleDerivedNoOverridevf)="<<sizeof(SingleDerivedNoOverridevf)<<endl;//8
cout<<"sizeof(SingleDerivedNoOverridevf2AndOneDM)="<<sizeof(SingleDerivedNoOverridevf2AndOneDM)<<endl;//16
cout<<"sizeof(SingleDerivedWithOverridevf)="<<sizeof(SingleDerivedWithOverridevf)<<endl;//8
cout<<"sizeof(DiamiondProblemWithNoFinalOverrider)="<<sizeof(DiamiondProblemWithNoFinalOverrider)<<endl;//16
cout<<"sizeof(DiamiondProblemWithFinalOverrider)="<<sizeof(DiamiondProblemWithFinalOverrider)<<endl;//16
cout<<"sizeof(DiamondDerivedWithThree)="<<sizeof(DiamondDerivedWithThree)<<endl;//24
cout<<"sizeof(DerivedWithThreeDifferent)="<<sizeof(DerivedWithThreeDifferent)<<endl;//24
return 0;
}
/*
sizeof(EmptyClass)=1
sizeof(PolymorphicVF)=8
sizeof(PolymorphicDesstructor)=8
sizeof(PolymorphicPureDestructor)=8
sizeof(AbstractClassWithOnePureVF)=8
sizeof(classWithOnepureVFAnd4vf)=8
sizeof(Base with one vf)=8
sizeof(SingleDerivedNoOverridevf)=8
sizeof(SingleDerivedNoOverridevf2AndOneDM)=16
sizeof(SingleDerivedWithOverridevf)=8
sizeof(DiamiondProblemWithNoFinalOverrider)=16
sizeof(DiamiondProblemWithFinalOverrider)=16
sizeof(DiamondDerivedWithThree)=24
sizeof(DerivedWithThreeDifferent)=16
*/
//How to check content of vtable
//compiled with command:
//g++ -fdump-class-hierarchy vtable.cpp
//vtables are taken from generated class table file by above command:
//vtable.cpp.002t.class
Class EmptyClass
size=1 align=1
base size=0 base align=1
EmptyClass (0x0x7fc113139d80) 0 empty
//No vtable for non polymorphic class EmptyClass
Vtable for PolymorphicVF
PolymorphicVF::_ZTV13PolymorphicVF: 3u entries
0 (int (*)(...))0
8 (int (*)(...))(& _ZTI13PolymorphicVF)
16 (int (*)(...))PolymorphicVF::vf
Class PolymorphicVF
size=8 align=8
base size=8 base align=8
PolymorphicVF (0x0x7fc113139de0) 0 nearly-empty
vptr=((& PolymorphicVF::_ZTV13PolymorphicVF) + 16u)
Disadvantages of references in Cpp
References can not be re initialized. They always refer to first referent.
Reference arithmetic is not allowed. Operation effects on actual referent.
#include<iostream>
using namespace std;
int main()
{
int i=10;
int& j = i;
int & k =j;
cout<<"i: "<<i<<endl;
cout<<"j: "<<j<<endl;
cout<<"k: "<<k<<endl;
cout<<"&i: "<<&i<<endl;
cout<<"&j: "<<&j<<endl;
cout<<"&k: "<<&k<<endl;
//reference once initialized can not be reinitialed. point to first referent only.
int m=10;
int n=20;
int &x=m;
cout<<"x="<<x<<endl;//10
x=n;//this is not reassignment. actually its modifying value of m using x. this can be verified taking address of m and x as both would be same.
cout<<"x="<<x<<endl;//20
cout<<"m="<<m<<endl;//m gets modified to 20 now
cout<<"&x="<<&x<<endl;//0x7fff9671caf0
cout<<"&m="<<&m<<endl;//0x7fff9671caf0
//reference arithmatic is not allowed
int a=10;
int b=20;
int& ar=a;
cout<<"a="<<a<<endl;//10
cout<<"ar="<<ar<<endl;//10
ar++;//referant gets incremented not reference
cout<<"a="<<a<<endl;//11
cout<<"ar="<<ar<<endl;//11
return 0;
}
/*
i: 10
j: 10
k: 10
&i: 0x7fffee78603c
&j: 0x7fffee78603c
&k: 0x7fffee78603c
x=10
x=20
m=20
&x=0x7fffee786040
&m=0x7fffee786040
a=10
ar=10
a=11
ar=11
*/
Reference arithmetic is not allowed. Operation effects on actual referent.
#include<iostream>
using namespace std;
int main()
{
int i=10;
int& j = i;
int & k =j;
cout<<"i: "<<i<<endl;
cout<<"j: "<<j<<endl;
cout<<"k: "<<k<<endl;
cout<<"&i: "<<&i<<endl;
cout<<"&j: "<<&j<<endl;
cout<<"&k: "<<&k<<endl;
//reference once initialized can not be reinitialed. point to first referent only.
int m=10;
int n=20;
int &x=m;
cout<<"x="<<x<<endl;//10
x=n;//this is not reassignment. actually its modifying value of m using x. this can be verified taking address of m and x as both would be same.
cout<<"x="<<x<<endl;//20
cout<<"m="<<m<<endl;//m gets modified to 20 now
cout<<"&x="<<&x<<endl;//0x7fff9671caf0
cout<<"&m="<<&m<<endl;//0x7fff9671caf0
//reference arithmatic is not allowed
int a=10;
int b=20;
int& ar=a;
cout<<"a="<<a<<endl;//10
cout<<"ar="<<ar<<endl;//10
ar++;//referant gets incremented not reference
cout<<"a="<<a<<endl;//11
cout<<"ar="<<ar<<endl;//11
return 0;
}
/*
i: 10
j: 10
k: 10
&i: 0x7fffee78603c
&j: 0x7fffee78603c
&k: 0x7fffee78603c
x=10
x=20
m=20
&x=0x7fffee786040
&m=0x7fffee786040
a=10
ar=10
a=11
ar=11
*/
C program to find and replace a string in a sentence
C Program for finding a pattern in a sentence and replace it with another string.
//main string: Manoj Kumar Sahu
//string to be found and replaced: uma
//new string to be replaced with uma: UMA
//resultant string: Manoj KUMAr Sahu
#include<stdio.h>
#include<string.h>
#include<malloc.h>
char* fr(char* str, char* old, char* new)
{
int count=0, i=0;
char* ret;
int lenold = strlen(old);
int lennew = strlen(new);
for(i=0;str[i]!='\0';i++)
{
if(strstr(&str[i],old)==&str[i])
{
count=count+1;
lenold=lenold-1;
}
else{}
}
ret=(char*)malloc(i+count*(lennew-lenold));
i=0;
while(*str)
{
//if(strstr(&str[i],old)=&str[i])//l value required
if(strstr(str,old)==str)
{
//copy
strcpy(&ret[i],new);
i=i+lennew;
str=str+lenold;
}
else
{
ret[i]=*str;
i++;
str++;
}
}
return ret;
}
int main()
{
char str[]="manoj kumar sahu";
char old[]="uma";
char new[]="UMA";
char *result=fr(str,old,new);
printf("resltanc string=%s\n",result);
return 0;
}
//main string: Manoj Kumar Sahu
//string to be found and replaced: uma
//new string to be replaced with uma: UMA
//resultant string: Manoj KUMAr Sahu
#include<stdio.h>
#include<string.h>
#include<malloc.h>
char* fr(char* str, char* old, char* new)
{
int count=0, i=0;
char* ret;
int lenold = strlen(old);
int lennew = strlen(new);
for(i=0;str[i]!='\0';i++)
{
if(strstr(&str[i],old)==&str[i])
{
count=count+1;
lenold=lenold-1;
}
else{}
}
ret=(char*)malloc(i+count*(lennew-lenold));
i=0;
while(*str)
{
//if(strstr(&str[i],old)=&str[i])//l value required
if(strstr(str,old)==str)
{
//copy
strcpy(&ret[i],new);
i=i+lennew;
str=str+lenold;
}
else
{
ret[i]=*str;
i++;
str++;
}
}
return ret;
}
int main()
{
char str[]="manoj kumar sahu";
char old[]="uma";
char new[]="UMA";
char *result=fr(str,old,new);
printf("resltanc string=%s\n",result);
return 0;
}
Multiple Inheritance aptitude questions.Diamond problem
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
};
class C:public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous//candidates are A::f and A::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//A::f
d.C::f();//A::f
//d.D::f();// error: request for member ‘f’ is ambiguous
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
};
class C:virtual public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
d.f();//A::f
d.A::f();//A::f
d.B::f();//A::f
d.C::f();//A::f
d.D::f();//A::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
public:
void f(){cout<<"D::f"<<endl;}
};
int main()
{
D d;
d.f();//D::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//B::f
d.C::f();//C::f
d.D::f();//D::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:virtual public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
public:
void f(){cout<<"D::f"<<endl;}
};
int main()
{
D d;
d.f();//D::f
d.A::f();//A::f
d.B::f();//B::f
d.C::f();//C::f
d.D::f();//D::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous//candidates are A::f and C::f and B::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//B::f
d.C::f();//C::f
//d.D::f();// error: request for member ‘f’ is ambiguous//candidates are A::f C::f and B::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:virtual public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous
d.A::f();//A::f
d.B::f();//B::f
d.C::f();//A::f
//d.D::f();//error: request for member ‘f’ is ambiguous
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous//candidates are B::f and A::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//B::f
d.C::f();//A::f
//d.D::f();// error: request for member ‘f’ is ambiguous//candidates are A::f and B::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:virtual public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
d.f();//B::f
d.A::f();//A::f
d.B::f();//B::f
d.C::f();//A::f
d.D::f();// B::f
cout<<""<<endl;
return 0;
}
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
};
class C:public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous//candidates are A::f and A::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//A::f
d.C::f();//A::f
//d.D::f();// error: request for member ‘f’ is ambiguous
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
};
class C:virtual public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
d.f();//A::f
d.A::f();//A::f
d.B::f();//A::f
d.C::f();//A::f
d.D::f();//A::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
public:
void f(){cout<<"D::f"<<endl;}
};
int main()
{
D d;
d.f();//D::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//B::f
d.C::f();//C::f
d.D::f();//D::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:virtual public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
public:
void f(){cout<<"D::f"<<endl;}
};
int main()
{
D d;
d.f();//D::f
d.A::f();//A::f
d.B::f();//B::f
d.C::f();//C::f
d.D::f();//D::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous//candidates are A::f and C::f and B::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//B::f
d.C::f();//C::f
//d.D::f();// error: request for member ‘f’ is ambiguous//candidates are A::f C::f and B::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:virtual public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous
d.A::f();//A::f
d.B::f();//B::f
d.C::f();//A::f
//d.D::f();//error: request for member ‘f’ is ambiguous
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous//candidates are B::f and A::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//B::f
d.C::f();//A::f
//d.D::f();// error: request for member ‘f’ is ambiguous//candidates are A::f and B::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:virtual public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
d.f();//B::f
d.A::f();//A::f
d.B::f();//B::f
d.C::f();//A::f
d.D::f();// B::f
cout<<""<<endl;
return 0;
}
Remove consecutive repeated characters from a string.
//C program to remove consecutive repeated characters from string.
//C program to remove consecutive repeated elements from an array.
#include <stdio.h>
#include<string.h>
int main()
{
int i,j,len,len1;
char str[100];
printf("Enter a string with repeated adjacent characters:");
gets(str);
len=strlen(str);
//assign 0 to len1 - length of removed characters
len1=0;
//Removing consecutive repeated characters from string
for(i=0; i<(len-len1);)
{
if(str[i]==str[i+1])
{
//shift all characters
for(j=i;j<(len-len1);j++)
str[j]=str[j+1];
len1++;
}
else
{
i++;
}
}
printf("String after removing duplicate adjacent elements : %s\n",str);
return 0;
}
/*
Enter a string with repeated adjacent characters:abbcccdefabc
String after removing duplicate adjacent elements : abcdefabc
*/
//Removing consecutive members in an array
#include<iostream>
using namespace std;
class Base
{
};
void print(int* p)
{
while(*p)
cout<<*p++<<" ";
cout<<endl;
};
int main()
{
cout<<""<<endl;
int arr[]={1,2,2,3,3,2,4,5,5,55,555,5};
print(arr);
int size=sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;)
{
if(arr[j]==arr[j+1])
{
for(int k=j;k<size;k++)
{
arr[k]=arr[k+1];
}
//shift
size--;
}
else
{
//check next
j++;
}
}
}
print(arr);
return 0;
}
/*
1 2 2 3 3 2 4 5 5 55 555 5
1 2 3 2 4 5 55 555 5
*/
//C program to remove consecutive repeated elements from an array.
#include <stdio.h>
#include<string.h>
int main()
{
int i,j,len,len1;
char str[100];
printf("Enter a string with repeated adjacent characters:");
gets(str);
len=strlen(str);
//assign 0 to len1 - length of removed characters
len1=0;
//Removing consecutive repeated characters from string
for(i=0; i<(len-len1);)
{
if(str[i]==str[i+1])
{
//shift all characters
for(j=i;j<(len-len1);j++)
str[j]=str[j+1];
len1++;
}
else
{
i++;
}
}
printf("String after removing duplicate adjacent elements : %s\n",str);
return 0;
}
/*
Enter a string with repeated adjacent characters:abbcccdefabc
String after removing duplicate adjacent elements : abcdefabc
*/
//Removing consecutive members in an array
#include<iostream>
using namespace std;
class Base
{
};
void print(int* p)
{
while(*p)
cout<<*p++<<" ";
cout<<endl;
};
int main()
{
cout<<""<<endl;
int arr[]={1,2,2,3,3,2,4,5,5,55,555,5};
print(arr);
int size=sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;)
{
if(arr[j]==arr[j+1])
{
for(int k=j;k<size;k++)
{
arr[k]=arr[k+1];
}
//shift
size--;
}
else
{
//check next
j++;
}
}
}
print(arr);
return 0;
}
/*
1 2 2 3 3 2 4 5 5 55 555 5
1 2 3 2 4 5 55 555 5
*/
C program to find only unique elements of array
//C++ Program to find only unique elements in array of integers.
using namespace std;
#include <stdio.h>
#include <string.h>
//#include <stdlib.h>
int main() {
int arr[20], i, j, k, size;
printf("\nEnter the size of the array : ");
scanf("%d", &size);
printf("\nEnter the Numbers in array : ");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("\nArray with Unique elements is : ");
for (i = 0; i < size; i++) {
for (j = i + 1; j < size;) {
if (arr[j] == arr[i]) {
for (k = j; k < size; k++) {
arr[k] = arr[k + 1];
}
size--;
} else
{
j++;
}
}
}
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return (0);
}
/*
Enter the size of the array : 8
Enter the Numbers in array :
1
2
2
3
4
2
3
1
Array with Unique elements is : 1 2 3 4
*/
//C Program to remove all duplicate elements from an array
#include<iostream>
using namespace std;
class Base
{
};
void print(int* p)
{
while(*p)
cout<<*p++<<" ";
cout<<endl;
};
int main()
{
cout<<""<<endl;
int arr[]={1,2,2,3,3,2,4,5,5,55,555,5};
print(arr);
int size=sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;)
{
if(arr[i]==arr[j])
{
for(int k=j;k<size;k++)
{
arr[k]=arr[k+1];
}
//shift
size--;
}
else
{
//check next
j++;
}
}
}
print(arr);
return 0;
}
/*
1 2 2 3 3 2 4 5 5 55 555 5
1 2 3 4 5 55 555
*/
using namespace std;
#include <stdio.h>
#include <string.h>
//#include <stdlib.h>
int main() {
int arr[20], i, j, k, size;
printf("\nEnter the size of the array : ");
scanf("%d", &size);
printf("\nEnter the Numbers in array : ");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("\nArray with Unique elements is : ");
for (i = 0; i < size; i++) {
for (j = i + 1; j < size;) {
if (arr[j] == arr[i]) {
for (k = j; k < size; k++) {
arr[k] = arr[k + 1];
}
size--;
} else
{
j++;
}
}
}
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return (0);
}
/*
Enter the size of the array : 8
Enter the Numbers in array :
1
2
2
3
4
2
3
1
Array with Unique elements is : 1 2 3 4
*/
//C Program to remove all duplicate elements from an array
#include<iostream>
using namespace std;
class Base
{
};
void print(int* p)
{
while(*p)
cout<<*p++<<" ";
cout<<endl;
};
int main()
{
cout<<""<<endl;
int arr[]={1,2,2,3,3,2,4,5,5,55,555,5};
print(arr);
int size=sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;)
{
if(arr[i]==arr[j])
{
for(int k=j;k<size;k++)
{
arr[k]=arr[k+1];
}
//shift
size--;
}
else
{
//check next
j++;
}
}
}
print(arr);
return 0;
}
/*
1 2 2 3 3 2 4 5 5 55 555 5
1 2 3 4 5 55 555
*/
CMakeLists.txt and cmake interview questions
What is a cmake?
cmake is a cross platform open source software for managing build system of a software. It generates native makefile which is the recipe file for native make tool to create any application in Linux. It is also used with other native build environments such as make Apple's Xcode, and Microsoft Visual Studio.
It supports directory hierarchies. Each directory contains a CMakeLists.txt file.
It supports applications that depends on multiple libraries.
What is CMakeLists.txt?
CMakeLists.txt file contains instruction to "cmake" tool to generate a native Makefile which the instructions file for "make" to create an application.
A Simple multi folder camke project example say MyProject.Simple cmake demo example can be found here.
MyProject
├── CMakeLists.txt
├── myapp
│ ├── CMakeLists.txt
│ └── main.cpp
└── mylibs
├── CMakeLists.txt
├── mylib.cpp
└── mylib.h
Top most MyProject/CMakeList.txt
cmake_minimum_required(VERSION 2.8)
add_subdirectory(mylibs)
add_subdirectory(myapp)
myapp/CMakeLists.txt
add_executable(app main.cpp)
target_link_libraries(app mylib)
myapp/main.cpp
#include<iostream>
using namespace std;
#include "../mylibs/mylib.h"
int main()
{
cout<<"main"<<endl;
libfun1();
libfun2();
return 0;
}
mylibs/CMakeLists.txt
add_library(mylib mylib.cpp)
mylib/mylib.cpp
#include<iostream>
using namespace std;
void libfun1()
{
cout<<"libfun1"<<endl;
}
void libfun2()
{
cout<<"libfun2"<<endl;
}
mylib/mylib.h
void libfun1();
void libfun2();
How to build in a separate direcory
cd MyProject
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX:PATH=/tmp/foo
make
make install.
Summary:
Using CMake with executables
add_executable(myapp main.c)
Using CMake with static libraries
add_library(test STATIC test.c)
Using CMake with dynamic libraries
add_library(test SHARED test.c)
Linking libraries to executables with CMake
add_subdirectory(libtest_project)
add_executable(myapp main.c)
target_link_libraries(myapp test)
cmake is a cross platform open source software for managing build system of a software. It generates native makefile which is the recipe file for native make tool to create any application in Linux. It is also used with other native build environments such as make Apple's Xcode, and Microsoft Visual Studio.
It supports directory hierarchies. Each directory contains a CMakeLists.txt file.
It supports applications that depends on multiple libraries.
What is CMakeLists.txt?
CMakeLists.txt file contains instruction to "cmake" tool to generate a native Makefile which the instructions file for "make" to create an application.
A Simple multi folder camke project example say MyProject.Simple cmake demo example can be found here.
MyProject
├── CMakeLists.txt
├── myapp
│ ├── CMakeLists.txt
│ └── main.cpp
└── mylibs
├── CMakeLists.txt
├── mylib.cpp
└── mylib.h
Top most MyProject/CMakeList.txt
cmake_minimum_required(VERSION 2.8)
add_subdirectory(mylibs)
add_subdirectory(myapp)
myapp/CMakeLists.txt
add_executable(app main.cpp)
target_link_libraries(app mylib)
myapp/main.cpp
#include<iostream>
using namespace std;
#include "../mylibs/mylib.h"
int main()
{
cout<<"main"<<endl;
libfun1();
libfun2();
return 0;
}
mylibs/CMakeLists.txt
add_library(mylib mylib.cpp)
mylib/mylib.cpp
#include<iostream>
using namespace std;
void libfun1()
{
cout<<"libfun1"<<endl;
}
void libfun2()
{
cout<<"libfun2"<<endl;
}
mylib/mylib.h
void libfun1();
void libfun2();
How to build in a separate direcory
cd MyProject
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX:PATH=/tmp/foo
make
make install.
Summary:
Using CMake with executables
add_executable(myapp main.c)
Using CMake with static libraries
add_library(test STATIC test.c)
Using CMake with dynamic libraries
add_library(test SHARED test.c)
Linking libraries to executables with CMake
add_subdirectory(libtest_project)
add_executable(myapp main.c)
target_link_libraries(myapp test)
Subscribe to:
Posts (Atom)