ย Codding/Programming Notes

Hello World.cpp

#include<iostream>

using namespace std;

int main()

{

ย ย ย ย cout<<"Hello world\n";

ย ย ย ย return 0;

}

Output :ย 

Hello World

Arithmetic operator.cpp

#include <iostream>

using namespace std;

int main()

{

ย ย ย ย int first, second, add, subtract, multiply;

ย ย ย ย float divide;

ย 

ย ย ย ย cout<<"Enter two integers\n";

ย ย ย ย cin>>first>>second;

ย 

ย ย ย ย add = first + second;

ย ย ย ย subtract = first - second;

ย ย ย ย multiply = first * second;

ย ย ย ย divide = first / (float)second;

//typecasting

ย 

cout<<"Sum = "<<add<<"\n";

cout<<"Difference ="<<subtract<<"\n";

cout<<"Multiplication ="<<multiply<<"\n";

cout<<"Division ="<<divide<<"\n";

ย 

return 0;

}

output :

Enter two integers

1

3

Sum = 4

Difference =-2

Multiplication =3

Division =0.333333

If statement.cpp

#include <iostream>

using namespace std;


int main ()

{

int a = 10;


// check for condition

if( a > 5 )

{

// if condition is true then print the following


cout<< " if Condition is satisfied";

cout << " a is greater than 5 " << endl;

}

cout << " value of a is : " << a << endl;


return 0;

}

Output :

ย if Condition is satisfied a is greater than 5

ย value of a is : 10

If Else Statement.cpp

#include <iostream>


using namespace std;


int main ()

{


int a = 10;


// check for condition

if( a > 20 )

{

// if condition is true then print the following


cout<< " If Condition is satisfied ";

cout << "a is greater than 20" << endl;

}

else

{

// if condition is false then print the following


cout<< " If Condition is not satisfied ";

cout << "a is not greater than 20" << endl;

}


cout << "value of a is : " << a << endl;


return 0;

}

Output :

ย If Condition is not satisfied a is not greater than 20

value of a is : 10

Nested If Statement.cpp

#include <iostream>

using namespace std;


int main ()

{

// local variable declaration:

int a = 10;

int b = 20;

int c = 15;


// check for condition

if( a < c )

{

// if condition is true then check the following


if( b > c )

{

// if condition is true then print the following


cout << "value of c is greater than a but less than b " << endl;

}

}


cout << "value of a is : " << a << endl;

cout << "value of b is : " << b << endl;

cout << "value of c is : " << c << endl;


return 0;

}

Output :

value of c is greater than a but less than b

value of a is : 10

value of b is : 20

value of c is : 15

Nested If Else Statement.cpp

#include <iostream>

using namespace std;

int main()

{

int marks;

cout<<"Enter your marks :";

cin>>marks;


if(marks>100)

/*marks greater than 100*/

cout<<"Not valid marks";


else if(marks>=80)

/*marks between 80 & 99*/

cout<<"your grade is A";


else if(marks >=70)

/*marks between 70 & 79*/

cout<<"your grade is B";


else if(marks>=50)

/*marks between 50 & 69*/

cout<<"your grade is C";


else if(marks>=35)

/*marks between 35 & 49*/

cout<<"your grade is D";


else

/*marks less than 35*/

cout<<"your grade is E";

}

Output :

Enter your marks :

98

your grade is A

For Loop.cpp

#include <iostream>

using namespace std;


int main ()

{

// Local variable declaration:

int a = 1;


// while loop execution

for(a=1; a<6; a++) {

cout <<"value of a: " << a << endl;

}


return 0;

}

Output :

value of a: 1

value of a: 2

value of a: 3

value of a: 4

value of a: 5

While loop.cpp

#include <iostream>

using namespace std;


int main ()

{

// Local variable declaration:

int a = 1;


// while loop execution

while( a < 6 );

{

cout <<"value of a: " << a << endl;

a = a + 1;

}


return 0;

}

Output :ย 

value of a: 1

value of a: 2

value of a: 3

value of a: 4

value of a: 5

Do-While Loop.cpp



#include <iostream>

using namespace std;


int main ()

{

// Local variable declaration:

int a = 1;


// do loop execution

do

{

cout <<"value of a: " << a << endl;

a = a + 1;

}while( a < 6 );


return 0;

}

Output :ย 

value of a: 1

value of a: 2

value of a: 3

value of a: 4

value of a: 5

Fibonacci Series.cpp

#include <iostream>

using namespace std;

int main()

{

int n, first = 0, second = 1, next, c;


cout<<"Enter the number of terms\n";

cin>>n;


cout<<"First"<<n<<"terms of Fibonacci series are :-\n";


for ( c = 0 ; c < n ; c++ )

{

if ( c <= 1 )

next = c;

else

{

next = first + second;

first = second;

second = next;

/*replaced first no by second & second by addition of first & second */

}

cout<<next<<"\n";

}


return 0;

}

Output :

ย Enter the number of termsย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

5ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

First 5 terms of Fibonacci series are :-ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

0ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

3

Array.cpp

#include <iostream>

using namespace std;


#include <iomanip>

using std::setw;


int main ()

{

int n[ 15 ]; //array size is 15


// initialize elements of array n to 0

for ( int i = 1; i <= 10; i++ )

{

n[ i ] = i * 2; // Twice the element

}


cout << "\t Table of Two" << endl;


// output each array element's value

for ( int j = 1; j <= 10; j++ )

{

cout << setw( 7 )<<"2*"<< j<< "="<< setw(10) << n[ j ] << endl;

//setw used for formatting output

}


return 0;

}

Output :ย 

Table of 2

2*1= 2

2*2= 4

2*3= 6

2*4= 8

2*5= 10

2*6= 12

2*7 =14

2*8 =16

2*9= 18

2*10= 20

2D Array.cpp

#include <iostream>

#define size 16

using namespace std;


int main ()

{

int m,n;

int a[size][size];

cout<<"Enter the number of rows"<<endl;

cin>>m;

cout<<"Enter the number of columns"<<endl;

cin>>n;


cout<<"Enter the Elements in Table"<<endl;

for ( int i = 0; i < m; i++ )

{

for ( int j = 0; j < n; j++ )

{

cin >>a[i][j];

}

}

// output each array element"s value

for ( int i = 0; i < m; i++ )

for ( int j = 0; j < n; j++ )

{

cout << "a[" << i << "][" << j << "]: ";

cout << a[i][j]<< endl;

}


return 0;

}

Output :

Enter the number of rows

2

Enter the number of columns

3

Enter the Elements in Table

1 2 3 4 5 6

a[0][0]: 1

a[0][1]: 2

a[0][2]: 3

a[1][0]: 4

a[1][1]: 5

a[1][2]: 6

Min number in Array.cpp

#include <iostream>

using namespace std;

int main()

{

int array[100], minimum, size, c, location = 1;


cout<<"Enter the number of elements in array\n";

cin>>size;


cout<<"Enter"<<size<<"integers\n";


for ( c = 0 ; c < size ; c++ )

cin>>array[c];


minimum = array[0];

/*assuming first no. as minimum*/


for ( c = 1 ; c < size ; c++ )

{

if ( array[c] < minimum )

{

minimum = array[c];

location = c+1;

/*If any no. lesser than maximum found then set it as minimum and save its location */

}

}


cout<<"Minimum element is present at location"<< location<<"and it's value is"<<minimum<<"\n";

return 0;

}

Output :

Enter the number of elements in arrayย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย 

4ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter4integersย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

ย 8 7 6 5ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Minimum element is present at location4and it's value is 5

Max number in Array.cpp

#include <iostream>

using namespace std;

int main()

{

int array[100], maximum, size, c, location = 1;


cout<<"Enter the number of elements in array\n";

cin>>size;


cout<<"Enter"<<size<<"integers\n";


for (c = 0; c < size; c++)

cin>>array[c];


maximum = array[0];

/*assuming first no. as maximum*/


for (c = 1; c < size; c++)

{

if (array[c] > maximum)

{

maximum = array[c];

location = c+1;

/*If any no. greater than maximum found then set it as maximum and save its location */

}

}


cout<<"Maximum element is present at location"<<location<<"and it's value is"<<maximum<<"\n";

return 0;

}

Output :

Enter the number of elements in arrayย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย 

4ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter4integersย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

8 7 6 5ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Maximum element is present at location1and it's value is 8


Inserting numbers in Array

#include <iostream>

using namespace std;

int main()

{

int array[100], position, c, n, value;


cout<<"Enter number of elements in array\n";

cin>>n;


cout<<"Enter"<<n<<"elements\n";

for (c = 0; c < n; c++)

cin>>array[c];


cout<<"Enter the location where you wish to insert an element\n";

cin>>position;


cout<<"Enter the value to insert\n";

cin>>value;


for (c = n - 1; c >= position - 1; c--)

array[c+1] = array[c];

/* from given position shifting all elements by 1 position */


array[position-1] = value;

/*inserting value to given position*/


cout<<"Resultant array is\n";

for (c = 0; c <= n; c++)

cout<<array[c]<<"\n";

return 0;

}

Output :

Enter number of elements in arrayย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

4ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter 4 elementsย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

6 7 8 9ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter the location where you wish to insert an elementย  ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter the value to insertย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

5ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Resultant array isย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

5ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

6ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

7ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

8ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

9

Merge Arrays.cpp

#include <iostream>

using namespace std;

int merge(int [], int, int [], int, int []);


int main() {

int a[100], b[100], m, n, c, sorted[200];


cout<<"Input number of elements in first array\n";

cin>>m;


cout<<"Input"<<m<<"integers\n";

for (c = 0; c < m; c++) {

cin>>a[c];

}


cout<<"Input number of elements in second array\n";

cin>>n;


cout<<"Input"<<n<<"integers\n";

for (c = 0; c < n; c++) {

cin>>b[c];

}


merge(a, m, b, n, sorted);


cout<<"Sorted array:\n";


for (c = 0; c < m + n; c++) {

cout<<sorted[c]<<"\n";

}


return 0;

}

int merge(int a[], int m, int b[], int n, int sorted[]) {

int i, j, k;

j = k = 0;

for (i = 0; i < m + n;) {

if (j < m && k < n) {

if (a[j] < b[k]) {

sorted[i] = a[j];

j++;

}

else {

sorted[i] = b[k];

k++;

}

/*if element of a array is less than b then push it in resultant array else push element of b in array*/

i++;

}

else if (j == m) {

for (; i < m + n;) {

sorted[i] = b[k];

k++;

i++;

/*if array a is finished push all the elements of b in resultant array*/

}

}

else {

for (; i < m + n;) {

sorted[i] = a[j];

j++;

i++;

/*if array b is finished push all the elements of a in resultant array*/

}

}

}

}

Output :

Input number of elements in first arrayย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

3ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Input 3 integers ย  ย  ย  ย  ย  ย  ย  ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

6 5 4ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Input number of elements in second arrayย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

3ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Input 3 integersย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

3 2 1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Sorted array:ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

3ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

6ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

5ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

4

Passing Array to functions.cpp

#include <iostream>

using namespace std;


// function declaration:

double getAverage(int arr[], int size);


int main ()

{

// an int array with 8 ages of people

int balance[8] = {18,9,51,33,15,24,67,54};

double avg;


// pass pointer to the array as an argument.

avg = getAverage( balance, 8 ) ;


// output the returned value

cout << "Average of all ages is : " << avg << endl;


return 0;

}


double getAverage(int arr[], int size)

{

int i, sum = 0;

double avg;


for (i = 0; i < size; ++i)

{

sum += arr[i];

}


avg = double(sum) / size;


return avg;

}

Output :

Average of all ages is : 33.875ย 

Reverse Array.cpp

#include <iostream>

using namespace std;

int main()

{

int n, c, d, a[100], b[100];

ย 

cout<<"Enter the number of elements in array\n";

cin>>n;

ย 

cout<<"Enter the array elements\n";

ย 

for (c = 0; c < n ; c++)

cin>>a[c];

ย 

/* Copying elements into array b starting from end of array a */

ย 

for (c = n - 1, d = 0; c >= 0; c--, d++)

b[d] = a[c];

ย 

/* Copying reversed array into original.

* Here we are modifying original array, this is optional.*/

ย 

for (c = 0; c < n; c++)

a[c] = b[c];

cout<<"Reverse array is\n";

ย 

for (c = 0; c < n; c++)

cout<<a[c]<<"\n";

return 0;

}

Output :

Enter the number of elements in arrayย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

4ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter the array elementsย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

ย 4 5 6 7 ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Reverse array isย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

7ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

6ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

5ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

4

Reverse num using Array.cpp

#include <iostream>

using namespace std;

int main()

{

int n, array[100], i;

cout<<"Enter no of digits in your number";

cin>>n;


cout<<"Enter no.";

for(i=0,i < n;i++)

cin>>a[i];

/*storing each no in array*/


cout<<"Reverse no is : ";

for(i=n-1,i > =0;i--)

cout<<a[i];

/*Printing array in reverse order*/

}

Output :

ย Enter no of digits in your number 2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter no.5 7 ย  ย  ย  ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Reverse no is : 7 5

Return Array from pointer.cpp

#include <iostream>


using namespace std;


int * getElements( )

{

static int r[5];

for (int i = 0; i < 5; ++i)

{

cout << "Enter the element at " << i << " : " ;

cin >> r[i] ;

}

for (int i = 0; i < 5; ++i)

{

cout << r[i] << endl;

}


return r;

}


// main function to call above defined function.

int main ()

{

// a pointer to an int.

int *p;


p = getElements();

for ( int i = 0; i < 5; i++ )

{

cout << "*(p + " << i << ") : ";

cout << *(p + i) << endl;

}

Output :

ย 

Enter the element at 0 : 1

Enter the element at 1 : 2

Enter the element at 2 : 3

Enter the element at 3 : 4

Enter the element at 4 : 5

1

2

3

4

5

*(p + 0 ) : 1

*(p + 1 ) : 2

*(p + 2 ) : 3

*(p + 3 ) : 4

*(p + 4 ) : 5


ย Array of pointer.cpp

#include <iostream>

using namespace std;

using namespace std;

const int MAX = 5;


int main ()

{

int arr[MAX] = {10,20,30,40,50};

int *ptr[MAX];


for (int i = 0; i < MAX; i++)

{

ptr[i] = &arr[i]; // assign the address of integer.

}

for (int i = 0; i < MAX; i++)

{

cout << "Value of var[" << i << "] = " << *ptr[i] << endl;

}

return 0;

}

Output :

Value of var[0] = 10

Value of var[1] = 20

Value of var[2] = 30

Value of var[3] = 40

Value of var[4] = 50


pointer to Array.cpp

#include <iostream>

using namespace std;


int main ()

{

// an array of 4 elements.

double balance[4] = {18,9,51,33};

double *p;


p = balance;


// output each array element's value

cout << "Using Pointer array values are as follows " << endl;

for ( int i = 0; i < 4; i++ )

{

cout << "*(p + " << i << ") : ";

cout << *(p + i) << endl;

}


cout << "Using Balance array values are as follows" << endl;

for ( int i = 0; i < 4; i++ )

{

cout << "*(balance + " << i << ") : ";

cout << *(balance + i) << endl;

}


return 0;

}

Output :

ย 

Array values using pointer

*(p + 0) : 18

*(p + 1) : 9

*(p + 2) : 51

*(p + 3) : 33

Array values using balance as address

*(balance + 0) : 18

*(balance + 1) : 9

*(balance + 2) : 51

*(balance + 3) : 33

Bubble sort

#include <iostream>

using namespace std;

void bubble_sort(long [], long);


int main()

{

long array[100], n, c, d, swap;


cout<<"Enter number of elements\n";

cin>>n;


cout<<"Enter"<<n<<"integers\n";


for (c = 0; c < n; c++)

cin>>array[c];


bubble_sort(array, n);


cout<<"Sorted list in ascending order:\n";


for ( c = 0 ; c < n ; c++ )

cout<<array[c]<<"\n";


return 0;

}


void bubble_sort(long list[], long n)

{

long c, d, t;


for (c = 0 ; c < ( n - 1 ); c++)

{

for (d = 0 ; d < n - c - 1; d++)

{

if (list[d] > list[d+1])

{

/* Swapping */

t = list[d];

list[d] = list[d+1];

list[d+1] = t;

}

}

}

}

Output :

Enter number of elementsย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

5ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter 5 integersย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

8 4 66 23 56ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Sorted list in ascending order:ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

4ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

8ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

23ย  ย  ย  ย  ย  ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

56ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

66

Selection Sort.cpp

#include <iostream>

using namespace std;

int main()

{

int array[100], n, c, d, position, swap;


cout<<"Enter number of elements\n";

cin>>n;


cout<<"Enter"<<n<<"integers\n";


for ( c = 0 ; c < n ; c++ )

cin>>array[c];


for ( c = 0 ; c < ( n - 1 ) ; c++ )

{

position = c;

/* for all array, from position c selecting smallest element in array and swap with c position*/

for ( d = c + 1 ; d < n ; d++ )

{

if ( array[position] > array[d] )

position = d;

}

if ( position != c )

{

swap = array[c];

array[c] = array[position];

array[position] = swap;

}

}


cout<<"Sorted list in ascending order:\n";

for ( c = 0 ; c < n ; c++ )

cout<<array[c]<<"\n";


return 0;

}

Output :

Enter number of elementsย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

5ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter 5 integers ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

4 88 47 87 13ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Sorted list in ascending order:ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

4ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

13ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

47ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

87ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

88

Insertion sort


#include <iostream>

using namespace std;

int main()

{

int n, array[1000], c, d, t;


cout<<"Enter number of elements\n";

cin>>n;


cout<<"Enter"<<n<<"integers\n";


for (c = 0; c < n; c++) {

cin>>array[c];

}


for (c = 1 ; c <= n - 1; c++) {

d = c;


/* positioning element at c, by this way all the elements in ascending order */

while ( d > 0 && array[d] < array[d-1]) {

t = array[d];

array[d] = array[d-1];

array[d-1] = t;

d--;

}

}


cout<<"Sorted list in ascending order:\n";


for (c = 0; c <= n - 1; c++) {

cout<<array[c]<<"\n";

}


return 0;

}


Output:


Enter number of elementsย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

5ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter5integersย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2 4 66 11 1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Sorted list in ascending order:ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

4ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

11ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

66ย 

Merge sort


/* C++ program for Merge Sort */


#include<iostream>


using namespace std;


// Merges two subarrays of arr[].


// First subarray is arr[l..m]


// Second subarray is arr[m+1..r]


void merge(int arr[], int l, int m, int r) {


int i, j, k;


int n1 = m - l + 1;


int n2 = r - m;


/* create temp arrays */


int L[n1], R[n2];


/* Copy data to temp arrays L[] and R[] */


for (i = 0; i < n1; i++)


L[i] = arr[l + i];


for (j = 0; j < n2; j++)


R[j] = arr[m + 1+ j];


/* Merge the temp arrays back into arr[l..r]*/


i = 0;


// Initial index of first subarray


j = 0;


// Initial index of second subarray


k = l;


// Initial index of merged subarray


while (i < n1 && j < n2) {


if (L[i] <= R[j]) {


arr[k] = L[i]; i++;


}


else { arr[k] = R[j]; j++;


}


k++;


}


/* Copy the remaining elements of L[], if there are any */


while (i < n1) {


arr[k] = L[i]; i++; k++;


}


/* Copy the remaining elements of R[], if there are any */


while (j < n2) { arr[k] = R[j]; j++; k++;


}


}


/* l is for left index and r is right index of the sub-array of arr to be sorted */


void mergeSort(int arr[], int l, int r) {


if (l < r) {


// Same as (l+r)/2, but avoids overflow for


// large l and h


int m = l+(r-l)/2;


// Sort first and second halves


mergeSort(arr, l, m);


mergeSort(arr, m+1, r);


merge(arr, l, m, r);


}


}


/* UTILITY FUNCTIONS */


/* Function to print an array */


void printArray(int A[], int size) {


int i;


for (i=0; i < size; i++)


cout<<A[i];


cout<<"\n";


}


/* Driver program to test above functions */


int main() {


int arr[] = {12, 11, 13, 5, 6, 7};


int arr_size = sizeof(arr)/sizeof(arr[0]);


cout<<"Given array is \n";


printArray(arr, arr_size);


mergeSort(arr, 0, arr_size - 1);


cout<<"\nSorted array is \n";


printArray(arr, arr_size);


return 0;


}

Output:


Given array is 1 11 3 5 6 7


Sorted array is 1 3 5 6 7 11

Quick sort


/* C++ implementation QuickSort */


#include<iostream>


using namespace std;


// A utility function to swap two elements


void swap(int* a, int* b) {


int t = *a;


*a = *b;


*b = t;


}


/* This function takes last element as pivot, places the pivot element at its correct position in sorted array, and places all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot */


int partition (int arr[], int low, int high) {


int pivot = arr[high];


// pivot int i = (low - 1);


// Index of smaller element


for (int j = low; j <= high- 1; j++) {


// If current element is smaller than or


// equal to pivot


if (arr[j] <= pivot) {


i++;


// increment index of smaller element


swap(&arr[i], &arr[j]);


} }


swap(&arr[i + 1], &arr[high]);


return (i + 1);


}


/* The main function that implements QuickSort arr[] --> Array to be sorted, low --> Starting index, high --> Ending index */


void quickSort(int arr[], int low, int high) {


if (low < high) {


/* pi is partitioning index, arr[p] is now at right place */


int pi = partition(arr, low, high);


// Separately sort elements before


// partition and after partition


quickSort(arr, low, pi - 1);


quickSort(arr, pi + 1, high);


} }


/* Function to print an array */


void printArray(int arr[], int size) {


int i; for (i=0; i < size; i++)


cout<<"%d ", arr[i]; cout<<"n";


}


// Driver program to test above functions


int main() {


int arr[] = {10, 7, 8, 9, 1, 5};


int n = sizeof(arr)/sizeof(arr[0]);


quickSort(arr, 0, n-1);


cout<<"Sorted array: n";


printArray(arr, n);


return 0; }

Output:


Sorted array: 1 5 7 8 9 10

Matrix Addition


#include <iostream>

using namespace std;

int main()

{

int m, n, c, d, first[10][10], second[10][10], sum[10][10];


cout<<"Enter the number of rows and columns of matrix\n";


cin>>m>>n;

cout<<"Enter the elements of first matrix\n";


for ( c = 0 ; c < m ; c++ )

for ( d = 0 ; d < n ; d++ )

cin>>first[c][d];


cout<<"Enter the elements of second matrix\n";


for ( c = 0 ; c < m ; c++ )

for ( d = 0 ; d < n ; d++ )

cin>>second[c][d];


for ( c = 0 ; c < m ; c++ )

for ( d = 0 ; d < n ; d++ )

sum[c][d] = first[c][d] + second[c][d];

/* Matrix addition */


cout<<"Sum of entered matrices:-\n";


for ( c = 0 ; c < m ; c++ )

{

for ( d = 0 ; d < n ; d++ )

cout<<sum[c][d]<<"\t";


cout<<"\n";

}


return 0;

}

Output:


Enter the number of rows and columns of matrixย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter the elements of first matrixย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1 1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1 1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter the elements of second matrixย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2 2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2 2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Sum of entered matrices:-ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

3 ย  ย  ย  3ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

3 ย  ย  ย  3ย ย 

Matrix Subtraction


#include <iostream>

using namespace std;

int main()

{

int m, n, c, d, first[10][10], second[10][10], difference[10][10];


cout<<"Enter the number of rows and columns of matrix\n";

cin>>m>>n;

cout<<"Enter the elements of first matrix\n";


for (c = 0; c < m; c++)

for (d = 0 ; d < n; d++)

cin>>first[c][d];


cout<<"Enter the elements of second matrix\n";


for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

cin>>second[c][d];


for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

difference[c][d] = first[c][d] - second[c][d];

/* Subtract Matrices*/


cout<<"difference of entered matrices:-\n";


for (c = 0; c < m; c++)

{

for (d = 0; d < n; d++)

cout<<difference[c][d]<<"\t";


cout<<"\n";

} return 0; }

Output:


Enter the number of rows and columns of matrixย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter the elements of first matrixย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1 1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1 1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter the elements of second matrixย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2 2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2 2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

difference of entered matrices:-ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

-1 ย  ย  ย  -1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

-1 ย  ย  ย  -1ย 

Matrix Multiplication


#include <iostream>

using namespace std;

int main()

{

int m, n, p, q, c, d, k, sum = 0;

int first[10][10], second[10][10], multiply[10][10];


cout<<"Enter the number of rows and columns of first matrix\n";

cin>>m>>n;

cout<<"Enter the elements of first matrix\n";


for ( c = 0 ; c < m ; c++ )

for ( d = 0 ; d < n ; d++ )

cin<<first[c][d];


cout<<"Enter the number of rows and columns of second matrix\n";

cin>>p>>q;


if ( n != p )

cout<<"Matrices with entered orders can't be multiplied with each other.\n";

else

{

cout<<"Enter the elements of second matrix\n";


for ( c = 0 ; c < p ; c++ )

for ( d = 0 ; d < q ; d++ )

cin>>second[c][d];


for ( c = 0 ; c < m ; c++ )

{

for ( d = 0 ; d < q ; d++ )

{

for ( k = 0 ; k < p ; k++ )

{

sum = sum + first[c][k]*second[k][d];

}


multiply[c][d] = sum;

sum = 0;

}

}


cout<<"Product of entered matrices:-\n";


for ( c = 0 ; c < m ; c++ )

{

for ( d = 0 ; d < q ; d++ )

cout<<multiply[c][d]<<"\t";


cout<<"\n";

}

}


return 0;

}


Output:


Enter the number of rows and columns of matrixย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter the elements of first matrixย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1 1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1 1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter the elements of second matrixย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2 2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2 2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

difference of entered matrices:-ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

4ย  ย  ย  4ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

4 ย  ย  ย  4ย 

Transpose Matrix


#include <iostream>

using namespace std;

int main()

{

int m, n, c, d, matrix[10][10], transpose[10][10];

cout<<"Enter the number of rows and columns of matrix ";

cin>>m>>n; cout<<"Enter the elements of matrix \n";

for( c = 0 ; c < m ; c++ )

{

for( d = 0 ; d < n ; d++ )

{

cin>>matrix[c][d];

}

}


for( c = 0 ; c < m ; c++ )

{

for( d = 0 ; d < n ; d++ )

{

transpose[d][c] = matrix[c][d];

/* transpose by interchanging rows and columns */

}

}


cout<<"Transpose of entered matrix :-\n";

for( c = 0 ; c < n ; c++ )

{

for( d = 0 ; d < m ; d++ )

{

cout<<transpose[c][d]<<"\t";

}

cout<<"\n";

}

return 0;

}

Output:


Enter the number of rows and columns of matrix 2 2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter the elements of matrixย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1 2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

3 4ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Transpose of entered matrix :-ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1 ย  ย  ย  3ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2 ย  ย  ย  4ย 

Matrix Addition using classes


#include<iostream>


using namespace std;


class Add{


public: void sum(int r, int c){


int m1[r][c], m2[r][c], s[r][c];


cout << "Enter the elements of first 1st matrix: ";


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


for (int j = 0;j < c;j++ ) {


cin>>m1[i][j]; }


}


cout << "Enter the elements of second 2nd matrix: ";


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


for (int j = 0;j<c;j++ ) {


cin>>m2[i][j]; }


}


cout<<"Output: ";


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


for (int j = 0;j<c;j++ ) {


s[i][j]=m1[i][j]+m2[i][j];


cout<<s[i][j]<<" ";


} } }


};


int main(){


int row, col;


cout<<"Enter the number of rows(should be >1 and <10): ";


cin>>row;


cout<<"Enter the number of column(should be >1 and <10): ";


cin>>col;


Add obj;


obj.sum(row, col);


return 0;


}


Output:


Enter the number of rows(should be >1 and <10): 2


Enter the number of column(should be >1 and <10): 3


Enter the elements of first 1st matrix:

ย ย 1 1 1

ย ย 1 1 1


Enter the elements of second 2nd matrix:ย 

5 5 5ย 

5 5 5


output:6 6 6ย 

6 6 6

Matrix Multiplication using classes


#include<iostream>


#include<conio.h>


using namespace std;


class matrix {


int a[10][10];


int m,n;


public:


void input();


void output();


void multiply(matrix,matrix);


};


void matrix::input() {


cout<<"Enter the number of row : ";


cin>>m;


cout<<"Enter the number of column : ";


cin>>n;


cout<<"Matrix"<<"\n";


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


for(int j=0;j<n;j++) {


cin>>a[i][j];


} ย  }ย  ย  }


void matrix ::


output() {


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


cout<<"\n";


for(int j=0;j<n;j++) {


cout<<a[i][j]<<"\t";


} ย  }ย  ย  ย  ย  ย  }


void matrix ::


multiply(matrix m1, matrix m2) {


if(m1.n!=m2.m) {


cout<<"matrix multiplication is not possible";


} else { for(int i=0;i<m1.m;i++) {


for(int j=0;j<m2.n;j++) {


a[i][j]=0; for(int k=0;k<m1.n;k++) {


a[i][j]=a[i][j] +( m1.a[i][k]*m2.a[k][j]);


m=m1.m; n=m2.n;


} } } } }


int main() {


matrix m1,m2,m3;


m1.input();


m2.input();


m3.multiply(m1,m2);


m3.output();


}

Output:


Enter the number of row : 2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter the number of column : 2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Matrixย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

3 3ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

3ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

3ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter the number of row : 2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter the number of column : 2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Matrixย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1 1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1 1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

6 ย  ย  ย  6ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

6 ย  ย  ย  6ย ย 

Complex number using classes


/* * C++ Program to Implement Complex Numbers using Classes */


#include <iostream>


using namespace std;


class Complex {


private: int real;


int imag;


public:


Complex(int r = 0, int i = 0):


real(r),


imag(i) {};


void setComplex(void) {


cout << "Enter the real and imaginary parts : ";


cin >> this->real;


cin >> this->imag;


}


Complex add(const Complex& c) {


Complex comp;


comp.real = this->real + c.real;


comp.imag = this->imag + c.imag;


return comp;


}


Complex subtract(const Complex& c) {


Complex comp;


comp.real = this->real - c.real;


comp.imag = this->imag - c.imag;


return comp; }


void printComplex(void) {


cout << "Real : " << this->real << endl << "Imaginary : " << this->imag << endl;


} ย  };


int main() {


Complex a, b, c, d;


cout << "Setting first complex number " << endl;


a.setComplex();


cout << "Setting second complex number " << endl;


b.setComplex();


/* Adding two complex numbers */


cout << "Addition of a and b : " << endl;


c = a.add(b); c.printComplex();


/* Subtracting two complex numbers */


cout << "Subtraction of a and b : " << endl;


d = a.subtract(b); d.printComplex();


}

Output:


Setting first complex number


Enter the real and imaginary parts : 1 1


Setting second complex number


Enter the real and imaginary parts : 2 2


Addition of a and b : Real : 3 Imaginary : 3


Subtraction of a and b : Real : -1 Imaginary : -1

Linear Search


#include <iostream>

using namespace std;

int main()

{

int array[100], search, c, n;


cout<<"Enter the number of elements in array\n";

cin>>n;


cout<<"Enter"<<n<<"integer(s)\n";


for (c = 0; c < n; c++)

cin>>array[c];


cout<<"Enter the number to search\n";

cin>>search;


/* We keep on comparing each element with the element to search until the desired element is found or list ends */

for (c = 0; c < n; c++)

{

if (array[c] == search){

/* if required element found*/

cout<<search<<" is present at location"<<c+1<<"\n";

break;

}

}

if (c == n)

cout<<search<< "is not present in array.\n";


return 0;

}


Output:


Enter the number of elements in arrayย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

4ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter 4 integer(s)ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

3 6 7 8ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter the number to searchย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

4ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

4is not present in array.

Binary search


#include <iostream>

using namespace std;

int main()

{

int c, first, last, middle, n, search, array[100];


cout<<"Enter number of elements\n";

cin>>n;


cout<<"Enter"<< n <<"integers\n";


for ( c = 0 ; c < n ; c++ )

cin>>array[c];


cout<<"Enter value to find\n";

cin>>search;


/*calculating first, last and middle position*/

first = 0;

last = n - 1;

middle = (first+last)/2;


/*Binary search will search element at middle, if element is not found in middle then it will split array into two parts and if element to be searched is less than middle then it will search only in lower part and if greater then in upper part */


while( first <= last )

{

if ( array[middle] == search )

{

cout<<search<< "found at location"<<middle+1<<endl;

break;

}

else if ( array[middle] < search )

first = middle + 1;

else

last = middle - 1;


middle = (first + last)/2;

}


if ( first > last )

cout<<"Not found!"<<search<< "is not present in the list.\n";


return 0;

}

Output:


Enter number of elementsย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

5ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter 5 integersย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2 7 4 5 9ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

Enter value to findย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

4ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

4 found at location 3ย 

Binary search using recursion


#include <iostream>

using namespace std;

int main()

{

int a[10],i,n,m,c,l,u;

cout<<"Enter the size of an array: ";

cin>>n;

cout<<"Enter the elements of the array: " ;

for(i=0;i < n;i++)

cin>>a[i];


cout<<"Enter the number to be search: ";

cin>>m;


l=0,u=n-1;

c=binary(a,n,m,l,u);

if(c==0)

cout<<"Number is not found.";

else

cout<<"Number is found.";

return 0;

}


/*Binary search will search element at middle, if element is not found and if element to be searched is less than middle then it will search only in lower part and if greater then in upper part */


int binary(int a[],int n,int m,int l,int u)

{

int mid,c=0;

if(l < = u){

mid=(l+u)/2;

if(m==a[mid]){

c=1;

}

else if(m < a[mid]){

return binary(a,n,m,l,mid-1);

}

else

return binary(a,n,m,mid+1,u);

}

else

return c;

}

Output:


Enter the size of an array:4

Enter the elements of the array:5 3 6 2

Enter the number to be search:3

Number is found.

call by value


#include <iostream>

using namespace std;


// function declaration

int addition(int x, int y);


int main ()

{

int a = 5;

int b = 10;

int sum ;


cout << "Value of a = " << a << endl;

cout << "Value of b = " << b << endl;


//Function call to add numbers

sum = addition(a, b); //call by value of variables


cout << " addition of two numbers = " << sum << endl;

return 0;

}

int addition(int x, int y)

{

int add;

add = x+y;

return add;

}

Output:


Value of a = 5

Value of b = 10

addition of two numbers = 15

call by reference


#include <iostream>

using namespace std;


// function declaration

void swap(int &x, int &y);


int main ()

{

int a = 5;

int b = 10;


cout << "value of a before swapping : " << a << endl;

cout << "value of b before swapping :" << b << endl;


/* calling a function to swap the values using variable reference.*/

swap(a, b);


cout << "value of a after swapping : " << a << endl;

cout << "value of b after swapping :" << b << endl;


return 0;

}


// function definition to swap the values.

void swap(int &x, int &y)

{

int temp;

temp = x; /* save the value at address x */

x = y; /* put y into x */

y = temp; /* put x into y */


return;

}

Output:


value of a before swapping : 5

value of b before swapping : 10

value of a after swapping : 10

value of b after swapping : 5

call by pointer


#include <iostream>

using namespace std;


// function declaration

void swap(int *x, int *y);


int main ()

{

int a = 5;

int b = 10;


cout << "value of a before swapping : " << a << endl;

cout << "value of b before swapping : " << b << endl;


/* calling a function to swap the values.

* &a indicates pointer to a ie. address of variable a and

* &b indicates pointer to b ie. address of variable b.

*/

swap(&a, &b);


cout << "value of a after swapping : " << a << endl;

cout << "value of b after swapping : " << b << endl;


return 0;

}


// function definition to swap the values.

void swap(int *x, int *y)

{

int temp;

temp = *x; /* save the value at address x */

*x = *y; /* put y into x */

*y = temp; /* put x into y */


return;

}

Output:


value of a before swapping : 5

value of b before swapping : 10

value of a after swapping : 10

value of b after swapping : 5

calling from outside class


#include <iostream>


using namespace std;


class Rectangle

{

public:

double length;

void setWidth( double wid );

double getWidth( void );


private:

double width;

};


// Defination of the Member Function getWidth

double Rectangle::getWidth(void)

{

return width ;

}

// Defination of the Member Function setWidth

void Rectangle::setWidth( double wid )

{

width = wid;

}


// Main function for the program

int main( )

{

Rectangle R;


// Can set Length of Rectangle without member function as it is not private

R.length = 5.0;


cout << "Length of Rectangle : " << R.length <<endl;


// Can not set Width of Rectangle without member function as it is private

R.setWidth(5.0); // Use member function to set it.


cout << "Width of Rectangle : " << R.getWidth() <<endl;


return 0;

}

Output:


Length of box : 5

Width of box : 5

fibonacci series using recursion


#include <iostream>

using namespace std;

int Fibonacci(int);


main()

{

int n, i = 0, c;


cout<<"Enter the number of terms ";

cin>>n;


cout<<"First"<<n<<"terms of Fibonacci series are :-\n";


for ( c = 1 ; c <= n ; c++ )

{

cout<<Fibonacci(i)<<"\n";

i++;

}


return 0;

}


int Fibonacci(int n)

{

if ( n == 0 )

return 0;

else if ( n == 1 )

return 1;

else

return ( Fibonacci(n-1) + Fibonacci(n-2) );

/*adding Fibonacci of (n-1) & (n-2) by recursive calling it*/

}


Output:


Enter the number of terms 5ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

First5terms of Fibonacci series are :-ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

0ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

1ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

2ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

3ย 

factorial using recursion


#include <iostream>

using namespace std;

long factorial(int);


int main()

{

int n;

long f;


cout<<"Enter an integer to find factorial\n";

cin>>n;


if (n < 0)

cout<<"Negative integers are not allowed.\n";

else

{

f = factorial(n);

cout<<n<<"! ="<<f<<"\n";

}


return 0;

}


long factorial(int n)

{

if (n == 0)

return 1;

else

return(n * factorial(n-1));

/*recursive call to factorial function*/

}

Output:


Enter an integer to find factorialย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

5ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

5! =120ย 

single inheritance


#include <iostream>


using namespace std;


// Base class

class Shape

{

public:

void setWidth(int w)

{

width = w;

}

void setHeight(int h)

{

height = h;

}

protected:

int width;

int height;

};


// Derived class

class Rectangle: public Shape

{

public:

int getArea()

{

return (width * height);

}

};


int main(void)

{

Rectangle R;


R.setWidth(6);

R.setHeight(8);


// Print the area of the object.

cout << "Area of Rectangle : " << R.getArea() << endl;


return 0;

}


Output:


Area of Rectangle : 48

multiple inheritance


#include <iostream>


using namespace std;


// Base class Area

class Area

{

public:

void setlength(int l)

{

length = l;

}

void setbreath(int b)

{

breath = b;

}

protected:

int length;

int breath;

};


// Base class PerSQCost

class PerSQCost

{

public:

int getCost(int area)

{

return area * 1500;

}

};


// Derived class

class Room: public Area, public PerSQCost

{

public:

int getArea()

{

return (length * breath);

}

};


int main(void)

{

Room R;

int area;


R.setlength(5);

R.setbreath(7);


area = R.getArea();


// Print the area of the object.

cout << "Area of Room " << R.getArea() << endl;


// Print the total cost of painting

cout << "Cost of Room : $" << R.getCost(area) << endl;


return 0;

}

Output:


Area of Room : 120

Cost of Room : $180000


Hierarchial inheritance


#include <iostream>


using namespace std;


// Base class

class Shape

{

public:

void setWidth(int w)

{

width = w;

}

void setHeight(int h)

{

height = h;

}

protected:

int width;

int height;

};


// Derived class

class Rectangle: public Shape

{

public:

int getArea()

{

return (width * height);

}

};


class Triangle: public Shape

{

public:

int getArea()

{

return (width * height)/2;

}

};



int main(void)

{

Rectangle R;

R.setWidth(6);

R.setHeight(8);


// Print the area of the object.

cout << "Area of Rectangle : " << R.getArea() << endl;


Triangle T;

T.setWidth(5);

T.setHeight(6);

cout << "Area of Triangle : " << T.getArea() << endl;

return 0;

}


Output:


Area of Rectangle : 48

Area of Triangle : 15

Abstract class


#include <iostream>

using namespace std;


// Base class

class Shape

{

public:

// pure virtual function providing interface framework.

virtual int getArea() = 0;

void setHeight(int h)

{

height = h;

}

void setWidth(int w)

{

width = w;

}


protected:

int width;

int height;

};


// Derived class Triangle

class Triangle: public Shape

{

public:

int getArea()

{

return (width * height)/2;

}

};


//Derived class Rectangle

class Rectangle: public Shape

{

public:

int getArea()

{

return (width * height);

}

};



int main(void)

{

Rectangle R;

Triangle T;


R.setWidth(6);

R.setHeight(10);

// Print the area of the Rectangle

cout << " Area of Rectangle : " << R.getArea() << endl;


T.setWidth(4);

T.setHeight(7);

// Print the area of the Triangle

cout << "Area of Triangle : " << T.getArea() << endl;


return 0;

}


Output:


Area of Rectangle : 60

Area of Triangle : 14

Video Lectures