You are on page 1of 45

Computer Science

PRACTICAL FILE

Dhruv Tyagi
XII-A
Roll No. - 13
Computer Science Practical File

INDEX
Q1 Create the table and write SQL queries for the given tables ..................................................- 1 -
i. To show all information about the Sofa’s from the interiors table. ......................................- 1 -
Output: creating the table ........................................................................................................... - 1 -
Code: ............................................................................................................................................ - 2 -
List all details where type is Sofa. ...................................................................................................- 3 -
Output: ......................................................................................................................................... - 3 -
Code: ............................................................................................................................................ - 3 -
ii. List of items with price greater than 10000. ........................................................................- 3 -
Output: ......................................................................................................................................... - 3 -
Code: ............................................................................................................................................ - 3 -
iii. To display item name and date of stock of those items, in which the discount percentage is
more than 15. ....................................................................................................................- 4 -
Output: ......................................................................................................................................... - 4 -
Code: ............................................................................................................................................ - 4 -
iv. To list item name and type of items, in which data of stock is before 22-Jan-2002 in descending
order of item name. ...........................................................................................................- 4 -
Output: ......................................................................................................................................... - 4 -
Code: ............................................................................................................................................ - 4 -
v. To count the number of items of each type.........................................................................- 4 -
Output: ......................................................................................................................................... - 4 -
Code: ............................................................................................................................................ - 4 -
vi. To insert new row in interiors table with following data: ....................................................- 5 -
Output: ......................................................................................................................................... - 5 -
Code: ............................................................................................................................................ - 5 -
vii. To count distinct types from interiors table. .......................................................................- 5 -
Output: ......................................................................................................................................... - 5 -
Code: ............................................................................................................................................ - 5 -
viii. To find average discount from interiors for each type of interiors........................................- 5 -
Output: ......................................................................................................................................... - 5 -
Code: ............................................................................................................................................ - 5 -
ix. To calculate sum of price from interiors where data of stock is before 12-Feb-2002 .............- 6 -
Output: ......................................................................................................................................... - 6 -
Code: ............................................................................................................................................ - 6 -
x. To increase price of all Office table by 3000 from interiors table. ........................................- 6 -
Output: ......................................................................................................................................... - 6 -
Code: ............................................................................................................................................ - 6 -
Q2 Create the tables and Write SQL queries for given tables. .....................................................- 7 -

Dhruv Tyagi -i-


XII-A
Roll no.: 13
Computer Science Practical File

CREATING THE TABLE: ....................................................................................................................- 7 -


Output: ......................................................................................................................................... - 7 -
Code: ............................................................................................................................................ - 8 -
i. To select item purchased after 31-Jan-2005. .......................................................................- 9 -
Output: ......................................................................................................................................... - 9 -
Code: ............................................................................................................................................ - 9 -
ii. To list the item name in descending order of date of purchase where quantity is more than
three .................................................................................................................................- 9 -
Output: ......................................................................................................................................... - 9 -
Code: ............................................................................................................................................ - 9 -
iii. To count number of Items (total quantity) whose cost is more than 10000. .........................- 9 -
Output: ......................................................................................................................................... - 9 -
Code: ............................................................................................................................................ - 9 -
iv. To insert a new record in the table STOCK with following data: ......................................... - 10 -
Output: ....................................................................................................................................... - 10 -
Code: .......................................................................................................................................... - 10 -
v. To find minimum distinct item type (itemcode) from STOCK. ............................................ - 10 -
Output: ....................................................................................................................................... - 10 -
Code: .......................................................................................................................................... - 10 -
vi. To generate a report on each item code with the item name and total value ..................... - 10 -
Output: ....................................................................................................................................... - 10 -
Code: .......................................................................................................................................... - 10 -
vii. To find average cost of all items with more than 2 items. .................................................. - 11 -
Output: ....................................................................................................................................... - 11 -
Code: .......................................................................................................................................... - 11 -
viii. To find average cost of items with date of purchase is before 1-Jan-2005. ......................... - 11 -
Output: ....................................................................................................................................... - 11 -
Code: .......................................................................................................................................... - 11 -
ix. To reduce the cost of all UPS by 100 ................................................................................. - 11 -
Output: ....................................................................................................................................... - 11 -
Code: .......................................................................................................................................... - 11 -
x. To delete the table STOCK. ............................................................................................... - 12 -
Output: ....................................................................................................................................... - 12 -
Code: .......................................................................................................................................... - 12 -
Q3 Write a menu driven program to add, subtract, multiply and divide 2 complex numbers using
structures. ......................................................................................................................... - 13 -
Output: ....................................................................................................................................... - 13 -
//Code ........................................................................................................................................ - 14 -
Q4 Write a menu driven program to show upper triangle, lower triangle of a 2D matrix ........... - 16 -
Output: ....................................................................................................................................... - 16 -
//Code: ....................................................................................................................................... - 17 -
Dhruv Tyagi -ii-
XII-A
Roll no.: 13
Computer Science Practical File

Q5 Write a menu driven program to show sum of primary diagonal and sum of secondary diagonal
of a 2D matrix. .................................................................................................................. - 19 -
Output: ....................................................................................................................................... - 19 -
//Code: ....................................................................................................................................... - 20 -
Q6 Write a menu driven program for addition, subtraction and multiplication of 2 matrices. ... - 22 -
Output: ....................................................................................................................................... - 22 -
//Code ........................................................................................................................................ - 23 -
Q7 Write a menu driven program to find row sum and column sum in a 2D array. .................... - 26 -
Output: ....................................................................................................................................... - 26 -
//Code ........................................................................................................................................ - 27 -
Q8 Write a menu driven program to search and display the details of the students from the array
of students ........................................................................................................................ - 29 -
Output: ....................................................................................................................................... - 29 -
//Code ........................................................................................................................................ - 30 -
Q9 Write a program to input integer data in two arrays. .......................................................... - 33 -
Output: ....................................................................................................................................... - 33 -
//Code ........................................................................................................................................ - 34 -
Q10 Write a menu driven program which allows the user to perform operations on a one
dimensional array.............................................................................................................. - 36 -
Output: ....................................................................................................................................... - 36 -
//Code: ....................................................................................................................................... - 37 -

Dhruv Tyagi -iii-


XII-A
Roll no.: 13
Computer Science Practical File

Q1 Create the table and write SQL queries for the given tables
i. To show all information about the Sofa’s from the interiors table.
Output: creating the table

Dhruv Tyagi -- 1 --
XII-A
Roll no.: 13
Computer Science Practical File

Code:
USE HHW
create table interiors
(
NO int primary key,
ITEMNAME CHAR(20),
TYPE CHAR(20),
DATEOFSTOCK DATE,
PRICE INT,
DISCOUNT INT
);
insert into interiors
values(1,"Red rose","Double Bed",'2006/02/23',32000,15);
insert into interiors
values(2,"Soft touch","Baby cot",'2006/01/20',9000,10);
insert into interiors
values(3,"Jerry’s home","Baby cot",'2006/02/19',8500,10);
insert into interiors
values(4,"Rough woof","Office table",'2006/01/01',20000,20);
insert into interiors
values(5,"Comfort zone","Double Bed",'2006/01/12',15000,20);
insert into interiors
values(6,"Jerry look","Baby cot",'2006/02/24',7000,19);
insert into interiors
values(7,"Lion king","Office table",'2006/02/20',16000,20);
insert into interiors
values(8,"Royal tiger","Sofa",'2006/02/22',30000,25);
insert into interiors
values(9,"Park sitting","Sofa",'2005/12/13',9000,15);
insert into interiors
values(10,"Dine Paradise","Dining Table",'2006/02/19',11000,15);

Dhruv Tyagi -- 2 --
XII-A
Roll no.: 13
Computer Science Practical File

List all details where type is Sofa.


Output:

Code:
select * from interiors where type = ‘sofa’;

ii. List of items with price greater than 10000.


Output:

Code:
Select * from interiors where price > 10000;

Dhruv Tyagi -- 3 --
XII-A
Roll no.: 13
Computer Science Practical File

iii. To display item name and date of stock of those items, in which the
discount percentage is more than 15.
Output:

Code:
select itemname, dateofstock from interiors where discount > 15;

iv. To list item name and type of items, in which data of stock is before
22-Jan-2002 in descending order of item name.
Output:

Code:
select itemname, type from interiors where dateofstock < ‘2002/01/22’ order by
itemname desc;

v. To count the number of items of each type.

Output:

Code:
select type, count(*) from interiors group by type;

Dhruv Tyagi -- 4 --
XII-A
Roll no.: 13
Computer Science Practical File

vi. To insert new row in interiors table with following data:

11 White wood Double bed 23-Feb-2006 20000 20

Output:

Code:
insert into interiors
values(11,’White wood’,’Double bed’,’2006/02/23’,20000,20);

vii. To count distinct types from interiors table.


Output:

Code:
select count(distinct type) form interiors;

viii. To find average discount from interiors for each type of interiors.
Output:

Code:
select type, avg(discount) from interiors group by type;

Dhruv Tyagi -- 5 --
XII-A
Roll no.: 13
Computer Science Practical File

ix. To calculate sum of price from interiors where data of stock is before
12-Feb-2002
Output:

Code:
select sum(price) from interiors where dateofstock < ‘2002/2/12’;

x. To increase price of all Office table by 3000 from interiors table.


Output:

Code:
update interiors
set price = price+3000 where type = ‘Office table’;

Dhruv Tyagi -- 6 --
XII-A
Roll no.: 13
Computer Science Practical File

Q2 Create the tables and Write SQL queries for given tables.
CREATING THE TABLE:
Output:

Dhruv Tyagi -- 7 --
XII-A
Roll no.: 13
Computer Science Practical File

Code:
USE HHW
create table STOCK
(
NO int primary key,
ITEMCODE CHAR(20),
QUANTITY INT,
DATEOFPURCHASE DATE,
WARRANTY INT
);
insert into STOCK
values(1,"C201",9,'2005/05/21',2);
insert into STOCK
values(2,"P1010",3,'2005/05/21',4);
insert into STOCK
values(3,"S203",1,'2004/09/29',3);
insert into STOCK
values(4,"C201",2,'2005/06/13',1);
insert into STOCK
values(5,"P1010",1,'2004/10/31',2);
insert into STOCK
values(6,"U34",5,'2005/05/21',1);
insert into STOCK
values(7,"P1010",2,'2006/01/11',2);
create table ITEM
(
ITEMCODE CHAR(20) primary key,
ITEMNAME CHAR(20),
PRICE INT
);
insert into ITEM
values("C201",'COMPUTER',39000);
insert into ITEM
values("P1010",'PRINTER',11000);
insert into ITEM
values("S203",'SCANNER',4500);
insert into ITEM
values("WC05",'CAMERA',1200);
insert into ITEM
values("U34",'UPS',1900);

Dhruv Tyagi -- 8 --
XII-A
Roll no.: 13
Computer Science Practical File

i. To select item purchased after 31-Jan-2005.


Output:

Code:
select item.itemname from item,stock where item.itemcode = stock.itemcode and
stock.dateofpurchase > '2005/1/31';

ii. To list the item name in descending order of date of purchase where
quantity is more than three
Output:

Code:
select item.itemname from item,stock where item.itemcode = stock.itemcode and
quantity > 3 order by dateofpurchase desc;

iii. To count number of Items (total quantity) whose cost is more than
10000.
Output:

Code:
select sum(quantity) from stock, item where stock.itemcode = item.itemcode and
item.price > 10000;
Dhruv Tyagi -- 9 --
XII-A
Roll no.: 13
Computer Science Practical File

iv. To insert a new record in the table STOCK with following data:

8 C201 5 13-June-2005 1

Output:

Code:
insert into stock
values(8,'C201',5,'2005/06/13',1);

v. To find minimum distinct item type (itemcode) from STOCK.


Output:

Code:
select min(distinct itemcode) from stock;

vi. To generate a report on each item code with the item name and total
value
Output:

Code:
select itemname, sum(stock.quantity)*item.price as 'TOTAL VALUE' from item, stock
where stock.itemcode = item.itemcode group by itemname;

Dhruv Tyagi -- 10 --
XII-A
Roll no.: 13
Computer Science Practical File

vii. To find average cost of all items with more than 2 items.
Output:

Code:
select avg(price) as 'AVERAGE PRICE' from stock s, item i where quantity > 2 and
s.itemcode = i.itemcode;

viii. To find average cost of items with date of purchase is before 1-Jan-
2005.
Output:

Code:
select avg(price) as 'AVERAGE PRICE' from stock s, item i where dateofpurchase <
'2005/01/01' and s.itemcode = i.itemcode;

ix. To reduce the cost of all UPS by 100


Output:

Code:
update item
set price = price - 100 where itemname = 'UPS';

Dhruv Tyagi -- 11 --
XII-A
Roll no.: 13
Computer Science Practical File

x. To delete the table STOCK.


Output:

Code:
drop table stock;

Dhruv Tyagi -- 12 --
XII-A
Roll no.: 13
Computer Science Practical File

Q3 Write a menu driven program to add, subtract, multiply and


divide 2 complex numbers using structures.

Output:

Dhruv Tyagi -- 13 --
XII-A
Roll no.: 13
Computer Science Practical File

//Code
#include<iostream.h>
#include<conio.h>
struct com
{
float r;
float i;
}i1,i2,ans;
void add(com i1, com i2, com &ans)
{
ans.i = i1.i + i2.i;
ans.r = i1.r + i2.r;
}
void sub(com i1, com i2, com &ans)
{
ans.i = i1.i - i2.i;
ans.r = i1.r - i2.r;
}
void mul(com i1, com i2, com &ans)
{
ans.r = (i1.r * i2.r) - (i1.i * i2.i);
ans.i = (i1.r * i2.i) + (i2.r * i1.i);
}
void dev(com i1, com i2, com &ans)
{
ans.r = ((i1.r * i2.r) + (i1.i * i2.i)) / ((i2.r * i2.r) + (i2.i * i2.i));
ans.i = ((i1.i * i2.r) - (i1.r * i2.i)) / ((i2.r * i2.r) + (i2.i * i2.i));
}
void get(com &a)
{
cout << "Enter the value of A: ";
cin >> a.r;
cout << "Enter the value of B: ";

Dhruv Tyagi -- 14 --
XII-A
Roll no.: 13
Computer Science Practical File

cin >> a.i;


cout << endl;
}
void main()
{
clrscr();
int ch;
char op = 'y';
do
{
cout << "Select an option:" << endl;
cout << "\t1.) ADD " << endl;
cout << "\t2.) SUBTRACT " << endl;
cout << "\t3.) MULTIPLY " << endl;
cout << "\t4.) DEVIDE " << endl;
cin >> ch;
cout << "Enter the first complex number: " << endl;
cout << "X = A + Bi" << endl;
get(i1);
cout << "Enter the second complex number: " << endl;
get(i2);
switch(ch)
{
case 1: add(i1,i2,ans);break;
case 2: sub(i1,i2,ans);break;
case 3: mul(i1,i2,ans);break;
case 4: dev(i1,i2,ans);break;
}
cout << "ANSWER = " << ans.r << " + " << ans.i << " i" << endl;
cout << "Do you want to run the program again? (Y/N)\n";
cin >> op;
}
while(op == 'y' || op == 'Y'); }

Dhruv Tyagi -- 15 --
XII-A
Roll no.: 13
Computer Science Practical File

Q4 Write a menu driven program to show upper triangle, lower


triangle of a 2D matrix
Output:

Dhruv Tyagi -- 16 --
XII-A
Roll no.: 13
Computer Science Practical File

//Code:
#include <iostream.h>
#include <conio.h>
void up(int a[50][50], int r, int c)
{
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j ++)
if(j <= i)
cout << 0 << ' ';
else
cout << a[i][j] << ' ';
cout << endl; }
}
void dow(int a[50][50], int r, int c)
{
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j ++)
if(j >= i)
cout << 0 << ' ';
else
cout << a[i][j] << ' ';
cout << endl; }
}
void get(int a[50][50], int &r, int &c)
{
cout << "Enter the no. of rows and columns:" << endl;
cin >> r >> c;
cout << "Enter the elements of the array:" << endl;
for(int i = 0; i < r; i++)
{
cout << "Row " << i+1 << " : " <<endl;
for(int j = 0; j < c; j ++)
{
cout << "\t Column " << j+1 << " : ";
cin >> a[i][j];

Dhruv Tyagi -- 17 --
XII-A
Roll no.: 13
Computer Science Practical File

}
}
}

void main()
{
clrscr();
int r,c,op,a[50][50];
char ch = 'y';
get(a,r,c);
do
{
cout << "Enter an option:" << endl;
cout << "\t1.)Upper triangle:" << endl;
cout << "\t2.)Lower triangle:" << endl;
cin >> op;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
cout << a[i][j] << ' ';
cout << endl;
}
cout << endl;
switch(op)
{
case 1: up(a,r,c); break;
case 2: dow(a,r,c); break;
}
cout << "Do you want to re-run the program? " << endl;
cin >> ch;
}
while (ch == 'y' || ch == 'Y');
}

Dhruv Tyagi -- 18 --
XII-A
Roll no.: 13
Computer Science Practical File

Q5 Write a menu driven program to show sum of primary


diagonal and sum of secondary diagonal of a 2D matrix.
Output:

Dhruv Tyagi -- 19 --
XII-A
Roll no.: 13
Computer Science Practical File

//Code:
#include<iostream.h>
#include<conio.h>
void prim(int a[10][10], int r,int c)
{
int sum = 0;
for(int i = 0; i < r; i++)
for(int j = 0; j < c; j++)
if(i==j)
sum += a[i][j];
cout << "Sum of primary diagomal = " << sum << endl;
}
void sec(int a[10][10], int r,int c)
{
int sum = 0;
for(int i = 0; i < r; i++)
for(int j = 0; j < c; j++)
if(i+j == r-1)
sum += a[i][j];
cout << "Sum of secondary diagonal diagomal = " << sum << endl;
}
void get(int a[10][10], int &r, int &c)
{
do
{
cout << "Enter the no. of rows and columns:" << endl;
cin >> r >> c ;
if(r == c)
{
cout << "Enter the elements:" << endl << endl;
for(int i = 0; i < r; i++)
{
cout << "Row " << i+1 << " : " << endl;
for(int j = 0; j < c; j++)
{
cout << "\tColumn " << j+1 << " : ";
cin >> a[i][j];
}
}

Dhruv Tyagi -- 20 --
XII-A
Roll no.: 13
Computer Science Practical File

}
else
cout << "NOT A SQUARE MATRIX!!! " << endl;
}
while(r != c);
}
void disp(int a[10][10], int r,int c)
{
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
cout << a[i][j] << ' ';
cout << endl;
}
}
void main()
{
clrscr();
int ch,a[10][10],r,c;
char op;
do
{
clrscr();
cout << "Enter a choice: " << endl;
cout << "\t1.) Sum of primary diagonal." << endl;
cout << "\t2.) Sum of secondary diagonal." << endl;
cin >> ch;
get(a,r,c);
cout << endl << "The entered array is:" << endl;
disp(a,r,c);
switch(ch)
{
case 1:prim(a,r,c); break;
case 2:sec(a,r,c); break;
default: cout << "Wrong choice!" << endl;break;
}
cout << "Do you want to continue?" << endl;
cin >> op;
}while (op == 'y' || op == 'Y');
}
Dhruv Tyagi -- 21 --
XII-A
Roll no.: 13
Computer Science Practical File

Q6 Write a menu driven program for addition, subtraction and


multiplication of 2 matrices.
Output:

Dhruv Tyagi -- 22 --
XII-A
Roll no.: 13
Computer Science Practical File

//Code
#include<iostream.h>
#include<conio.h>
#include<process.h>
void disp(int a[10][10],int r, int c)
{
for(int i = 0; i < r; i++)
{
for(int j = 0 ; j < c; j++)
cout << a[i][j] << ' ';
cout << endl;
}
Cout << endl;
}
void add(int a[10][10],int b[10][10],int r1, int c1,int r2, int c2)
{ int d[10][10];
if(r1 == r2 && c1 == c2)
{
for(int i = 0; i < r1; i++)
for(int j = 0; j < c1; j++)
d[i][j] = a[i][j] + b[i][j];
cout << "Array after addition:" << endl << endl;
disp(d,r1,c1);
}
else
cout << "ADDITION NOT POSSIBLE!!!" << endl;
}
void sub(int a[10][10],int b[10][10],int r1, int c1,int r2, int c2)
{
int d[10][10];
if(r1 == r2 && c1 == c2)
{
for(int i = 0; i < r1; i++)
for(int j = 0; j < c1; j++)
d[i][j] = a[i][j] - b[i][j];
cout << "Array after subtracation:" << endl << endl;
disp(d,r1,c1);
}
else

Dhruv Tyagi -- 23 --
XII-A
Roll no.: 13
Computer Science Practical File

cout << "SUBTRACTION NOT POSSIBLE!!!" << endl;


}
void mul(int a[10][10],int b[10][10],int r1, int c1,int r2, int c2)
{
int d[10][10];
if(c1 != r2)
{
cout << "MULTIPLICATION NOT POSSIBLE!!!" << endl;
exit(0);
}
for (int i = 0; i < r1; i++)
for (int j = 0; j < c2; j++)
{
d[i][j] = 0;
for (int k = 0; k < r2; k++)
d[i][j] += a[i][k] * b[k][j];
}
cout << "Array after multiplication:" << endl << endl;
disp(d,r1,c2);
}
void get(int a[10][10],int &r, int &c)
{
cout << "Enter the rows and columns for the matrix:" << endl;
cin >> r >> c;
cout << "Enter the elements:" << endl << endl;
for(int i = 0; i < r; i++)
{ cout << "Row " << i+1 << " : " << endl;
for(int j = 0; j < c; j++)
{ cout << "\tColumn " << j+1 << " : ";
cin >> a[i][j];
}
}

}
void main()
{
clrscr();
int a[10][10],b[10][10],r1,r2,c1,c2,ch;
char op = 'y';
do
Dhruv Tyagi -- 24 --
XII-A
Roll no.: 13
Computer Science Practical File

{
clrscr();
cout << "Enter a choice: " << endl;
cout << "\t1.) Sum of matrices." << endl;
cout << "\t2.) Difference of matrices." << endl;
cout << "\t3.) Product of matrices." << endl;
cin >> ch;
cout << "Enter the first matrix:" << endl;
get(a,r1,c1);
cout << "Enter the second matrix:" << endl;
get(b,r2,c2);
cout << endl << "Matrix 1: " << endl;
disp(a,r1,c1);
cout << endl << "Matrix 2: " << endl;
disp(b,r2,c2);
switch(ch)
{
case 1: add(a,b,r1,c1,r2,c2);break;
case 2: sub(a,b,r1,c1,r2,c2);break;
case 3: mul(a,b,r1,c1,r2,c2);break;
}
cout << endl << "Do you want to continue?" << endl;
cin >> op;
}while (op == 'y' || op == 'Y');
}

Dhruv Tyagi -- 25 --
XII-A
Roll no.: 13
Computer Science Practical File

Q7 Write a menu driven program to find row sum and column


sum in a 2D array.
Output:

Dhruv Tyagi -- 26 --
XII-A
Roll no.: 13
Computer Science Practical File

//Code
#include<iostream.h>
#include<conio.h>
void disp(int a[10][10],int r, int c)
{
for(int i = 0; i < r; i++)
{ for(int j = 0 ; j < c; j++)
cout << a[i][j] << ' ';
cout << endl;
}
}
void rowsu(int a[10][10], int r, int c)
{
int sum;
for(int i = 0; i < r; i++)
{ sum = 0;
for(int j = 0; j < c; j++)
sum += a[i][j];
cout << "Sum of row " << i+1 << " is: " << sum << endl;
}
}
void colsu(int a[10][10], int r, int c)
{
int sum;
for(int i = 0; i < c; i++)
{
sum = 0;
for(int j = 0; j < r; j++)
sum += a[j][i];
cout << "Sum of column " << i+1 << " is: " << sum << endl;
}
}
void get(int a[10][10],int &r, int &c)
{
cout << "Enter the rows and columns for the matrix:" << endl;
cin >> r >> c;
cout << "Enter the elements:" << endl << endl;
for(int i = 0; i < r; i++)
{

Dhruv Tyagi -- 27 --
XII-A
Roll no.: 13
Computer Science Practical File

cout << "Row " << i+1 << " : " << endl;
for(int j = 0; j < c; j++)
{ cout << "\tColumn " << j+1 << " : ";
cin >> a[i][j];
}
}

}
void main()
{
clrscr();
int op, r, c, a[10][10];
char ch;
do
{
clrscr();
cout << "Select an option:" << endl;
cout << "\t1.)Row sum." << endl;
cout << "\t2.)column sum." << endl;
cin >> op;
get(a,r,c);
cout << endl;
disp(a,r,c);
switch (op)
{
case 1: rowsu(a,r,c);break;
case 2: colsu(a,r,c);break;
}
cout << "Do you want to run the program again? (y/n)" << endl;
cin >> ch;
}
while (ch == 'y' || ch == 'Y');
}

Dhruv Tyagi -- 28 --
XII-A
Roll no.: 13
Computer Science Practical File

Q8 Write a menu driven program to search and display the


details of the students from the array of students
Output:

Dhruv Tyagi -- 29 --
XII-A
Roll no.: 13
Computer Science Practical File

//Code
include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct stud
{
int roll;
int marks;
char name[30];
}a[10],temp;
void disp(stud a)
{
cout << "Name :";
puts(a.name);
cout << "Roll no.: " << a.roll << "\nMarks : " << a.marks << endl;
}
void linear (stud a[10], int n, int ele)
{
for (int i = 0; i < n; i++)
if(a[i].marks == ele)
disp(a[i]);
}
void bub(stud a[10], int n)
{

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


for(int j = 0; j < n-1-i; j++)
if(a[j].marks > a[j+1].marks)
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
void get(stud a[10], int &n)
{
cout << "Enter the no of students: ";
cin >> n;
for(int i = 0; i < n; i++)

Dhruv Tyagi -- 30 --
XII-A
Roll no.: 13
Computer Science Practical File

{
cout << "STUDENT " << i+1 << " : " << endl;
cout << "\tEnter the name: ";
gets(a[i].name);
cout << "\tEnter the roll number: ";
cin >> a[i].roll;
cout << "\tEnter the marks: ";
cin >> a[i].marks;
}
cout << endl;
}
void bin(stud a[10], int n, int ele)
{
bub(a,n);
int start = 0, end = n-1, mid = (start + end) / 2;
while(start <= end)
{
mid = (start + end)/2;
if(ele == a[mid].marks)
{
disp(a[mid]);
return;
}
else if(ele < mid)
end = mid-1;
else
start = mid+1;
}
}
void main()
{
clrscr();
int op, n, ele;
char ch;
do
{
clrscr();
cout << "Select an option:" << endl;
cout << "\t1.)Linear search." << endl;
cout << "\t2.)Binary search." << endl;
Dhruv Tyagi -- 31 --
XII-A
Roll no.: 13
Computer Science Practical File

cin >> op;


get(a,n);
cout << endl;
cout << "Enter the element to be searched: ";
cin >> ele;
switch (op)
{
case 1: linear(a,n,ele);break;
case 2: bin(a,n,ele);break;
}
cout << "Do you want to run the program again? (y/n)" << endl;
cin >> ch;
}
while (ch == 'y' || ch == 'Y');
}

Dhruv Tyagi -- 32 --
XII-A
Roll no.: 13
Computer Science Practical File

Q9 Write a program to input integer data in two arrays.


Output:

Dhruv Tyagi -- 33 --
XII-A
Roll no.: 13
Computer Science Practical File

//Code
#include<iostream.h>
#include<conio.h>
void asc(int a[100], int n)
{
int temp;
for(int i = 0; i < n-1; i++)
for(int j = 0; j < n-i-1; j++)
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
void desc(int a[100], int n)
{ int temp;
for(int i = 0; i < n-1; i++)
for(int j = 0; j < n-i-1; j++)
if(a[j] < a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
void get(int a[100],int &n)
{
cout << "Enter the numer of elements of the array:" << endl;
cin >> n;
cout << "Enter the array:" <<endl;
for(int i = 0; i < n; i++)
cin >> a[i];
}
void disp(int a[100],int n)
{
cout << "The array is : " ;
for(int i = 0; i < n; i++)
cout << a[i] << ' ';

Dhruv Tyagi -- 34 --
XII-A
Roll no.: 13
Computer Science Practical File

cout << endl;


}
void merge(int a[100], int b[100], int c[100], int n1, int n2)
{
int i = 0, j = n2-1, k = 0;
for(; i < n1 && j >= 0;)
{
if(a[i] < b[j])
c[k++] = a[i++];
else
c[k++] = b[j--];
}
if(i < n1)
while(i != n1+1)
c[k++] = a[i++];
else if(j > 0)
while(j >= 0)
c[k++] = b[j--];
}
void main() //Perform merge sort(ADA)
{ clrscr();
int a[100],b[100], c[100], n1, n2;
get(a,n1);
get(b,n2);
asc(a,n1);
desc(b,n2);
disp(a,n1);
disp(b,n2);
merge(a,b,c,n1,n2);
disp(c,n1+n2);
getch();
}

Dhruv Tyagi -- 35 --
XII-A
Roll no.: 13
Computer Science Practical File

Q10 Write a menu driven program which allows the user to


perform operations on a one dimensional array
Output:

Dhruv Tyagi -- 36 --
XII-A
Roll no.: 13
Computer Science Practical File

//Code:
#include<iostream.h>
#include<conio.h>
#include <limits.h>
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void selsort(int a[100], int n)
{
for(int i = 0; i < n-1; i++)
for(int j = i+1; j < n; j++)
if(a[j] < a[i])
swap(a[j], a[i]);
}
void inssort(int a[100], int n)
{
for(int uio = n; uio >= 0; uio--)
a[uio] = a[uio-1];
n++;
a[0] = INT_MIN;
int temp,j = 0;
for(int i = 1; i < n; i++)
{
temp = a[i];
j = i-1;
while(temp < a[j])
{
a[j+1] = a[j];
j--;
}
a[j+1] = temp;
}
}
void insert(int a[100], int &n)
{

Dhruv Tyagi -- 37 --
XII-A
Roll no.: 13
Computer Science Practical File

cout << "Enter the element to be inserted:";


int ele, pos;
cin >> ele;
for(int i = n-1; (i >= 0) && (ele < a[i]) ; i--)
pos = i;
for(int j = n; j > pos; j--)
a[j] = a[j-1];
a[pos] = ele;
n++;
}
void disp(int a[100], int n)
{
cout << "The array is:" << endl;
for(int i = 0; i < n; i++)
cout << a[i] << ' ';
cout << endl;
}
void get(int a[100], int &n)
{
cout << "Enter the no of elements in the array: ";
cin >> n;
cout << "Enter the array: " << endl;
for(int i = 0; i < n; i++)
cin >> a[i];
cout << endl;
}
void deletex(int a[100], int &n)
{
cout << "Enter the element to be deleted:";
int ele, pos;
cin >> ele;
for(int i = 0; i < n; i++)
if(a[i] == ele)
{
pos = i;
break;
}
for(int j = pos; j < n; j++)
a[j] = a[j+1];
n--;
Dhruv Tyagi -- 38 --
XII-A
Roll no.: 13
Computer Science Practical File

}
void main()
{
clrscr();
int a[100], n, choice;
char op = 'y';
get(a,n);
disp(a,n);
do
{
clrscr();
cout << "Select an option:" << endl;
cout << "\t1.)Sorting and Insertion." <<endl;
cout << "\t2.)Sorting and Deletion." <<endl;
cout << "\t3.)Sorting." <<endl;
cout << "\t4.)Display." <<endl;
cin >> choice;

if(choice == 1)
{
cout << "Which sorting algorithm should be used? (1 = insertion
sort, 2 = selection sort)" << endl;
cin >> choice;
if(choice == 1)
inssort(a,n);
else
selsort(a,n);
insert(a,n);
}
else if(choice == 2)
{
cout << "Which sorting algorithm should be used? (1 = insertion
sort, 2 = selection sort)" << endl;
cin >> choice;
if(choice == 1)
inssort(a,n);
else
selsort(a,n);
deletex(a,n);
}
Dhruv Tyagi -- 39 --
XII-A
Roll no.: 13
Computer Science Practical File

else if(choice == 3)
{
cout << "Which sorting algorithm should be used? (1 = insertion
sort, 2 = selection sort)" << endl;
cin >> choice;
if(choice == 1)
inssort(a,n);
else
selsort(a,n);
}
else if(choice == 4)
disp(a,n);
cout << "Do you want to perform more operations?" << endl;
cin >> op;
}
while(op == 'y' || op == 'Y');
disp(a,n);
cout << "Press any key to end the program." << endl;
getch();
}

Dhruv Tyagi -- 40 --
XII-A
Roll no.: 13

You might also like