[go: up one dir, main page]

0% found this document useful (0 votes)
16 views18 pages

cms practical_075336

Uploaded by

crystalkiran62
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views18 pages

cms practical_075336

Uploaded by

crystalkiran62
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Computer Practical

Q1. WAP to find addition, subtraction, multiplication and division of two numbers given
by user using Switch - case.

#include <iostream>
#include <conio.h>
#include<math.h>
using namespace std;
main()
{

int n ;
cout<<"\n 1. Addition.\t 2. Subtraction. \t 3. Multiplication. \t 4. Division. ";
cout<<"\n Enter your choice.";
cin>>n;
switch(n)
{

case 1:
int a,b,c;
cout<<"\n Enter two numbers . ";
cin>>a>>b;
c = a + b;
cout << "Sum of two numbers is :" << c << endl ;
break;
case 2:
int x,y,sub;
cout<<"\n Enter two numbers . ";
cin>>x>>y;
sub = x - y;
cout << "Subtraction of two numbers is :" << sub << endl ;
break;
case 3:
int p,q,m;
cout<<"\n Enter two numbers . ";
cin>>p>>q;
m = p * q;
cout << " Multiplication of two numbers is :" << m << endl ;
break;
case 4:
double d,e,div;
cout<<"\n Enter two numbers . ";
cin>>d>>e;
div = d / e;
cout << "Division of two numbers is :" << div << endl ;
break;
default:
cout<<"Wrong Choice.";
break;
}
getch();
}

Q2. WAP for sum of two matrix.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],b[3][3],c[3][3],i,j;
cout<<"\n Enter first matrix :";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}}
cout<<"\n Enter second matrix :";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>b[i][j];
}}
cout<<"\n First matrix is :"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
cout<<"\n Second matrix is :"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<b[i][j]<<" ";
}
cout<<"\n";
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}}
cout<<"\n Sum of two matrix is :"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
getch();
}
Q3. WAP for Selection sort.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int ar[20],i,j,size,temp,loc,min;
cout<<"Enter array size : " ;
cin>>size;
cout<<" Enter array elements :";
for(i=0;i<size;i++)
{
cin>>ar[i];
}
for(i=0;i<size-1;i++)
{
min=ar[i]; //element
loc=i; //loc=0 index number
for(j=i+1;j<size;j++)
{
if(ar[j]<min)
{
min=ar[j];
loc=j;
}}
temp=ar[i];
ar[i]=ar[loc];
ar[loc]=temp;
}
cout<<" \n Array after selection sort : ";
for(i=0;i<size;i++)
{
cout<<ar[i]<<" ";
}
getch();
}

Q4. WAP for Insertion sort program.


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20],size,i,j,temp;
cout<<"Enter array size : ";
cin>>size;
cout<<" Enter array elements :";
for(i=0;i<size;i++)
{
cin>>a[i];
}
for(i=1;i<size;i++)
{
temp=a[i];
j=i-1;
while(j>=0 && a[j]>temp)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
cout<<" \n After insertion sort : ";
for(i=0;i<size;i++)
{
cout<<a[i]<<" ";
}
getch();
}
Q5. WAP for Bubble sort.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20],size,i,j,temp,swap;
cout<<" Enter array size :";
cin>>size;
cout<<" Enter array elements :";
for(i=0;i<size;i++)
{
cin>>a[i];
}
for(i=0;i<(size-1);i++)
{
swap=0;
for(j=0;j<(size-i)-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
swap=1;
}}
if(swap==0)
{
break;
}}
cout<<"\n After Bubble sort : ";
for(i=0;i<size;i++)
{
cout<<a[i]<<" ";
}
getch();
}

Q6. Program for push and pop operations in stack.

// Push and Pop implementation in stack.


#include<iostream.h>
#include<conio.h>
#define MAX 5
int stack[MAX];
int top=-1;
void push(int); //push function declaration.
int pop(); //pop function declaration.
void display(); //Display function declaration.
void main()
{
clrscr();
int choice;
int num;

do
{
cout<<"\n Enter your choice : ";
cout<<"\n 1. PUSH ";
cout<<"\n 2. POP ";
cout<<"\n 3. Display ";
cout<<"\n 4. Exit "<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n Enter element to add in the stack : ";
cin>>num;
push(num); //function push is calling.
break;

case 2:
pop(); //function pop is calling.
break;
case 3:
display(); //display function is calling.
break;
}}
while(choice!=4);
getch();
}

void push(int n) //push function definition.


{
if(top==MAX-1)
{
cout<<"\n Overflow";
return;
}
else
{
top=top+1;
stack[top]=n;
}}

int pop() //pop function definition.


{
int x;
if (top==-1)
{
cout<<"\n Underflow";
return 0;
}
else
{
x=stack[top];
top=top-1;
cout<<"\n Deleted element is : "<<x;
return x;
}}

void display()
{
if(top==-1)
{
cout<<"\n Stack is Empty. ";
return ;
}
else
{
cout<<"\n Stack elements are : "<<endl;
int i;
for (i=top;i>=0;i--)
{
cout<<endl;
cout<<stack[i];
} }}

Q7. WAP to find prime numbers in given range.

#include <iostream>
#include <conio.h>
using namespace std;
main()
{
int i,j,n,f;
cout<<" Enter your range :-";
cin>>n;
cout<<" Prime Numbers between 1 to "<<n<<endl;
for( i=2;i<=n;i++)
{
f=1;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
f=0;
break;
}
}
if(f==1)
cout<<i<<", ";

getch();
}
Q8. A program that stores and displays information using structure.

#include <iostream>
#include <conio.h>
struct employee
{
int empID;
char name[50];
int salary;
char department[50];
};
void main()
{
employee emp[3] = { { 1 , "Harry" , 20000 , "Finance" } , { 2 , "Sally" , 50000 , "HR" } , { 3 ,
"John" , 15000 , "Technical" } };
cout<<"The employee information is given as follows:"<<endl;
cout<<endl;
for(int i=0; i<3;i++)
{
cout<<"Employee ID: "<<emp[i].empID<<endl;
cout<<"Name: "<<emp[i].name<<endl;
cout<<"Salary: "<<emp[i].salary<<endl;
cout<<"Department: "<<emp[i].department<<endl;
cout<<endl;
}
getch();
}
Output
The employee information is given as follows:
Employee ID: 1
Name: Harry
Salary: 20000
Department: Finance

Employee ID: 2
Name: Sally
Salary: 50000
Department: HR

Employee ID: 3
Name: John
Salary: 15000
Department: Technical

Q9. Sum of odd numbers using for loop.

#include <iostream>
#include <conio.h>
using namespace std;
main()
{
int n, sum=0, i=1;
cout<<"Enter your number ";
cin>>n;

for(i; i<n*2; i=i+2)


{
sum=sum+i;
}
cout<<" Sum of "<<n<<" ODD numbers = "<< sum;
getch();
}

Q10. WAP to find surface area of box using class .

#include <iostream.h>
#include<conio.h>
class box
{
public:
double length;
double breadth;
double height;
};

void main()
{
clrscr();
box b1;
box b2;
double SA ;
cout<<" Enter height of b1 : ";
cin>>b1.height;
cout<<" Enter length of b1 : ";
cin>>b1.length;
cout<<" Enter Breadth of b1 : ";
cin>>b1.breadth;

SA =2*(( b1.height * b1.length)+(b1.length * b1.breadth)+


(b1.height*b1.breadth));
cout<<"volume of B1 shape = " <<SA<<endl;

getch();
}

Q11. WAP using hierarchical inheritance find product and sum of any number.

#include <iostream>
#include<conio.h>

class A //single base class A


{
public:
int x, y;
void getdata()
{
cout << "\nEnter value of x and y:\n";
cin >> x >> y;
}
};
class B : public A //B is derived from class A
{
public:
void product()
{
cout << "\nProduct= " << x * y;
}
};
class C : public A //C is also derived from class A
{
public:
void sum()
{
cout << "\nSum= " << x + y;
}
};
int main()
{
B obj1; //object of derived class B
C obj2; //object of derived class C
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
}

Output

Enter value of x and y:


2
3
Product= 6
Enter value of x and y:
2
3
Sum= 5

Q12. Program for parameterized constructor.

#include<iostream>
#include<conio.h>
class ABC
{
private:
int l,w,x;
public:
ABC (int a,int b) //parameterized constructor to initialize l and w
{
l = a;
w = b;
}
int area( ) //function to find area
{
x = l * w;
return x;
}
void display( ) //function to display the area
{
cout << "Area = " << x << endl;
}
};

int main()
{
ABC c(2,4); //initializing the data members of object 'c' implicitly
c.area();
c.display();

ABC c1= ABC(4,4); // initializing the data members of object 'c'


c1.area();
c1.display();
return 0;
} //end of program

Output
Area = 8
Area = 16

Q 13. Program for copy constructor.

#include<iostream>
#include<conio.h>
class example
{
private:
int x;
public:
example (int a) //parameterized constructor to initialize l and b
{
x = a;
}
example( example &b) //copy constructor with reference object argument
{
x = b.bx;
}
int display( ) //function to display
{
return x;
}
};

int main()
{
example c1(2); //initializing the data members of object 'c' implicitly
example c2(c1); //copy constructor called
example c3 = c1;
example c4 = c2;
cout << "example c1 = " << c1.display() << endl;
cout << "example c2 = " << c2.display() << endl;
cout << "example c3 = " << c3.display() << endl;
cout << "example c4 = " << c4.display() << endl;
return 0;
}
Output
example c1 = 2
example c2 = 2
example c3 = 2
example c4 = 2

Q13.A program to highlight the concept of destructor.

#include <iostream>
#include<conio.h>
class ABC
{
public:
ABC () //constructor defined
{
cout << "Hey look I am in constructor" << endl;
}
~ABC() //destructor defined
{
cout << "Hey look I am in destructor" << endl;
}
};

int main()
{
ABC x; //constructor is called
cout << "function main is terminating...." << endl;
/*....object x goes out of scope ,now destructor is being called...*/
return 0;
}
Output
Hey look I am in constructor
function main is terminating....
Hey look I am in destructor

Q14. What is database management system.

DBMS (Data Base Management System)


Database is a collection of related data and data is a collection of facts and figures (e.g.
–text, numbers, videos, images, audios). that can be processed to produce information
Mostly data represents recordable facts. Data aids in producing information, which is based on
facts. For example, if we have data about marks obtained by all students, we can then conclude
about toppers and average marks.
A database management system stores data in such a way that it becomes easier to retrieve,
manipulate, and produce information.
DBMS Software’s – My SQL (Structured query language), ORACLE(Oak Ride Automatic
Computer And Logical engine), SAP ( System Application And Products in data processing),
HANA (High-performance Analytic Appliance), IMDB ( Internet Movie database) etc.

Characteristics Or Advantages
Traditionally, data was organized in file formats. DBMS was a new concept then, and all
the research was done to make it overcome the deficiencies in traditional style of data
management. A modern DBMS has the following characteristics −
1. Real-world entity − A modern DBMS is more realistic and uses real-world entities
to design its architecture. It uses the behavior and attributes too. For example, a
school database may use students as an entity and their age as an attribute.
2. Relation-based tables − DBMS allows entities and relations among them to form
tables. A user can understand the architecture of a database just by looking at the
table names.
3. Less redundancy − DBMS follows the rules of normalization, which splits a relation
when any of its attributes is having redundancy in values. Normalization is a
mathematically rich and scientific process that reduces data redundancy.

If a database design is not perfect, it may contain anomalies (Error), which are
like a bad dream for any database administrator. Managing a database
with anomalies is next to impossible.
 Update anomalies − If data items are scattered and are not linked to each other properly,
then it could lead to strange situations. For example, when we try to update one data
item having its copies scattered over several places, a few instances get updated properly
while a few others are left with old values. Such instances leave the database in an
inconsistent state.
 Deletion anomalies − We tried to delete a record, but parts of it was left undeleted
because of unawareness, the data is also saved somewhere else.
 Insert anomalies − We tried to insert data in a record that does not exist at all.

4. Query Language − DBMS is equipped with query language, which makes it more
efficient to retrieve and manipulate data. A user can apply as many and as different
filtering options as required to retrieve a set of data. Traditionally it was not possible
where file-processing system was used.

5. Multiuser and Concurrent Access − DBMS supports multi-user environment and


allows them to access and manipulate data in parallel. Though there are restrictions
on transactions when users attempt to handle the same data item, but users are always
unaware of them.

6. Multiple views − DBMS offers multiple views for different users. A user who is in
the Sales department will have a different view of database than a person working in
the Production department. This feature enables the users to have a concentrate view
of the database according to their requirements.

7. Security − Features like multiple views offer security to some extent where users are
unable to access data of other users and departments. DBMS offers methods to
impose constraints while entering data into the database and retrieving the same at a
later stage. DBMS offers many different levels of security features, which enables
multiple users to have different views with different features. For example, a user in
the Sales department cannot see the data that belongs to the Purchase department.
Additionally, it can also be managed how much data of the Sales department should
be displayed to the user. Since a DBMS is not saved on the disk as traditional file
systems, it is very hard for miscreants to break the code.

Q15.Write different structured query language.

SQL (Structured Query Language) –


Compute language for storing, Manipulating and
retrieving data from database.
Inventor – Raymard Boyce and Donald Chamberlin (1970).
SQL Commands – To access data from My SQL database
1. Data definition (description) language (DDL) – DDL is used to define the
structure of your tables and other objects in database.
a. CREATE – to create objects or table and database in DBMS.
Syntax-
For creating database –
CREATE DATABASE <database name>;
For creating table-
CREATE TABLE <table name> (<column name> <data type>
(size));
CREATE TABLE student (student name varchar (30));
b. ALTER – Used to add columns, delete existing columns, change data type.
Syntax-
To add a column-
ALTER TABLE< table_name> ADD <column_name> <data type>;
To delete a column-
ALTER TABLE <table name> DROP COLUMN <column name>;

c. DROP – Used to delete objects from DBMS.


Syntax –
DROP TABLE <table name>;

d. TRUNCATE – Used to remove all records from table.


Syntax –
TRUNCATE TABLE < table name>;
e. RENAME – Used to rename objects.
Syntax-
RENAME TABLE <table name> TO <new table name>;
2. Data manipulation language (DML) – Commands used to access and manipulate
data in existing DBMS.
a. SELECT – Used to retrieve data from DBMS.
Syntax –
SELECT * FROM <table name>;
b. INSERT – Used to insert data into a table.
Syntax –
INSERT INTO <table name>
(column 1, Column 2, …………) VALUES ( Value 1, Value 2, ……….);
c. UPDATE – Used to Update data in existing table.
Syntax –
UPDATE <table name> SET column 1 = value 1, column 2 = value
2,……. WHERE any column = any value;
d. DELETE – Used to delete records from table.
Syntax –
DELETE FROM <table name > WHERE <any column> = <any value>;

Types of DML
i. Procedural DML – These require a user to specify what data is needed and how
to get it.
ii. Non-procedural DML – These require a user to specify what data is needed
without specifying how to get it.

3. Transaction Control Language (TCL) – Commands are used to manage


transaction is DBMS.
a. COMMIT – used to save the work done.
COMMIT;
b. SAVEPOINT – Used to create restore point.
Syntax –
SAVEPOINT <savepoint name>;
c. ROLLBACK – Used to restore since last commit.
Syntax –
ROLLBACK;
4. Data Control Language (DCL) – Used to assign security level in database. Like
multiple user setup, grant roles, permissions, access privileges to the user, these are
two kinds of users –
i. Users – Work with data no change.
ii. Admin – Can change structure and data.
Commands are –
a. GRANT – Gives users access and privileges.
b. REVOKE – To withdraw access and privileges.

You might also like