Anda di halaman 1dari 38

( As per CBSE Guidelines )

Under the guidance of:

Submitted By: Ayush Varshney and Indraneel Bhakta Class XII-C

Mr. Mahendra Kothari


Sr. Computer Lecturer (Campion School)

Campion School Bhopal


Session 2013-2014

C++ Program to insert a node in the beginning of the list.

#include<constream.h> #include<process.h> struct node{int info; node*next;} *start,*newptr,*save,*ptr; node*create_new_node(int); void insert_beg(node*); void display(node*); void main() {node*null=0; start=null; int inf;char ch='y'; while(ch=='y'||ch=='Y') {clrscr(); cout<<"\nentor information for the new node..."; cin>>inf; cout<<"\ncreating new node!!press enter to continue...";getch(); newptr=create_new_node(inf); if(newptr!=null) {cout<<"\n\nnew node created successfully. press enter to continue..."; getch(); } else {cout<<"\ncannot create new node!!!aborting!!!\n"; getch(); exit(1); }

{cout<<"\n\nnow inserting this node in the beginning of the list...\n"; cout<<"press enter to continue...\n"; getch();} insert_beg(newptr); cout<<"\nnow the list is:\n"; display(start); cout<<"\npress y to enter more nodes, n to exit...\n"; cin>>ch;} } node*create_new_node(int n) {node*null=0; ptr=new node; ptr->info=n; ptr->next=null; return ptr; } void insert_beg(node*np) {node*null=0; if(start==null) start=np; else {save=start; start=np; np->next=save; }} void display(node*np) {node*null=0; while(np!=null) {cout<<np->info<<"->"; np=np->next; } cout<<"!!!!"<<endl;}

C++ program to delete first node.

#include<iostream.h> #include<process.h> #include<conio.h> struct node{int info; node*next; }*start,*newptr,*ptr,*save,*rear; node*create_new_node(int); void insert(node*); void display(node*); void delnode(); void main() {node*null=0; start=rear=null; int inf; char ch='y'; while(ch=='y'||ch=='Y') {clrscr(); cout<<"enter information for the new node"; cin>>inf; newptr=create_new_node(inf); if(newptr==null) {cout<<"cannot create new node!!aborting"; getch(); exit(1);} insert(newptr); cout<<"press Y to enter more nodes,N to exit...."; cin>>ch;

} clrscr(); do {cout<<"now the list is:"; display(start); getch(); cout<<"want to delete first node?(y/n)"; cin>>ch; if(ch=='y'||ch=='Y') delnode(); } while(ch=='y'||ch=='Y'); } node*create_new_node(int n) {node*null=0; ptr=new node; ptr->info=n; ptr->next=null; return ptr; } void insert(node*np) {node*null=0; if(start==null) start=rear=np; else {rear->next=np; rear=np; } } void delnode() {node*null=0; if(start==null) cout<<"UNDERFLOW!!!"; else

{ptr=start; start=start->next; delete ptr; } } void display(node*np) {node*null=0; while(np!=null) {cout<<np->info<<"->"; np=np->next; } cout<<"!!!"; }

Program

search.

to implement binary

#include<iostream.h> int Bsearch(int[],int,int); void main() {int AR[50],ITEM,N,index; cout<<"Enter limit"; cin>>N; cout<<"\nEnter elements\n"; for(int i=0;i<N;i++) {cin>>AR[i];} cout<<"Enter element to be searched for..."; cin>>ITEM; index=Bsearch(AR,N,ITEM); if(index==-1) cout<<"\nsorry! element could not be found\n"; else cout<<"\nelement found at:"<<index<<"position:"<<index+1<<endl; }

int Bsearch(int AR[],int size,int ITEM) {int beg,last,mid; beg=0; last=size-1; while(beg<=last) {mid=(beg+last)/2; if(ITEM==AR[mid]) return mid; else if(ITEM>AR[mid]) beg=mid+1; else last=mid-1;} return-1;}

Program

to insert a node in the end of the list.

#include<iostream.h> #include<conio.h> #include<process.h> struct node{int info; node*next;} *start,*newptr,*save,*ptr,*rear; node*create_new_node(int); void insert_end(node*); void display(node*); void main() {node*null=0; start=rear=null; int inf;char ch='y'; while(ch=='y'||ch=='Y')

{clrscr(); cout<<"\nenter information for the new node..."; cin>>inf; cout<<"\ncreating new node!!press enter to continue...";getch(); newptr=create_new_node(inf); if(newptr!=null) {cout<<"\n\nnew node created successfully. press enter to continue..."; getch(); } else {cout<<"\ncannot create new node!!!aborting!!!\n"; getch(); exit(1); } cout<<"\n\nnow inserting this node in the end of the list...\n"; cout<<"press enter to continue...\n"; getch(); insert_end(newptr); cout<<"\nnow the list is:\n"; display(start); cout<<"\npress y to enter more nodes, n to exit...\n"; cin>>ch;} } node*create_new_node(int n) {node*null=0; ptr=new node; ptr->info=n; ptr->next=null; return ptr; } void insert_end(node*np) {node*null=0; if(start==null) start=rear=np;

else {rear->next=np; rear=np; }} void display(node*np) {node*null=0; while(np!=null) {cout<<np->info<<"->"; np=np->next; } cout<<"!!!\n"; }

program to implement linear search.

#include<iostream.h> int Lsearch(int[],int,int); void main() {int AR[50],ITEM,N,index; cout<<"Enter limit"; cin>>N; if(N>50) cout<<"OVERFLOW"<<endl; else cout<<"\nEnter elements\n"; for(int i=0;i<N;i++) {cin>>AR[i];} cout<<"Enter element to be searched for"; cin>>ITEM; index=Lsearch(AR,N,ITEM); if(index==-1)

cout<<"\nSORRY!!!!!!!!!!\n"; else cout<<"\nelement found at:"<<index<<"position:"<<index+1<<endl; } int Lsearch(int AR[],int size,int ITEM) {for(int i=0;i<size;i++) {if(AR[i]==ITEM) return i; } return-1;}

C++ Program on Multilevel Inheritance.

#include<process.h> #include<iostream.h> #include<conio.h> class MNC {char Cname[25]; protected: char Hoffice[25]; public: MNC(); char country[25]; void enterdata(); void displaydata();}; class branch:public MNC {long NOE; char city[25]; protected: void association(); public:

branch(); void add(); void show();}; class outlet:public branch {char state[25]; public: outlet(); void enter(); void output(); };

Program

stack.

to insert elements on to

#include<iostream.h> #include<conio.h> #include<process.h> int push(int[],int&,int); void display(int[],int); const int size=50; void main() {int stack[size],item,top=-1,res; char ch='y'; clrscr(); while(ch=='y'||ch=='Y') {cout<<"\nenter item for insertion:\n"; cin>>item; res=push(stack,top,item); if(res==-1) {cout<<"OVERFLOW!!!! aborting!!\n"; exit(1); } cout<<"\n the stack now is:\n";

display(stack,top); cout<<"\nwant to insert more elments???(y/n)....\n"; cin>>ch; } } int push(int stack[],int &top,int ele) {if(top==size-1) return -1; else {top++; stack[top]=ele; } return(0); } void display(int stack[],int top) {cout<<stack[top]<<"<--"<<endl; for(int i=top-1;i>=0;i--) cout<<stack[i]<<endl; }

Program

on Selection sort technique.

#include<iostream.h> #include<conio.h> void selsort(int[],int); void main() {int ar[50],element,n,position; cout<<"enter limit->"; cin>>n; cout<<"\nenter elements of array->"; for(int i=0;i<n;i++) cin>>ar[i];

selsort(ar,n); cout<<"\n\nthe sorted array is->\n"; for(i=0;i<n;i++) cout<<ar[i]<<" "; cout<<endl; } void selsort(int ar[],int size) {int small,pos,tmp; for(int i=0;i<size;i++) {small=ar[i]; for(int j=i+1;j<size;j++) {if(ar[j]<small) {small=ar[j];pos=j;} } tmp=ar[i]; ar[i]=ar[pos]; ar[pos]=tmp; cout<<"\narray after pass"<<i+1<<"is->"; for(j=0;j<size;j++) cout<<ar[j]<<" "; }}

Program

to perform POP command in a stack OR deletion from a stack list.

#include<constream.h> #include<process.h> int pop(int[],int&); int push(int[],int&,int); void display(int[],int);

const int size=50; void main() {int stack[size],top=-1,res,item; char ch='y'; while(ch=='y'||ch=='Y') {cout<<"enter item for insertion:"; cin>>item; res=push(stack,top,item); if(res==-1) {cout<<"OVERFLOW!!!"; exit(1); } cout<<"the stack now is:"; display(stack,top); cout<<"want to insert more elements???(y/n)"; cin>>ch;} cout<<"now the deletion of elements begins"; ch='y'; while(ch=='y'||ch=='Y') {res=pop(stack,top); if(res==-1) cout<<"UNDERFLOW!!!"; else {cout<<"the element deleted is "<<res; cout<<"the stack after deletion is:"; display(stack,top);} cout<<"want to delete more elements??(y/n)"; cin>>ch;}} int push(int stack[],int&top,int ele) {if(top==size-1) return-1; else {top++; stack[top]=ele;

} return 0; } int pop(int stack[],int& top) {int ret; if(top==-1) return-1; else {ret=stack[top]; top--;} return ret; } void display(int stack[],int top) {if(top==-1) return; cout<<stack[top]<<"<--"<<endl; for(int i=top-1;i>=0;i++) cout<<stack[i]<<endl;}

Program

on Single level Inheritance (Conceptual).

#include<stdio.h> #include<constream.h> #include<process.h> class emp {int code; char name[10]; public: void input(); void output();}; class salary:private emp

{int basic; public: void read(); void display();}; void emp::input() {cout<<"enter name &code"; cin>>code; gets(name);} void emp::output() {cout<<code; puts(name);} void salary::read() {input(); cout<<"enter basic"; cin>>basic;} void salary::display() {output(); cout<<"gross salary"; cout<<basic+basic*1.2;} void main() {salary s; s.read(); s.display();}

Program

using for loop to print THE SUM OF TWO MATRICES.

#include<iostream.h> #include<process.h> #include<conio.h> void main() {clrscr();

int a[10][10],b[10][10],c[10][10]; int i,j,m,n,p,q; cout<<"ENTER ROWS AND COLOUMNS OF MATRIX A..............."<<endl; cin>>m>>n; cout<<"ENTER ROWS AND COLOUMNS OF MATRIX B..............."<<endl; cin>>p>>q; if((m==p) && (n==q)) cout<<"MATRICES CAN BE ADDED!!!!!!!!!"; else {cout<<"MATRICES CANNOT BE ADDED!!!!!!!!!"; exit(0);} cout<<"\nINPUT MATRIX A->\n"<<endl; for(i=0;i<m;i++) {for(j=0;j<n;j++) cin>>a[i][j];} cout<<"\nINPUT MATRIX B->\n"<<endl; for(i=0;i<p;i++) {for(j=0;j<q;j++) cin>>b[i][j];} for(i=0;i<m;i++) {for(j=0;j<n;j++) c[i][j]=a[i][j]+b[i][j];} cout<<"\nTHE SUM OF TWO MATRICES IS:\n"<<endl; for(i=0;i<m;i++) {cout<<"\n"; for(j=0;j<n;j++) cout<<" "<<c[i][j];}}

Program

to merge two matrices in descending order.

#include<iostream.h> #include<conio.h> void merge(int[],int[],int[],int,int); void main() {int A[10],B[10],C[20],M,N,MN,i; cout<<"enter length of A->"; cin>>M; cout<<"enter elements of'A'in ascending order->"; for(i=0;i<M;i++) cin>>A[i]; cout<<"enter length of B->"; cin>>N; cout<<"enter elements of 'B'in descending order->"; for(i=0;i<N;i++) cin>>B[i]; MN=M+N; merge(A,B,C,M,N); cout<<"after merging"; for(i=0;i<MN;i++) cout<<C[i]<<" "; cout<<endl;} void merge(int A[],int B[],int C[],int M,int N) {int a,b,c; for(a=0,b=N-1,c=0;a<M&&b>=0;) {if(A[a]<=B[b]) C[c++]=A[a++]; else C[c++]=B[b--];} if(a<M)

{while(a<M) C[c++]=A[a++];} else {while (b>=0) C[c++]=B[b--];}}

Program

on Multiple Inheritance.

#include<stdlib.h> #include<iomanip.h> #include<ctype.h> #include<fstream.h> #include<iostream.h> class regular {char schoolcode[10]; public: void inregular(); void outregular();}; class distance {char studycentrecode[10]; public: void indistance(); void outdistance();}; class course:public regular,private distance {char code[10]; float fees; int duration; public: void incourse(); void outcourse();};

Program

to display a file.

#include<fstream.h> #include<conio.h> int main() {clrscr(); ofstream fout("STUDENT"); char name[39],ch; float marks=0.0; for(int i=0;i<5;i++) {cout<<"STUDENT"<<(i+1)<<":\tNAME:-"; cin.get(name,30); cout<<"\t\tMARKS:-"; cin>>marks; cin.get(ch); fout<<name<<'\n'<<marks<<'\n'; } fout.close(); ifstream fin("STUDENT"); fin.seekg(0); cout<<"\n"; for(i=0;i<5;i++) { fin.get(name,30); fin.get(ch); cout<<"STUDENT NAME:-"<<name; cout<<"\tMARKS:-"<<marks<<"\n";} fin.close(); return 0;}

Program

for reading and writing class objects.

#include<fstream.h> #include<conio.h> class student { char name[40]; char grade; float marks; public: void getdata(void); void display(void);}; void student::getdata(void) {char ch; cin.get(ch); cout<<"enter name:"; cin.getline(name.40); cout<<"enter grade:"; cin>>grade; cout<<"enter marks:"; cin>>marks; cout<<"\n"; } void student::display(void) { cout<<"Name:"<<name<<"\t" <<"Grade:"<<grade<<"\t" <<"Marks:"<<marks<<"\t"<<"\n"; } void main() { clrscr(); Student arts[3]; fstream filin; filin.open("stu.dat".ios::in|ios::out); if (!filin) { cout<<"Cannot open file!!\n"; return 1;

} cout<<"Enter details for 3 students \n"; for(int i=0; i<3; i++) { arts[i].getdata();filin.write((char*) & arts[i], sizeof (arts[i])); } filin.seekg(0); cout<<"The contents of stu.dat are shown below. \n"; for(i=0; i<3; i++) { filin.read((char*)& arts[i], sizeof (arts[i])); arts[i].display(); } filin.close(); }

Program

file.

to write data to a binary

#include<iostream.h> #include<conio.h> #include<fstream.h> #include<iomanip.h> struct Prod { int no; //product number char name[50]; //product name float price; //unit price }; Prod readdata() { Prod P; char str[50]; cout<<"product no."; cin>>P.no; cin.get(); cout<<"product name";

cin.getline(P.name,40); cout<<"unit price"; cin>>P.price; return(P);

//Product name may have blank spaces

} void main() { clrscr(); int i; Prod P; int n; cout<<"number of products"; cin>>n; ofstream fout("PRICE.LST",ios::out|ios::binary|ios::app); for(i=1;i<=n;i++) { P=readdata(); fout.write((char*)&P, sizeof (P)); //writes all data members } //to file }

Program

on Single level Inheritance (Working).

#include<iostream.h> #include<conio.h> class student {protected: int rollno; public: void getrollno(int); void prnrollno(void); }; void student::getrollno(int a) {rollno=a;} void student::prnrollno(void) {cout<<"rollno"<<rollno;} class test:public student { private: void add() {cout<<"add";} protected: float sub1; float sub2; float sub3; float total; float perc; char grade; void calc(void); public: void getmarks(float,float,float); void disp_result(void); }; void test::calc(void) {total=sub1+sub2+sub3; perc=(total/300)*100; if(perc<40.00)

grade='D'; else if(perc<50.00) grade='c'; else if(perc<60.00) grade='B'; else if(perc<80.00) grade='A'; } void test::getmarks(float x,float y,float z) {sub1=x,sub2=y,sub3=z;} void test::disp_result(void) {calc(); cout<<"result"; prnrollno(); cout<<"subject1"<<sub1<<endl; cout<<"total"<<total<<endl; cout<<"grade"<<grade<<endl; } int main() {clrscr(); test s1; int rno;

cout <<"enter the roll no:"; cin>>rno; s1.getrollno(rno); float f1,f2,f3; cout<<"enter marks in three subject"; cin>>f1>>f2>>f3; s1.getmarks(f1,f2,f3); s1.disp_result(); getch(); return 0; }

Program

to create a linked stack.

#include<iostream.h> #include<conio.h> struct node { int data; node *next; }; node *top; int value; node *push(int val); node *pop(); void show_stack(); void main() { int choice,value; top=NULL; clrscr(); do { cout<<"\n Linked implementation of Stack"; cout <<"\n PUSH-----1"; cout<<"\n POP--------2"; cout<<"\n ShowStack----3"; cout<<"\n Quit------4"; cout<<"\n Enter your choice"; cin>>choice; clrscr(); switch(choice) { case 1: cout<<"\n Enter the value to be pushed"; cin>>value; top=push( value); break; case 2: pop(); cout<<"\n The popped value is...."<<value;

break; case 3: show_stack(); break; } } while (choice!=4); } node *push(int value) { node *x; x= new node; x->data= value; x->next=top; top=x; return(top); } node *pop() { node *x; if(top == NULL) { cout<<"\n Stack Empty" ; } else { x=top; top=top->next; value=x->data; delete x; } return(top); } void show_stack() { node *ptr; ptr=top; cout<<"\n The stack is....."; while(ptr!=NULL)

{ cout<<ptr->data<<""; ptr=ptr->next; } }

Program

on Insertion sort technique.

#include<iostream.h> #include<conio.h> #include<limits.h> void inssort(int [],int); void main() { clrscr(); int ar[50],n; cout<<"Enter the no of element"; cin>>n; cout<<"Enter the elements"; for(int i=1;i<=n;i++) {cin>>ar[i];} inssort(ar,n); cout<<"The sorted array is as shown below"; for(i=0;i<=n;i++) cout<<ar[i]<<" "; cout<<endl; getch(); } void inssort(int ar[],int size) {int tmp,j; ar[0]=INT_MIN; for(int i=1;i<=size;i++) {tmp=ar[i]; j=i-1; while(tmp<ar[j]) {ar[j+1]=ar[j];

j--;} ar[j+1]=tmp; cout<<"Array after pass"<<i<<"is---\n"; for(int k=0;k<=size;k++) cout<<ar[k]<<" "; cout<<endl; } }

Program

to implement queue operations.

#include<iostream.h> #include<conio.h> int front,rear; int q[30]; //front=rear=0; void qinsert(int val); int qdelete(); void display(); void main() { int choice,value; front=rear=0; do { cout<<"\n Queue operations"; cout<<" Add 1 "; cout<<" Delete 2"; cout<<" Display 3"; cout<<" Quit 4"; cout<<"\n Enter your choice"; cin>>choice; clrscr(); switch(choice) {

case 1: cout<<"\n Enter the value to be added"; cin>>value; qinsert(value); break; case 2: value=qdelete(); cout<<"\n The deleted value is"<<value; break; case 3: cout<<"\n Queue is....."; display(); break; } } while (choice !=4); } void qinsert(int val) { if (rear < 30) { q[++rear]=val; } else cout <<"\n The Queue is full"; } int qdelete() { int v; if(front!=rear) { v= q[++front]; return v; } else { cout<<"\n Queue is empty";

return 0; } } void display() { int i; if (front < rear) { cout<<"\n"; for(i=front+1;i<=rear;i++) cout<<q[i]<<""; } }

SQL

Program 1

1. Create a table to store information about weather observation stations: -- No duplicate ID fields allowed.
CREATE TABLE STATION (ID INTEGER PRIMARY KEY, CITY CHAR(20), STATE CHAR(2), LAT_N REAL, LONG_W REAL); 2. Populate the table STATION with a few rows: INSERT INTO STATION VALUES (13, 'Phoenix', 'AZ', 33, 112); INSERT INTO STATION VALUES (44, 'Denver', 'CO', 40, 105); INSERT INTO STATION VALUES (66, 'Caribou', 'ME', 47, 68);

3. Query to look at table STATION in undefined order:


SELECT * FROM STATION;

Output:-

4. Query to select Northern stations (Northern latitude > 39.7): -- selecting only certain rows is called a "restriction".
SELECT * FROM STATION WHERE LAT_N > 39.7;

Output:-

5. Query to select only ID, CITY, and STATE columns: -- selecting only certain columns is called a "projection".
SELECT ID, CITY, STATE FROM STATION; Output:-

6. Query to both "restrict" and "project":


SELECT ID, CITY, STATE FROM STATION WHERE LAT_N > 39.7; Output:-

SQL

Program 2 :-

1. Create another table to store normalized temperature and precipitation data: -- ID field must match some STATION table ID (so name and location will be known). -- allowable ranges will be enforced for other values. -- no duplicate ID and MONTH combinations. -- temperature is in degrees Fahrenheit. -- rainfall is in inches.
CREATE TABLE STATS (ID INTEGER REFERENCES STATION(ID), MONTH INTEGER CHECK (MONTH BETWEEN 1 AND 12), TEMP_F REAL CHECK (TEMP_F BETWEEN -80 AND 150), RAIN_I REAL CHECK (RAIN_I BETWEEN 0 AND 100), PRIMARY KEY (ID, MONTH));

2. Populate the table STATS with some statistics for January and July:
INSERT INTO STATS VALUES (13, 1, 57.4, 0.31); INSERT INTO STATS VALUES (13, 7, 91.7, 5.15); INSERT INTO STATS VALUES (44, 1, 27.3, 0.18); INSERT INTO STATS VALUES (44, 7, 74.8, 2.11); INSERT INTO STATS VALUES (66, 1, 6.7, 2.10); INSERT INTO STATS VALUES (66, 7, 65.8, 4.52);

3. Query to look at table STATS in undefined order:


SELECT * FROM STATS; Output:-

4. Query to look at table STATS, picking up location information by joining with table STATION on the ID column: -- matching two tables on a common column is called a "join". -- the column names often match, but this is not required. -- only the column values are required to match.
SELECT * FROM STATION, STATS WHERE STATION.ID = STATS.ID; Output:-

5. Query to look at the table STATS, ordered by month and greatest rainfall, with columns rearranged:
SELECT MONTH, ID, RAIN_I, TEMP_F FROM STATS ORDER BY MONTH, RAIN_I DESC; Output:-

6. Query to look at temperatures for July from table STATS, lowest temperatures first, picking up city name and latitude by joining with table STATION on the ID column:
SELECT LAT_N, CITY, TEMP_F FROM STATS, STATION WHERE MONTH = 7 AND STATS.ID = STATION.ID ORDER BY TEMP_F; Output:-

7. Query to show MAX and MIN temperatures as well as average rainfall for each station:
SELECT MAX(TEMP_F), MIN(TEMP_F), AVG(RAIN_I), ID FROM STATS GROUP BY ID; Output:-

8. Query (with subquery) to show stations with year-round average temperature above 50 degrees: -- rows are selected from the STATION table based on related values in the STATS table.
SELECT * FROM STATION WHERE 50 < (SELECT AVG(TEMP_F) FROM STATS WHERE STATION.ID = STATS.ID); Output:-

9. Create a view (derived table or persistent query) to convert Fahrenheit to Celsius and inches to centimeters:
CREATE VIEW METRIC_STATS (ID, MONTH, TEMP_C, RAIN_C) AS SELECT ID, MONTH, (TEMP_F - 32) * 5 /9,

RAIN_I * 0.3937 FROM STATS;

10. Query to look at table STATS in a metric light (through the new view):
SELECT * FROM METRIC_STATS; Output:-

11. Another metric query restricted to January below-freezing (0 Celsius) data, sorted on rainfall:
SELECT * FROM METRIC_STATS WHERE TEMP_C < 0 AND MONTH = 1 ORDER BY RAIN_C; Output:-

Anda mungkin juga menyukai