Sunteți pe pagina 1din 69

LIST OF C++ PROGRAMS

SIGNATURE
S.NO DATE PROGRAM NAME OF THE
TEACHER
STUDENT RESULT PREPARATION SYSTEM USING
1 CLASS

BANK ACCOUNT SIMULATION USING CLASS


2

INVENTORY SIMULATION USING CLASS


3

4 FUNCTION OVERLOADING WITHOUT USING CLASS

MULTIPLE INHERITANCE
5

MULTILEVEL INHERITANCE
6

7 CONSTRUCTOR OVERLOADING

WRITING AND READING A TEXT FILE CONTENT


8

COUNTING LINES IN A TEXT FILE


9

BINARY FILE MANIPULATION(INSERT,SEARCH AND


10 DISPLAY)

BINARY FILE MANIPULATION(DELETE AND


11 DISPLAY)

BINARY FILE MANIPULATION(UPDATE AND


12 DISPLAY)

13 LINEAR SEARCH

14 BINARY SEARCH

15 MATRIX TRANSPOSE

ROW SUM,COLUMN SUM AND DIAGONAL SUM OF A


16
SQUARE MATRIX
17 INSERTION SORT

18 BUBBLE SORT

19 STACK IMPLEMENTATION USING LINKED LIST

20 QUEUE IMPLEMENTATION USING LINKED LIST

21 BASICS OF SQL 1 (DDL COMMANDS)

22 BASICS OF SQL 2 (DML COMMANDS)

QUERIES AND OUTPUTS-1


23

24 QUERIES AND OUTPUTS-2


1. STUDENT RESULT PREPARATION SYSTEM USING CLASS
AIM: Write a C++ program for student result preparation as per the following constraints

class descriptions:
data members:
student rollno: integer
student name:character array
student marks:float array
student average:float
student grade: character
member functions:
public:
void read();
void display();
void calgrade();

Grade Formula

percentage of Grade
marks
80 to 100 A
60 to 80 B
45 to 60 C
less than 45 D

CODING:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
//using namespace std;
class student
{
int rollno;
char name [20];
float marks[];
float average;
char grade;
public:
void read();
void display();
protected:
void calgrade();
}s[10];
int n;
void student::read()
{
cout<<”enter roll no:”;
cin >>rollno;
cout<<”enter name:”;
cin>>name;
cout<<”enter marks:”;
for(int i=0;i<n;i++)
cin>>marks[i];
calcgrade();
}
void student:: display()
{
cout<<”name:”<<name<<endl;
cout<<”rollno:”<<rollno<<endl;
cout<<”average:”<<(int)average<<endl;
cout<<”grade:”<<grade<<endl;
}
void student::calcgrade()
{
float total=0;
for(int i=0;i<n;i++)
total=total+marks[i];
average=total/n;
if(average<45)
grade=’d’;
else if(average<60)
grade=’c’;
else if(average<80)
grade=’b’;
else
grade=’a’;
}
void main()
{
int num;
cout<<”enter the no of students:”;
cin>>num;
cout<<”enter the no of subjects”;
cin>>n;
for(i=0;i<num;i++)
{
s[i].read();
s[i].calcgrade();
}
cout<<”student report\n”;
for(i=0;i<num;i++)
{
s[i].display();
}
getch();
}
INPUT AND OUTPUT

Result:

Thus the program has been written in C++ for generating student report using class in C++

2. BANK ACCOUNT SIMULATION USING CLASS


Aim: To simulate banking software which allows multiple customers and give them options to
deposit/withdraw money. It should have a view option. Define a class bank in C++ with the
following description:

Data members:
Name of the depositor :string
Account number :long
Type of account (saving or current) :character
Balance amount :decimal
Member function:
Initialize () : it reads the customer name, account number, account type, current balance
and initialize the object.
Deposit(): increases the balance amount
Withdraw() :checks for minimum balance in current account and decreases the balance
accordingly
Display() :display the customer details

Coding:
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
//using namespace std;
class account
{
char cname[30];
long accno;
char acctype;
double balance;
public:
void initialize()
{
cout<<" enter name";
gets(cname);
cout<<"enter account number";
cin>>accno;
int done=1;
do
{
cout<<" enter account type current-c and saving-s";
cin>>acctype;
acctype=tolower(acctype);
if(acctype=='s')
balance=1000.0;
else if(acctype=='s')
balance=0.0;
else
{
cout<<"invalid account type";
done=0;
}
}while(done!=1);
}
void display()
{
cout<<"account holder name";
puts(cname);
cout<<"\naccount number"<<accno;
cout<<"\naccount type:"<<acctype<<"\t"<<"balance:"<<balance<<endl;
}
void deposit(double amt);
void withdraw(double amt);
};
void account::deposit(double amt)
{
balance=balance+amt;
cout<<"amount deposited"<<endl;
display();
}
void account::withdraw(double amt)
{
if(acctype=='c')
{
if(balance>=1000)
{
balance=balance-amt;
cout<<"amount widthdrawn";
display();
}
else
cout<<"min balance 1000 is needed for this account";
}
else
{
if(balance>=amt)
{
balance=balance-amt;
cout<<"amount width drawn";
display();
}
else
cout<<"amount exceed the balance";
}
}
void main()
{
account a[10];
int i=0,choice,cont;
char ch;
double d;
do
{
a[i].initialize();
do
{
cout<<"main menu 1-deposit 2 -withdraw 3-display\n";
cin>>choice;
switch(choice)
{
case 1:
cout<<"enter the amount to be deposited";
cin>>d;
a[i].deposit(d);
break;
case 2:
cout<<"enter the amount to be withdrawn";
cin>>d;
a[i].withdraw(d);
break;
case 3:
a[i].display();
break;
default:
cout<<"thank you";
}
cout<<"do you want to continue? enter 1";
cin>>cont;
}while (cont==1);

cout<<"are there more customers";


cin>>ch;
i++;
}while(ch=='y');
getch();
}

Result:

Thus the program has been written in C++ to simulate banking software which allows multiple
customers and give them options to deposit/withdraw money

3. INVENTORY SIMULATION USING CLASS


Aim: To write a program in C++ using classes for inventory simulation based on the constraints
listed below.
Use an array of objects .The stock class has the following members:
Data members:
item code :integer
item name : string
price : float
qty :integer
discount :float
Member functions:
calculatediscount() :to calculate discount based on the following table
Inventaory Discount Details:
Quantity Discount
<=50 0
=0 and <=100 10
>100 20
get() :to get item details
show() :to display item details

Codings:

#include<iostream.h>
#include<conio.h>
using name space std;
float sum=0.0;
class stock
{
int icode;
char itemname[20];
float price;
int qty;
float discount;
private:
void calculatediscount()
{
if (qty<50)
discount=0;
else if(qty<100)
discount=10;
else
discount=20;
}
public:
void get()
{
cout<<”enter the item code”;
cin>>icode;
cout<<”enter item name”;
gets(itemname);
cout<<”enter price”;
cin>>price;
cout<<”enter quantity”;
cin>>qty;
calculatediscount();
}
void show()
{
float totprice=qty*price;
float final price=totprice-totprice*discount/100;
sum=sum+finalprice;
cout<<”itemcode:”<<icode<<endl;
cout<<”itemname:”<<itemname<<endl;
cout<<”qty:”<<qty<<endl;
cout<<”price:”<<price<<endl;
}
};
void main()
{
stock s[100];
int i=0;
char ch;
do
{
s[i].get();
s[i].show();
cout<<”are there any more items?”;
cin>>ch;
}while (ch==’y’);
cout<<”total bill amount=”<<sum;
getch();
}
Result: Thus we have written a program in C++ using classes for inventory simulation and compiled
and executed it successfully.
4. FUNCTION OVERLOADING

Aim: To write a program to find area of triangle, rectangle and circle to demonstrate Function
Overloading concept.

Coding:

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<math.h>
//function 1
void area(float r)
{
float area_c;
area_c=(3.14*r*r);
cout<<"Area of the circle="<<area_c<<endl;
}
//function 2
void area(float l,float h)
{
float area_r;
area_r=l*h;
cout<<"Area of the rectangle = "<<area_r<<endl;
}
//function 3
void area(float a,float b,float c)
{
float s,area_t ; s=(a+b+c)/2;
area_t=pow((s*(s-a)*(s-b)*(s-c)),0.5);
cout<<"Area of the triangle = "<<area_t<<endl;
}

void main()
{
clrscr();
int ch;
float r,a,b,c,l,h;
cout<<"1.Area of circle"<<endl<<"2.Area of rectangle"<<endl;cout<<"3.Area of
triangle"<<endl<<"4.Exit"<<endl;

X:
cout<<"Enter the choice"<<endl;
cin>>ch;

switch(ch)
{
case 1:cout<<"Enter the radius of the circle"<<endl;
cin>>r;
void area (float);
area(r);
goto X;
case 2:cout<<"Enter the sides of rectangle"<<endl;
cin>>l>>h; void area (float,float);
area (l,h);
goto X;
case 3:cout<<"Enter the sides of triangle"<<endl;
cin>>a>>b>>c;
void area (float,float,float);
area (a, b,c);
goto X;
case 4:exit(0);
default:cout<<"Wrong Choice"<<endl;
exit(1);
}
getch();
}

Result: thus we have written a program to find area of triangle, rectangle and circle to demonstrate
Function Overloading and it is compiled and executed successfully.
5. MULTIPLE INHERITANCE

Aim:To write a C++ program to demonstrate multiple inheritance. Consider 3 classes with the
following description.

Class name Data Members Member Functions


Base class :student protected: int rno,m1,m2; public: void get()

Base class :sports Protected :int msm; Public: void getsm()

Derived class: statement Private: int tot,avg; public:void display()


public student, public sports

Coding:

#include <iostream.h>
#include<conio.h>
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<” enter the rollno:”;
cin>>rno;
cout<<”enter the two marks:”;
cin>>m1>>m2;
}
};
class sports
{
protected:
int msm;
public:
void getsm()
{
cout<<”\n enter the sports marks:”;
cin>>sm;
}
};

class statement: public student, public sports


{
int tot,avg;
public:
void display()
{
tot=m1+m2+sm;
avg=tot/3;
cout<<”\n\n\t rollno:”<<rno<<”\n\ttotal:”<<tot;
cout<<”\n\taverage:”<<avg;
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}

Result: Thus we have written a C++ program to demonstrate multiple inheritance and it is compiled
and executed successfully.
6. MULTILEVEL INHERITANCE
Aim: To write a program in C++ to demonstrate multilevel inheritance as per the given
constraints below:
In base class person:
read() : to get the name and age
display() : to display the name and age
In derived class student:public person
readstudent() : to get the rollno and average mark
disprollno() : to display the rollno and average mark
float getaverage(): to return average mark
In derived class gradstudent:public student
void readgrade() :call readstudent(),get subject and get employed or not
char workstatus() : return working status
void displaysubject() :print the subject

Coding:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
using namespace std;
class person
{
char name[20];
int age;
public:
void read()
{
cout<<”enter name”;
gets(name);
cout<<”enter age”;
cin>>age;
}
void disp()
{
cout<<”name”<<name<<endl;
cout<<”age”<<age<<endl;
}
};
class student:public person
{
int rollno;
float average;
public:
void readstudent()
{
read();
cout<<”enter roll no”;
cin>>rollno;
cout<<”enter average marks”;
cin>>average;
}
void disprollno()
{
cout<<”roll no”:”<<rollno<<endl;
}
float getaverage()
{
return average;
}
};
class gradstudent:public student
{
char subject[5];
char working;
public:
void readgrade()
{
readstudent();
cout<<”enter subject”;
cin>>subject;
cout<<”working?y/n”;
cin>>working;
}
char workstatus()
{
return working;
}
void displaysubject()
{
cout<<”subject:”;
cout.write(subject,5);
}
};
void main()
{
int i;
const int size=2;
gradstudent g[size];
int year,total=0,n=0,w=0,pos;
float topscore=0,score;
cout<<”enter year:”;
cin>>year;
for(i=0;i<size;i++)
{
cout<<”enter the details of graduate””<<i+1<<endl;
g[i].readgrade();
total++;
if(g[i].workstatus()==’y’)
n++;
else
w++;
score=g[i].getaverage();
if(score>topscore)
topscore=score;

pos=i;
}

cout<<”report for the year”<<year<<endl;


cout<<”working graduates”<<n<<endl;
cout<<”non-working graduate”<<w<<endl;
cout<<”details of top scorer”;
g[pos].disp();
g[pos].displaysubject();
getch();
}
Result: Thus we have written program in C++ to implement multilevel inheritance and it was
compiled and executed successfully.
7. CONSTRUCTOR OVERLOADING
Aim: To write a program in C++ to demonstrate constructor overloading for simple interest
calculation

Formula:
Simple Interest=p*n*r/100;
Constructor signature:
si();
si(long p,int n,float r);
si(long p,int n);
si(long p,float r);
Coding:
#include<iostream.h>
#include<conio.h>
using namespace std;
class si
{
int p;
int years;
float rate;
float interest;
float amt;
public:
si();
si(int p,int n,float r);
si(int p,int n);
si(int p,float r);
void calculate();
void display();
};
si::si()
{
p=years=rate=0;
}
si:: si(int p,int n,float r)
{
cout<<” constructor with three arguments”<<endl;
p=p;
years=n;
rate=r;
}

si:: si(int p,int n)


{
cout<<” constructor with 2 arguments with fixed rate”<<endl;
p=p;
years=n;
rate=0.06;
}

si:: si(int p,float r)


{
cout<<” constructor with 2 arguments with fixed year”<<endl;
p=p;
years=2;
rate=r;
}
void si:: calculate()
{
interest=p*years*rate;
amt=p+interest;
}
void si::display()
{
cout<<”principal:”<<p<<endl;
cout<<”period:”<<years<<endl;
cout<<”rate:”<<rate<<endl;
cout<<”total:”<<amt<<endl;
}
void main()
{
si s1(2000,5);
s1.calculate();
s1.display();

si s2(2000,2.0,0.08f);
s2.calculate();
s2.display();

si s3(2000,0.10f);
s3.calculate();
s3.display();

getch();

}
Result: Thus we have written a program in c++ and it is complied and executed successfully.
8. WRITING AND READING A FILE CONTENT
Aim: To write a program in C++ for storing the country names and capital names in 2 text files and
printing both of them simultaneously

Coding:
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

//using namespace std;


void main()
{
char s[100];
clrscr();
fstream f1,f2;
f1.open("country.txt",ios::out);
f2.open("capital.txt",ios::out);
cout<<"enter the country names enter 'e' to finish\n";
do
{
gets(s);
if(strcmp(s,"e")!=0)
f1<<s<<"\n";
else
break;
}while(1);

cout<<"enter the capital names enter 'e' to finish\n";


do
{
gets(s);
if(strcmp(s,"e")!=0)
f2<<s<<"\n";
else
break;
}while(1);

f1.close();
f2.close();

f1.open("country",ios::in);
f2.open("capital",ios::in);
char str[80];
cout<<" the content of the files are…\n";
cout<<"country\t\tcapital\n";

while(f1&&f2)
{
f1.getline(str,80);
cout<<str<<"\t\t";
f2.getline(str,80);
cout<<str<<"\n";
}
getch();
}

Result: Thus we have written a program in C++ based on above constraints and it is complied and
executed successfully.
9. COUNTING LINES IN A TEXT FILE
Aim: To write a file program to count number of lines in an already existing file.

Coding:
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<process.h>
//using namespace std;
void main()
{
ifstream fin;
clrscr();
fin.open("pro1.cpp");
if(!fin)
{
cout<<"error";
exit(0);
}
char str[80];
int count=0;
while(!fin.eof())
{
fin.getline(str,80);
count++;
}
cout<<"number of lines in file is"<<count;
fin.close();
getch();
}

OUTPUT:
number of lines in file is 75

Result: thus we have written a file program to count number of lines in an already existing file and it
is compiled and executed successfully.
10. BINARY FILE MANIPULATION(INSERT,SEARCH AND DISPLAY)

Aim: To write a program in C++ to insert,search and display the details of all students from a
binary file “student.dat”.assuming the record structure is:
struct student
{
int rollno;
char name[20];
char grade;
};

Coding:
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
//using namespace std;
struct stu
{
int rollno;
char name[20];
char grade;
}s;
void display()
{
fstream f;
cout<<" the matched data is \n";
f.open("student.dat",ios::in);
cout<<s.rollno<<"\t"<<s.name<<"\t"<<s.grade<<endl;
f.close();
}

void main()
{
clrscr();
fstream fout("student.dat",ios::out);
ofstream ftemp;
char ch;
int n,i;
int flag=1,rno;
int choice;
cout<<"enter the number of students";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"enter rollno,name and grade of the students";
cin>>s.rollno>>s.name>>s.grade;
fout.write((char*)&s,sizeof(s));
}
fout.close();
flag=1;
fout.open("student.dat",ios::in);
cout<<"enter the roll to be searched";
cin>>rno;
while(fout.read((char*)&s,sizeof(s)))
{
if(s.rollno==rno)
{
display();
flag=0;
}
}
if (flag==1)
cout<<"roll number not found";
else
cout<<"roll number is found";
fout.close();

getch();
}

Result: Thus we have written a program in C++ to insert,search and display the file contents
as required and it is compiled and executed successfully.
11. BINARY FILE MANIPULATION ( DELETE AND DISPLAY)

Aim: To write a program in C++ to delete and display the details of all students from a binary
file “student.dat”, assuming the record structure is
struct student
{
int rollno;
char name[20];
char grade;
};

Coding:
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
//using namespace std;
struct student
{
int rollno;
char name[20];
char grade;
}s;
void display()
{
fstream f;
cout<<" the content of the student.dat are\n";
f.open("student.dat",ios::in);
f.clear();
f.seekg(0);
while(f.read((char*)&s,sizeof(s)))
{
cout<<s.rollno<<"\t"<<s.name<<"\t"<<s.grade<<endl;
}
f.close();
}
void main()
{
clrscr();
fstream fout("student.dat",ios::out);
ofstream ftemp("temp.dat",ios::out);
char ch;
int n,i;
int flag,rno;
int choice;
cout<<"enter the number of students";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"enter rollno,name and grade of the students";
cin>>s.rollno>>s.name>>s.grade;
fout.write((char*)&s,sizeof(s));
}
fout.close();

flag=1;
fout.open("student.dat",ios::in);
cout<<" enter the roll to be deleted";
cin>>rno;
fout.clear();
fout.seekg(0);
while(fout.read((char*)&s,sizeof(s)))
{
if(s.rollno!=rno)
{
ftemp.write((char*)&s,sizeof(s));
}
else
{
flag=0;
}
}
ftemp.close();
fout.close();
if(remove("student.dat")!=0)
{
cout<<"error in deletion";
}
rename("temp.dat","student.dat");

cout<<"\nafter deletion..\n";
if(flag==0)
{
cout<<"item found and it is deleted";
display();
}
else
{
cout<<"item is not found";
display();
}
getch();
}

Result: Thus we have written a program in C++ to delete and display the contents of the file
and it is complied and executed successfully.
12. BINARY FILE MANIPULATION(UPDATE AND DISPLAY)
Aim: To write a program in C++ to update and display the details of all students from a
binary file “student.dat”, assuming the record structure is
struct student
{
int rollno;
char name[20];
char grade;
};
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
//using namespace std;
struct student
{
int rollno;
char name[20];
char grade;
}s;
void display()
{
fstream f;
cout<<"the content of the student.dat are\n";
f.open("student.dat",ios::in);
while(f.read((char*)&s,sizeof(s)))
{
cout<<s.rollno<<"\t"<<s.name<<"\t"<<s.grade<<endl;
}
f.close();
}
void main()
{
clrscr();
fstream fout("student.dat",ios::out);
ofstream ftemp("temp.dat",ios::out);
char ch;
int i,n;
int flag=1,rno;
int choice;
cout<<"enter the number of students\n";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"enter rollno,name and grade of the students";
cin>>s.rollno>>s.name>>s.grade;
fout.write((char*)&s,sizeof(s));
}
fout.close();
flag=1;
fout.open("student.dat",ios::in);
cout<<"enter the roll to be updated";
cin>>rno;
while(fout.read((char*)&s,sizeof(s)))
{
if(s.rollno!=rno)
ftemp.write((char*)&s,sizeof(s));
else
{
flag=0;
cout<<"enter name and grade for updation\n";
gets(s.name);
cin>>s.grade;
ftemp.write((char *)&s,sizeof(s));
}

}
ftemp.close();
fout.close();
if(remove("student.dat")!=0)
{
cout<<"error in updation";
exit(1);
}
rename("temp.dat","student.dat");
if(flag==0)
{
cout<<"record found and updated";
display();
}
else
{
cout<<"record not found and not updated";
display();
}
getch();
}
Result: Thus we have written a program in C++ to update and display the contents of the file and it is
complied and executed successfully.
13. LINEAR SEARCH
Aim: To write a C++ program to search a item in an array using linear search Method
Method:
1.Get the data from the user in an array.
2.Ask the user to enter an element to search in the array.
3.Compare the given key element to each and every element in array
4.If element is present in the given array, then display the position of the element.
Coding:

#include<iostream.h>
#include<conio.h>
class lsearch
{
public:
int data[10],n,key;
void getdata();
void display();
};

void lsearch :: getdata()


{
cout<<"\nEnter the length of the array:";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\nEnter the element in the "<<(i+1)<<" position of the array:";
cin>>data[i];
}
cout<<"\nEnter the key to find the element in the array:";
cin>>key;
}
void lsearch :: display()
{
int flag=0;
for(int i=0;i<n;i++)
{
if(key == data[i])
{
cout<<"\n\nThe element "<<key<<" is present in the position "<<(i+1)<<" of
the array";
flag++;
}
}
if(flag==0)
cout<<"\nGiven key "<<key<<" is not present in the array";
}
void main()
{
clrscr();
lsearch ob;
ob.getdata();
ob.display();
getch();
}

Result: Thus we have written program in C++ to search a item in an array using linear search
method. it is compiled and executed successfully.

14. BINARY SEARCH


Aim: Aim: To write a C++ program to search a item in an array using binary search Method

Coding:
#include<iostream.h>
#include<conio.h>
class bsearch
{
public:
int data[10],n,key,first,last,middle;
void getdata();
void display();
};
void bsearch :: getdata()
{
cout<<"\nEnter the length of the array:";
cin>>n;
cout<<"\n enter the "<<n<<" elements ...\n";
for(int i=0;i<n;i++)
{
cin>>data[i];
}
cout<<"\nEnter the key to find the element in the array:";
cin>>key;
}

void bsearch :: display()


{
first=0;
last=n-1;
middle=(first+last)/2;
while(last>=first)
{
middle=(first+last)/2;
if(key>data[middle])
first=middle+1;
else if(key<data[middle])
last=middle-1;
else
{
cout<<"\nKey "<<key<<" found in the given array at the position"<<middle+1;
break;
}
}
}
void main()
{
clrscr();
bsearch ob;
ob.getdata();
ob.display();
getch();
}

Result: Thus we have written program in C++ to search a item in an array using binary search
method. it is compiled and executed successfully.
15. MATRIX TRANSPOSE

Aim: To write a program in C++ to transpose a given Matrix


Coding :
#include<conio.h>
#include<iostream.h>
class transpose
{
int m,n,i,j,k,a[5][5];
public:
void input();
void output();
};

void transpose::input()
{
cout<<"Enter order of the matrix: ";
cin>>m>>n;
cout<<"Enter "<<m*n<<"elements in the matrix:\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cin>>a[i][j];
}
}

void transpose::output()
{
cout<<"Before transpose the matrix is: \n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"After transpose the given matrix is:\n";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<a[j][i]<<" ";
}
cout<<endl;
}
}
void main()
{
transpose t1;
clrscr();
t1.input();
t1.output();
getch();
}

Result: Thus we have written program in C++ to find the transpose of a given matrix and it is
compiled and executed successfully.

16. ROW SUM COLUMN SUM DIAGONAL SUM OF A MATRIX


Aim: To wite a program in C++ to determine the row sum, column sum and diagonal sums of a
given square Matrix

Coding:
#include<iostream.h>
#include<conio.h>
#include<process.h>
int sum1(int a[50][50],int r)
{
int s=0;
for(int i=0;i<r;i++)
{
s=s+a[i][i];
}
return(s);
}

int sum2(int a[50][50],int r)


{
int s=0;
int j;
j=r-1;
for(int i=0;i<r;i++)
s=s+a[i][j--];
return(s);
}

void row(int a[50][50],int r)


{
int s=0;

for(int i=0;i<r;i++)
{
s=0;
for(int j=0;j<r;j++)
{
s=s+a[i][j];
}

cout<<"Sum of Row "<<i+1<<" = "<<s<<endl;


}
}
void col(int a[50][50],int r)
{
int s=0;
for(int i=0;i<r;i++)
{
s=0;
for(int j=0;j<r;j++)
{
s=s+a[j][i];
}
cout<<"Sum of Column "<<i+1<<" = "<<s<<endl;
}
}

void main()
{
clrscr();
int i,s,j,r,c,ch,a[50][50];
x:
cout<<"Entr Array Limit--(Enter only Row as R=C)"<<endl;
cin>>r;
cout<<"Enter Array"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
cin>>a[i][j];
}
}
y:
cout<<endl<<endl<<" Enter Choice :"<<endl;
cout<<"Sum of---- 1:main 2:Secondary 3.Rows 4.Columns 5.Re-enter 6.Exit "<<endl;
cin>>ch;
switch(ch)
{
case 1:
s=sum1(a,r);
cout<<"Sum = "<<s<<endl;
goto y;
case 2 :
s=sum2(a,r);
cout<<"Sum = "<<s<<endl;
goto y;
case 3:
row(a,r);
goto y;
case 4:
col(a,r);
goto y;
case 5:
goto x;
case 6:
exit(0);
default :
cout<<"Wrong Choice"<<endl;
break;
}
getch();
}

Result:Thus we have written a program in C++ to determine the row sum, column sum and diagonal
sums of a given square Matrix and it is compiled and executed successfully.
17. INSERTION SORT

Aim: to write a C++ program to sort the given array of numbers using Insertion sort technique as
per the given constraints:
Write a menu driven C++ program with following option
a. Accept elements of an array
b. Display elements of an array
c. Sort the array using insertion sort method

Write C++ functions for all options. The functions should have two parameters name of the
array and number of elements in the array.

Coding:

#include<iostream.h>
#include<conio.h>
#include<process.h>
void accept(int Arr[], int s);
void display(int Arr[], int s);
void isort(int Arr[], int s);

void main()
{
int Arr[100],n,choice;
int ch='y';
cout<<"Enter Size of Array ";
cin>>n;
do
{
cout<<"\nMENU";
cout<<"\n1. Accept elements of array";
cout<<"\n2. Sort the array using insertion sort method";
cout<<"\n3. Display elements of array";
cout<<"\n4. Exit";
cout<<"\n\nEnter your choice 1-4 :";
cin>>choice;
switch(choice)
{
case 1: accept(Arr,n);
break;
case 2: isort(Arr,n);
break;
case 3: display(Arr,n);
break;
case 4: exit(0);
break;
default:
cout<<"\nInvalid choice";
}
cout<<"do you want to continue?";
cin>>ch;
}while(ch=='y'|| ch=='Y');
getch();
}
void accept(int Arr[], int s)
{
for(int I=0;I<s;I++)
{
cout<<"Enter element "<<I+1<<":";
cin>>Arr[I];
}
}

void display(int Arr[], int s)


{
cout<<"The elements of the array are:\n";
for(int i=0;i<s;i++)
cout<<Arr[i]<<" ";
}

void isort(int Arr[], int s)


{
int I,J,Temp;
for(I=1;I<s;I++)
{
Temp=Arr[I];
J=I-1;
while((Temp<Arr[J]) && (J>=0))
{
Arr[J+1]=Arr[J];
J--;
}
Arr[J+1]=Temp;
}
}
Result: Thus we have written a program in C++ to sort the given array of numbers using insertion
sort techniques and it is compiled and executed successfully.
18. BUBBLE SORT

Aim: to write a C++ program to sort the given array of numbers using Insertion sort technique as
per the given constraints:
Write a menu driven C++ program with following option
a. Accept elements of an array
b. Display elements of an array
c. Sort the array using bubble sort method

Write C++ functions for all options. The functions should have two parameters name of the
array and number of elements in the array.

Coding:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
void accept(int Arr[], int s);
void display(int Arr[], int s);
void bsort(int Arr[],int s);

int main()
{
int Arr[100],n,choice;
clrscr();
cout<<"Enter Size of Array ";
cin>>n;
do
{
cout<<"\nMENU";
cout<<"\n1. Accept elements of array";
cout<<"\n2. Display elements of array";
cout<<"\n3. Sort the array using bubble sort method";
cout<<"\n4. Exit";
cout<<"\n\nEnter your choice 1-4 :";
cin>>choice;
switch(choice)
{
case 1: accept(Arr,n);
break;
case 2: display(Arr,n);
break;
case 3: bsort(Arr,n);
break;
case 4: cout<<"\nthank you....\n";
getch();
exit(0);
default:
cout<<"\nInvalid choice";
break;
}
} while(1);
}
void accept(int Arr[], int s)
{
for(int I=0;I<s;I++)
{
cout<<"Enter element "<<I+1<<":";
cin>>Arr[I];
}
}
void display(int Arr[], int s)
{
cout<<"The elements of the array are:\n";
for(int I=0;I<s;I++)
cout<<Arr[I]<<" ";
}

void bsort(int Arr[],int s)


{
int I,J,Temp;
for(I=0;I<s-1;I++) //sorting
{
for(J=0;J<(s-1-I);J++)
{
if(Arr[J]>Arr[J+1])
{
Temp=Arr[J]; //swapping
Arr[J]=Arr[J+1];
Arr[J+1]=Temp;
}
}
}
}
Result: Thus we have written a program in C++ to sort the given array of numbers using bubble sort
techniques and it is compiled and executed successfully
19. A PROGRAM TO IMPLEMENT STACK USING LINKED LIST.

Aim: To write a program in C++ to implement stack using linked list.

Assuming the note of the linked list is defined as follows:

class node
{
public:
int data;
node* next;
};

Coding:

#include<stdlib.h>
#include<conio.h>
#include<iostream.h>
//using namespace std;
class node
{
public:
int data;
node* next;
};

class StackList
{
private:
node *top;
int count; //head
int maxnum;
int stackData;
public:
StackList(int max)
{
top = NULL;
maxnum = max;
count=0;
}

void push(int element)


{
if(count == maxnum)
cout<<"stack is full";
else
{
node *newTop = new node;
if(top == NULL)
{
newTop->data = element;
newTop->next = NULL;
top = newTop;
count++;
}
else
{
newTop->data = element;
newTop->next = top;
top = newTop;
count++;
}
}
}

void pop()
{
if(top == NULL)
cout<< "nothing to pop";
else
{
node *old = top;
top = top->next;
count--;
delete(old);
}
}
void print()
{
node *temp;
temp = top;
while(temp!=NULL)
{
cout<<temp->data<<",";
temp=temp->next;
}
}
};
void main()
{
//StackList *sl = new StackList(5);
StackList s1(5);
clrscr();
int d;
int ch;
do
{

switch(ch)
{
case 1:
cout<<"\nenter element to push\n";
cin>>d;
s1.push(d);
break;
case 2:
s1.pop();
break;
case 3:
s1.print();
break;
case 4:
exit(1);
default:
cout<<"enter the correct choice?";
}
}while(1);

}
Result: Thus we have written a program in C++ to implement the stack using linked list and it is
compiled and executed successfully.
20. A PROGRAM TO IMPLEMENT QUEUE USING LINKED LIST.

Aim: To write a program in C++ to implement stack using linked list.


Assuming the note of the linked list is defined as follows:
struct node
{
int info;
struct node *next;
};

Coding:
#include<iostream.h>
#include<stdlib.h>
//using namespace std;
struct node
{
int info;
struct node *next;
};
class Queue
{
private:
node *rear; node *front;
public:
Queue();
void enqueue();
void dequeue();
void display();
};

Queue::Queue()
{
rear = NULL; front = NULL;
}
void Queue::enqueue()
{
int data;
node *temp = new node;
cout<<"Enter the data to enqueue: ";
cin>>data;
temp->info = data;
temp->next = NULL;
if(front == NULL)
{
front = temp;
}
else
{
rear->next = temp;
}
rear = temp;
}
void Queue::dequeue()
{
node *temp = new node;
if(front == NULL)
{
cout<<"\nQueue is Emtpty\n";
}
else
{
temp = front;
front = front->next;
cout<<"The data Dequeued is "<<temp->info;
delete temp;
}
}
void Queue::display()
{
node *p = new node;
p = front;
if(front == NULL)
{
cout<<"\nNothing to Display\n";
}
else
{
while(p!=NULL)
{
cout<<endl<<p->info;
p = p->next;
}
}
}
void main()
{
Queue queue;
int choice;
while(1)
{
cout<<"\n1.Enqueue\n2. Dequeue\n3. Display\n 4.Quit";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
queue.enqueue();
break;
case 2:
queue.dequeue();
break;
case 3:
queue.display();
break;
case 4:
exit(0);
default:
cout<<"\nInvalid Input. Try again! \n";
break;
}
}
}

Result: Thus we have written a program in C++ to implement the stack using linked list and it is
compiled and executed successfully.
SQL- Structured Query Language

1. Data control language (DCL) - used to control user access (e.g., GRANT, REVOKE)
2. Transactional language - used to control logical units of work (e.g., COMMIT)
EXPERIMENT -1 (DDL COMMANDS)
Aim: To understand and perform the DDL commands

Data definition language (DDL) - used to define or change database structure(s) (e.g., CREATE,
ALTER, DROP,etc)

Creating a Database:
Syntax: create database databasename;
eg: create database vbc;

Creating a new table in the database


use VBC;
Syntax :
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);

Example :
CREATE TABLE student
(
rno int,
name char(25),
fees int,
dob date,
class char(3)
);

Deleting a database completely

SYNTAX :DROP database name


EX: :drop VBC;

Deleting a table in the database completely


Syntax: DROP TABLE tablename;
EX: DROP TABLE student;

Show all databases


Syntax:SHOW DATABASES;
Ex:show databases;

Show all the tables in the database:


Syntax:
USE DATABASENAME;
SHOW TABLES;
EX:
use VBC;
Show tables;
Describe the structure of a table
Syntax:Desc tablename;
Eg: desc student;

Adding a new column(s) in the table


Syntax :
ALTER TABLE table_name
ADD column_name datatype
Examples :
ALTER TABLE student ADD (grade CHAR(2));

Modifying the data type of a column


Syntax;
ALTER TABLE table_name MODIFY column_name datatype
Example:
ALTER TABLE student MODIFY (grade CHAR(1));
Deleting a table

Result: Thus we have learnt various DDL commands and executed successfully
EXPERIMENT -2 (DML COMMANDS)

Aim: To understand and perform the DML commands

Data manipulation language (DML) - used to select or change data (e.g., INSERT, UPDATE,
DELETE, SELECT)

Note: Assuming student table is already created

Inserting a new row at the bottom of the table


Syntax :
INSERT INTO table_name VALUES (value1,value2,value3,...);
Examples
INSERT INTO student VALUES(10, 'Alex', 7800, '1998-10-03','K12');

Displaying the content from a table – SELECT


Syntax :
SELECT * FROM table_name [where <condition>];
Example :
SELECT * FROM student;
rno name fees Dob Class
10 Alex 7800 1998-10-03 K12
11 Peter 6700 1997-11-15 K12
12 Alisha 7800 1999-07-03 K11
13 John 6900 2000-12-13 K11

SELECT name FROM student;


Name
Alex
Peter
Alisha
John
Usage of Relational Operator(=, <, >, <=, >=, <>), Logical Operator(AND, OR, NOT )

SELECT * FROM student WHERE fees < 7000;

rno name fees Dob Class


11 Peter 6700 1997-11-15 K12
13 John 6900 2000-12-13 K11

SELECT * FROM student WHERE fess > 7000 AND fees < 8000;
rno name fees Dob Class
10 Alex 7800 1998-10-03 K12
12 Alisha 7800 1999-07-03 K11

SELECT * FROM student WHERE fees > 7000 OR class = 'K12';


rno name fees Dob Class
10 Alex 7800 1998-10-03 K12
11 Peter 6700 1997-11-15 K12
12 Alisha 7800 1999-07-03 K11

SELECT name, fees FROM student WHERE NOT (class = 'K12');


Name Fees
Alisha 7800
John 6900

SELECT name, fees FROM student WHERE class <> 'K12';


Name Fees
Alisha 7800
John 6900
SELECT * FROM student WHERE rno IN(10, 12, 13);
Rno name fees Dob Class
10 Alex 7800 1998-10-03 K12
12 Alisha 7800 1999-07-03 K11
13 John 6900 2000-12-13 K11

SELECT * FROM student WHERE rno BETWEEN 11 AND 13;


Rno name fees Dob Class
11 Peter 6700 1997-11-15 K12
12 Alisha 7800 1999-07-03 K11
13 John 6900 2000-12-13 K11
SELECT name FROM student WHERE name LIKE 'A%';
Name
Alex
Alisha
SELECT * name FROM student WHERE name LIKE '%a';
Rno name fees Dob Class
12 Alisha 7800 1999-07-03 K11

SELECT name FROM student WHERE Name LIKE '%e%' ;


Name
Alex
Peter

Modifying the existing content of the table


Syntax:
UPDATE table_name SET column1=value1,column2=value2,...
WHERE some_column=some_value;
Example
UPDATE student SET fees = '7900' WHERE rno = 12 ;

SELECT * FROM student;

rno Name fees Dob Class


10 Alex 7800 1998-10-03 K12
11 Peter 6700 1997-11-15 K12
12 Alisha 7900 1999-07-03 K11
13 John 6900 2000-12-13 K11

Arranging the data in ascending or descending order of one/multiple columns (ORDER BY


clause)

Syntax:
SELECT column_name,column_name FROM table_name
ORDER BY column_name,column_name ASC|DESC;
Example
SELECT * FROM student ORDER BY name;

rno name Fees Dob class


10 Alex 7800 1998-10-03 K12
12 Alisha 7900 1999-07-03 K11
13 John 6900 2000-12-13 K11
11 Peter 6700 1997-11-15 K12

SELECT * FROM student ORDER BY fees DESC;


rno name Fees Dob class
12 Alisha 7900 1999-07-03 K11
10 Alex 7800 1998-10-03 K12
13 John 6900 2000-12-13 K11
11 Peter 6700 1997-11-15 K12
SELECT class, name, dob, fees FROM student ORDER BY class, name DESC;

Class name Dob fees


K11 John 2000-12-13 6900
K11 Alisha 1999-07-03 7900
K12 Peter 1997-11-15 6700
K12 Alex 1998-10-03 7800

SELECT class, name, fees, fees*12 annualfees FROM student;

Class name Fees annualfees


K12 Alex 7800 93600
K12 Peter 6700 80400
K11 Alisha 7900 94800
K11 John 6900 82800
Using Aggregate Functions with SELECT
COUNT( ) To count the number of rows
SUM( ) To find the sum of values in the column
MAX( ) To find the maximum value in the column
MIN( ) To find the minimum value in the column
AVG( ) To find the average of values in the column
SELECT COUNT(*) FROM student;

COUNT(*)
4

SELECT COUNT(rno) FROM student;

COUNT(rno)
4

SUM(fees)
29300 SELECT SUM(fees) FROM student;

SELECT AVG(fees) FROM student;


AVG(fees)
7325.0000

SELECT MAX(fees), MIN(fees) FROM student;


MAX(fees) MIN(fees)
7900 6700

Grouping data under given Column- (GROUP BY)

SELECT class, SUM(fees) FROM student GROUP BY class;


Class SUM(fees)
K11 14800
K12 14500

SELECT class, MAX(fees), MIN(fees) FROM student GROUP BY class;


Class MAX(fees) MIN(fees)
K11 7900 6900
K12 7800 6700

SELECT class, MAX(dob) FROM student GROUP BY class HAVING COUNT(*)>1;


Class MAX(dob)
K11 2000-12-13
K12 1998-10-03

Deleting a row/rows from a table


Syntax:
DELETE FROM table_name WHERE some_column=some_value;
Example:
DELETE FROM Student WHERE rno = 13;

Working with more than one table


Syntax:
SELECT col1, col2, col3...FROM table_name1, table_name2
WHERE table_name1.col2 = table_name2.col1;
Table - product
product_id product_name supplier_name unit_price
100 Camera Nikon 300
101 Television Onida 100
102 Refrigerator Videocon 150
103 Ipod Apple 75
104 Mobile Nokia 50

Table - order_items
order_id product_id total_units customer
5100 104 30 Infosys
5101 102 5 Satyam
5102 103 25 Wipro
5103 101 10 TCS

SELECT order_id, product_name, unit_price, supplier_name, total_units


FROM product, order_items WHERE order_items.product_id = product.product_id;

Result: Thus we have learnt various DML commands and executed successfully
EXPERIMENT-3

Aim: To consider the following tables EMPLOYEES and EMPSALARY. Write SQL commands for
the statements(i) to (vi) and give outputs for SQL queries (vii) to (x)

Books:
Bookid BookName AuthorName Publisher Price Type Qty
C0001 Fast Cook Lata Kapoor EPB 355 Cookery 5
F0001 The Tears William First Publ 650 Fiction 20
Hopkins
T0001 My First Brain & EPB 350 Text 10
C++ Brooke
T0002 C++ A.W. TDH 350 Text 15
Brainworks Rossaine
F0002 Thunderbolts Anna First publ. 750 Fiction 50
Roberts
Issued
Bookid QtyIssued
T0001 4
C0001 5
F0001 2

(i) To display Book name, author name and price of First publishers books.
ANS: SELECT BookNmae, AuthorName,Price from Books where Publisher="First Publ";

(ii) To display the names and price of books in ascending order of their price.
ANS:SELECT BookName, Price FROM Books ORDER BY Price;

(iii) To display bookid, bookname, type & qtyissued


ANS: SELECT Book.Bookid, BookName, Type, QtyIssued from Books, Isssued Where
Books.Bookid=Issued.Bookid;

(iv) To increase the price of all books of EPB publishers by 50.


ANS: UPDATE Books SET Price= Price + 50 WHERE Publisher = "EPB";

(v) To select the books whose quantities are more than or equal to the average quantity.
SELECT BookName from Books where qty>=(select average(qty) from Books);

(vi) To display the BookName and quantity issued for the Bookid that have issued
ANS: Select BookName, QtyIssued from Issued I,Books B where I.Bookid=B.Bookid;

(vii) SELECT Count(*) FROM Books;


ANS: 5

(viii) SELECT BookName , AuthorName FROM Books WHERE Publisher = "EPB";


Fast Cook Lata Kapoor
My First C++ Brain & Brooke

(xi) SELECT COUNT(DISTINCT Publishers ) FROM Books WHERE Price>=400;


ANS: 1

(x) SELECT MAX(Price) From Books WHERE Qty > 15;


ANS: 750

Result: Thus the above queries are executed and their ouput is traced successfully.
EXPERIMENT -4

Aim: To consider the following tables Client and Bill. Write the SQL commands for the statements
and Write SQL commands for the statements(i) to (iv) and give outputs for SQL queries (v) to (viii)

Client:
Cust_id Cust_name Address Phone_no City
C007 MANI VILLAPURAM 7343234523 MADURAI
C008 KANI NARI MEDU 8343234523 CHENNAI
C010 VANI NILAIYOOR 9343234523 KOLKATTA
C012 SENTHIL AATHUR 6343234523 DELHI
C013 BANUMATHI CHINNALAPATTI 6643234523 DINDUGUL

Bill:
Ord_id Cust_id Item Ord_date Qty Price
7002 C007 Pizza 20-11-07 1 250
7003 C013 Garlic Bread 24-10-05 3 75.75
7004 C012 Pasta 03-03-07 4 173
7005 C010 Ice Cream 01-01-08 30 195.75
7006 C035 Pizza 02-03-06 4 250
7009 C035 Garlic Bread 02-03-08 2 75.75

i) To Display a report containing Cust_id, Cust_name, Item, Qty, Price and BillAmount.
bill amount is calculated using qty*price;

Ans: SELECT C.Cust_id, C.Cust_name,B.Item,B.Qty ,B.Qty*B.Price as


BillAmount from Client C, Bill B where C. Cust_id=B. Cust_id;

ii) display how many customers have ordered pizza and ord_date in the month March.
Ans: SELECT count(*) from bill where item=’Pizza’ and order_date like ‘ %03%’

iii) Count the number of customers who have order items worth than 1700;
total amt=sum of qty* price;
Ans: SELECT count(*),cust_id, sum(qty*price) as total_amt from bill group by cust_id
having sum(qty,price)>300;

iv) Display the names of customers along with their city in alphabetical order of city
Ans: SELECT cust_name,city from client order by city

v) Select Item , sum(price) from Bill group by Item


Ans:
Item sum(price)
Pizza 500
Garlic 151.5
Bread
Pasta 173
Ice Cream 195.75
vi) SELECT Item, count(*) from Bill group by Item order by Item
Ans:
Item count(*)
Garlic Bread 2
Ice Cream 1
Pasta 1
Pizza 2

vii) SELECT Cust_name,Address from Client where City not in(‘MADURAI’,’CHENNAI’);


Ans:
Cust_name Address
VANI NILAIYOOR
SENTHIL AATHUR
BANUMATHI CHINNALAPATTI

viii) Update Client Set Phone_no=9999993335 where Cust_name=’MANI’;


Select Cust_name,Phone_no from Client ;

Ans:
Cust_name Phone_no
MANI 9999993335
KANI 8343234523
VANI 9343234523
SENTHIL 6343234523
BANUMATHI 6643234523

S-ar putea să vă placă și