0% found this document useful (0 votes)
142 views

C++ Practicle File

The document discusses C++ programs to perform operations on matrices such as addition and subtraction. It contains functions to read and print two matrices, and functions to calculate the sum and difference of the matrices. The main function checks that the matrices are of the same order, then calls the respective functions to input the matrices, print them, and calculate their sum and difference.

Uploaded by

Devesh Rajotia
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views

C++ Practicle File

The document discusses C++ programs to perform operations on matrices such as addition and subtraction. It contains functions to read and print two matrices, and functions to calculate the sum and difference of the matrices. The main function checks that the matrices are of the same order, then calls the respective functions to input the matrices, print them, and calculate their sum and difference.

Uploaded by

Devesh Rajotia
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 28

Q.

Shorting in array
#include<iostream>

using namespace std;

void selectionSort(int a[], int n) {

int i, j, min, temp;

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

min = i;

for (j = i + 1; j < n; j++)

if (a[j] < a[min])

min = j;

temp = a[i];

a[i] = a[min];

a[min] = temp;

int main() {

int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };

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

int i;

cout<<"Given array is:"<<endl;

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

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

cout<<endl;

selectionSort(a, n);

printf("\nSorted array is: \n");


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

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

return 0;

OUTPUT
Given array is:

22 91 35 78 10 8 75 99 1 67

Sorted array is:

1 8 10 22 35 67 75 78 91 99

Q. Insertion of new element in Array at desired position


#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int arr[50], size, insert, i, pos;

cout<<"Enter Array Size : ";

cin>>size;

cout<<"Enter array elements : ";

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

cin>>arr[i];

cout<<"Enter element to be insert : ";

cin>>insert;
cout<<"At which position (Enter index number) ? ";

cin>>pos;

// now create a space at the required position

for(i=size; i>pos; i--)

arr[i]=arr[i-1];

arr[pos]=insert;

cout<<"Element inserted successfully..!!\n";

cout<<"Now the new array is : \n";

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

cout<<arr[i]<<" ";

getch();

OUTPUT
Q. Searching in Array using function

#include<iostream>

using namespace std;

int main()

int arr[10], i, num, n, cnt=0, pos;

cout<<"\n Enter Array Size : ";

cin>>n;

cout<<"\n Enter Array Elements : \n";

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

cout<<" ";

cin>>arr[i];

cout<<"\n Enter Element to be Searched : ";

cin>>num;

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

if(arr[i]==num)

cnt=1;

pos=i+1;

break;

}
}

if(cnt==0)

cout<<"\n Element Not Found..!!";

else

cout<<"\n Element "<<num<<" Found At Position "<<pos;

return 0;

OUTPUT
Q. Print table of any given variable using function

#include<iostream>

using namespace std;

int main()

int num;

cout<<"Enter Number To Find Multiplication table ";

cin>>num;

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

cout<<num<<" * "<<a<<" = "<<num*a<<endl;

return 0;

OUTPUT
Q. To find greatest among three using Call-by value function.

#include<iostream.h>

#include<stdio.h>

#include<conio. h>

int greatest (int, int, int);

void main ()

clrscr ();

int a, b, c, l;

cout<" \n Enter Value in A=";

cin>>a;

cout <" \n Enter Value in B=";

cin>>b;

cout<<" \n Enter Value in C=";

cin>>c;

l=greatest (a,b,c);

cout<<" \n Greatest Among Three=";


cout<l;

getch ();

int greatest (int a, int b, int c)

if (a>b&&a>c)

cout<<" \n A is Greatest.";

if (b>a &&b>c)

cout<<" \n B is Greatest.";

if (c>a&&c>b)

cout<<" \n C is Greatest";

return (a, b, c);

OUTPUT

Q.Insertion in Array using call by reference function.

#include<iostream.h>

#include<conio.h>

#include<stdio.h>
void read (int *xyz)

int t;

cout<<"\n Enter Value in Array's:-\n";

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

cin>>t;

*xyz=t;

xyz++;

xyz=xyz-5;

cout<<"\n Values in Array's:-\n";

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

{cout<<"\n\t"<<*xyz;

xyz++;}

voidst (int *xyz)

{int* start;

start=xyz;

inti,j,temp;

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

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

if (*xyz>* (xyz+1))

temp=*xyz;

*xyz= * (xyz+1);
* (xyz+1) =temp;

xyz++;

xyz=start;

void main ()

clrscr ();

intf,g, arr[5];

int **;

x=&arr[0];

read (x);

st (x);

cout<<"\nSortedArr is \n";

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

{cout<<arr[i]; cout<<"\n";}

getch ();

OUTPUT
Q-Insertion of data using single level inheritance.
#include <iostream>

#include <conio.h>

using namespace std;

class person /*Parent class*/

private:

char fname[100],lname[100],gender[10];

protected:

int age;

public:
void input_person();

void display_person();

};

class student: public person /*Child class*/

private:

char college_name[100];

char level[20];

public:

void input_student();

void display_student();

};

void person::input_person()

cout<<"First Name: ";

cin>>fname;

cout<<"Last Name: ";

cin>>lname;

cout<<"Gender: ";

cin>>gender;

cout<<"Age: ";

cin>>age;

void person::display_person()
{

cout<<"First Name : "<<fname<<endl;

cout<<"Last Name : "<<lname<<endl;

cout<<"Gender : "<<gender<<endl;

cout<<"Age : "<<age<<endl;

void student::input_student()

person::input_person();

cout<<"College: ";

fflush(stdin);

gets(college_name);

cout<<"Level: ";

cin>>level;

void student::display_student()

person::display_person();

cout<<"College : "<<college_name<<endl;

cout<<"Level : "<<level<<endl;

int main()

student s;
cout<<"Input data"<<endl;

s.input_student();

cout<<endl<<"Display data"<<endl;

s.display_student();

getch();

return 0;

OUTPUT
Input data

First Name: Harry

Last Name: Potter

Gender: Male

Age: 23

College: Abc International College

Level: Bachelors

Display data

First Name : Harry

Last Name : Potter

Gender : Male

Age : 23

College : Abc International College

Level : Bachelors

Q-Insertion of data using Multi level inheritance.


#include <iostream>

using namespace std;


class A

public:

void display()

cout<<"Base class content.";

};

class B : public A

};

class C : public B

};

int main()

C obj;

obj.display();

return 0;

OUTPUT
Base class content.

Q-Insertion of data using Multiple inheritace.


#include <iostream>

using namespace std;


class Mammal {

public:

Mammal()

cout << "Mammals can give direct birth." << endl;

};

class WingedAnimal {

public:

WingedAnimal()

cout << "Winged animal can flap." << endl;

};

class Bat: public Mammal, public WingedAnimal {

};

int main()

Bat b1;

return 0;

OUTPUT
Mammals can give direct birth.

Winged animal can flap.

Q-Write a C++ Program for Insertion & Deletion in Queue


#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<stdlib.h>

void main ()

clrscr();

int size=5,item;

intarr[5], ch,front 0,rear%3D0%3B

do

cout<<"\n 1. Insert \n 2. Delete \n 3.Exit";

cout<<"\ Enter Your Choice=";

cin>>ch;

switch (ch)

case 1:if (rear>size-1)

cout<<"\n Queue is Full.";

else

cout<<"\n Enter item to insert=";

cin>>item;

arr[rear]=item;

rear++;

};break;

case 2:if (front=3rear)


{

cout<<"\n Queue is Empty.";

else

cout<<"\n Item to deleted=";

cout<<arr [front];

rear++;

};break;

case 3: exit (0);

default :cout<<"\n You entered a wrong choice. \n\t Please try

again!";

} while (ch-3);

getch ();

OUTPUT
CONSTRUCTOR

#include <iostream>

using namespace std;

class Line {

public:
void setLength( double len );

double getLength( void );

Line(); // This is the constructor

private:

double length;

};

// Member functions definitions including constructor

Line::Line(void) {

cout << "Object is being created" << endl;

void Line::setLength( double len ) {

length = len;

double Line::getLength( void ) {

return length;

// Main function for the program

int main() {

Line line;

// set line length

line.setLength(6.0);

cout << "Length of line : " << line.getLength() <<endl;

return 0;
}

OUTPUT

Object is being created

Length of line : 6

DISTRUCTOR

#include <iostream>

using namespace std;

class Line {

public:

void setLength( double len );

double getLength( void );

Line(); // This is the constructor declaration

~Line(); // This is the destructor: declaration

private:

double length;

};

// Member functions definitions including constructor

Line::Line(void) {
cout << "Object is being created" << endl;

Line::~Line(void) {

cout << "Object is being deleted" << endl;

void Line::setLength( double len ) {

length = len;

double Line::getLength( void ) {

return length;

// Main function for the program

int main() {

Line line;

// set line length

line.setLength(6.0);

cout << "Length of line : " << line.getLength() <<endl;

return 0;

OUTPUT
Object is being created

Length of line : 6

Object is being deleted


Q.Program to Calculate the Sum & Difference of the Matrices
#include <stdio.h>

#include <stdlib.h>

void readmatA();

void printmatA();

void readmatB();

void printmatB();

void sum();

void diff();

int a[10][10], b[10][10], sumarray[10][10], arraydiff[10][10];

int i, j, row1, column1, row2, column2;

void main()

printf("Enter the order of the matrix A \n");

scanf("%d %d", &row1, &column1);

printf("Enter the order of the matrix B \n");

scanf("%d %d", &row2, &column2);

if (row1 != row2 && column1 != column2)

printf("Addition and subtraction are possible \n");

exit(1);

else

printf("Enter the elements of matrix A \n");

readmatA();
printf("MATRIX A is \n");

printmatA();

printf("Enter the elements of matrix B \n");

readmatB();

printf("MATRIX B is \n");

printmatB();

sum();

diff();

/* Function to read a matrix A */

void readmatA()

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

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

scanf("%d", &a[i][j]);

return;

/* Function to read a matrix B */

void readmatB()

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

{
for (j = 0; j < column2; j++)

scanf("%d", &b[i][j]);

/* Function to print a matrix A */

void printmatA()

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

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

printf("%3d", a[i][j]);

printf("\n");

/* Function to print a matrix B */

void printmatB()

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

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

printf("%3d", b[i][j]);

}
printf("\n");

/* Function to do the sum of elements of matrix A and Matrix B */

void sum()

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

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

sumarray[i][j] = a[i][j] + b[i][j];

printf("Sum matrix is \n");

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

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

printf("%3d", sumarray[i][j]) ;

printf("\n");

return;

/* Function to do the difference of elements of matrix A and Matrix B */

void diff()

{
for (i = 0; i < row1; i++)

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

arraydiff[i][j] = a[i][j] - b[i][j];

printf("Difference matrix is \n");

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

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

printf("%3d", arraydiff[i][j]);

printf("\n");

return;

OUTPUT
Enter the order of the matrix A

33

Enter the order of the matrix B

33

Enter the elements of matrix A

145

678
489

MATRIX A is

1 4 5

6 7 8

4 8 9

Enter the elements of matrix B

367

842

153

MATRIX B is

3 6 7

8 4 2

1 5 3

Sum matrix is

4 10 12

14 11 10

5 13 12

Difference matrix is

-2 -2 -2

-2 3 6

3 3 6

Q-SQL(DDL Commands & DML Commands).

You might also like