Escolar Documentos
Profissional Documentos
Cultura Documentos
AND
PROGRAMMING
METHODOLOGY
Submitted By:
Submitted To:
Page 1
SUSHANT RANJAN
MRS.KIRAN ARORA
CSE-3rd semester
100070303895
INDEX
Sr.No.
PROGRAM
DATE
PAGE
CREATING AN ARRAY
25/09/2011
4-5
TRAVERSING AN ARRAY
25/09/2011
6-7
INSERTIONTO AN ARRAY
27/09/2011
7-11
DELETING AN ARRAY
28/09/2011
12-15
02/10/2011
16-20
05/10/2011
21-22
BUBBLE SORT
04/10/2011
23-26
MULTIPLICATIONOF
MATRICES
TWO 28/09/2011
27-37
LINKED LIST(INSERTION)
28/09/2011
38-55
28/09/2011
56-71
29/09/2011
72-80
LINKED LIST(DELETION)
* AT THE BEGINNING
*AT THE END
*AT ANY POSITION
11
Page 2
SIGNATURE
An array is the collection of different variables under the same name. when elements
of linear structure are represented by means of sequential or contiguous memory
locations, these linear structures are called arrays. It is one of the simplest data
structures and are very easy to traverse, search, sort, insertion and deletion.
Page 3
LINEAR ARRAY
/*NAME OF THE PROGRAM=CREATING AN ARRAY
DESIGNED BY= SUSHANT RANJAN
DATE=25\09\2011*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
inti,n,a[6];
cout<<"\n"<<"\t\t\tENTER THE NUMBER OF ELEMENTS=";
cin>>n;
cout<<"ENTER THE SERIES"<<"\n";
for(i=1;i<=n;i++)
{
cout<<"ELEMENT["<<i<<"]=";
cin>>a[i];
}
cout<<"\n"<<"THE SERIES IS"<<"\n";
for(i=1;i<=n;i++)
{
cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";
}
getch();
}
Page 4
Page 5
TRAVERSING AN ARRAY
getch();
}
Page 7
EXAMPLE:
Suppose we have the following array:
arr[5]={5,7,2,1,3}
And we need to insert the element 6 at the 2nd position, after insertion:
arr[5]={5,6,7,2,1,3}
Thats exactly how insertion is done!
ALGORITHM:
Algorithm for Insertion of an element in an array
Suppose, the array to be arr[max], pos to be the position at which the element
num has to be inserted. For insertion, all the elements starting from the position pos
are shifted towards their right to make a vacant space where the element num is
Page 8
inserted.
STEP 1: set i=max.
STEP 2: repeat steps 3 & 4 while i>=pos.
STEP 3: set arr[i+1]=arr[I].
STEP 4: set i=i-1;
STEP 5: set arr[i] = num.
STEP 6: set max=max+1;
STEP 7: exit.
Page 9
cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";
}
getch();
}
Page
11
EXAMPLE:
Suppose we have the following array:
arr[5]={5,6,7,2,1}
Now we wish to delete the element at position 3rd.
After deletion array is:
arr[5]={5,6,2,1,0}
All the elements after the 3rd have shifted to their left and the vacant space is filled
with 0.
Thats exactly how deletion is done!
ALGORITHM:
Suppose, the array to be arr[max], pos to be the position from which the element
has to be deleted. For deletion, all the elements to the right of the element at position
pos are shifted to their left and the last vacant space is filled with 0.
Page
13
for(i=1;i<=n;i++)
{
cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";
}
getch();
}
Page
15
SEARCHING
Linear Search:
In linear search, each element of the array is compared with the given ITEM to be
searched for, one by one. This method, which traverses the array sequentially
to locate the given ITEM, is called linear or sequential search.
ALGORITHM:
Algorithm for linear search.
Algorithm for searching ITEM in an array arr[max] with lower bound L and upper
bound U. as soon as the search is successful, it jumps out of the loop
otherwise continues till the last element.
/* end loop */
}
STEP 4: k=k+1
STEP 5: If k>max then
Print SEARCH UNSUCCESSFUL.
STEP 6: Exit.
Binary Search:
Binary search method is popular for searching a specific item in an ordered array (sorted). It
can perform the search in minimum possible comparisons, but it needs the array to be sorted
in any order. In binary search , the ITEM is searched for in smaller segments (nearly half
the previous segment) after every stage.
ALGORITHM:
Algorithm for Binary search.
Suppose,
Page
18
LINEAR SEARCH
/*NAME OF THE PROGRAM=LINEAR SEARCH
DESIGNED BY=SUSHANT RANJAN
DATE=2/10/2011*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[6];
intn,i;
intnum;
cout<<"\n"<<"\t\t\tENTER THE SIZE OF THE ARRAY=";
cin>>n;
cout<<"ENTER THE SERIES"<<"\n";
for(i=1;i<=n;i++)
{
cout<<"ELEMENT["<<i<<"]=";
cin>>a[i];
}
cout<<"\n"<<"THE SERIES IS"<<"\n";
for(i=1;i<=n;i++)
{
cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";
}
cout<<"\n"<<"ENTER THE ELEMENT=";
cin>>num;
a[n+1]=num;
Page
19
intloc=0;
while(a[loc]!=num)
{
loc++;
}
if(loc==(n+1))
{
cout<<"\n"<<"SEARCH UNSUCCESSFUL";
}
else
cout<<"\n"<<"LOCATION="<<(loc);
getch();
}
Page
20
BINARY SEARCH
/*NAME OF THE PROGRAM=BINARY SEARCH
DESIGNED BY=SUSHANT RANJAN
DATE=5/10/2011*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5];
inti,n;
intnum,beg,end,mid;
cout<<"\n"<<"\t\tENTER THE NUMBER OF ELEMENTS IN THE SERIES=";
cin>>n;
cout<<"ENTER THE SERIES"<<"\n";
for(i=1;i<=n;i++)
{
cout<<"ELEMENT["<<i<<"]=";
cin>>a[i];
}
cout<<"\n"<<"THE SERIES IS"<<"\n";
for(i=1;i<=n;i++)
{
cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";
}
cout<<"\n\n"<<"ENTER THE NUMBER FOR THE SEARCH OPERATION=";
Page
21
cin>>num;
beg=1;
end=n;
while(beg<=end)
{
mid=int((beg+end)/2);
if(a[mid]==num)
{
cout<<"\n"<<"THE LOCATION IS="<<(mid);
break;
}
if(num<a[mid])
end=(mid-1);
else
//if(num>a[mid])
beg=(mid+1);
}
if(a[mid]!=num)
cout<<"SEARCH UNSUCCESSFUL";
getch();
}
Page
22
BUBBLE SORTING
EXAMPLE:
Suppose we have the following array:
arr[5]={5,1,7,2,8}
Now we wish to sort the array using bubble sort.
Pass1: {1, 5, 2, 7, 8}
Pass2: {1, 2, 5, 7, 8}
Pass3: {1, 2, 5, 7, 8}
Page
23
Pass4: {1, 2, 5, 7, 8}
ALGORITHM:
FOR I = L TO U
FOR J = L TO (U-1)
IF AR[J] > AR[JA1] THEN
temp = AR[J]
AR[J] = AR[J+1]
END OF INNER LOOP
END OF OUTER LOOP
Page
24
for(j=1;j<=(n-i);j++)
{
if(a[j]>a[j+1])
{
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\n"<<"THE SORTED SERIES IS"<<"\n";
for(i=1;i<=n;i++)
{
cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";
}
getch();
}
Page
26
Introduction to arrays:
An array is a collection of variables of the same type that are referenced by the
same name. When elements of linear structure are represented by means of
sequential or contiguous memory locations, these linear structures are called arrays.
Two types of arrays:
a)- one-dimensional arrays
b)-multi-dimensional arrays
type of an array is known as the BASE TYPE of the array. The elements
of the array are referred to by only a single subscript. It is also called
VECTOR.
Page
27
Address
2007
B)
2002
2003
2004 2005
2006
column 0
Row
0
Row
A[1][1]
28TH SEPTEMBER,2011
#include<iostream.h>
Page
28
*/
#include<conio.h>
#include<dos.h>
#include<ctype.h>
#include<process.h>
int i;
void exit()
{
cout<<"\n\n\n\n\n\n\n\t\t\t\t";
cout<<"PROGRAM TERMINATING ";
for(i=0;i<6;i++)
{
delay(1000);
cout<<".";
}
exit(0);
void main()
{
clrscr();
int n,i,j;
Page
29
char ch,ch2;
do
{
clrscr();
menu:
cout<<"\n\n\n\t\t\t*********MENU*********";
cout<<"\n\n\n\n\tFIND THE MULTIPLICATION
ARRAY\n\t\tb)2-D ARRAY\n\t\tq)QUIT";
OF
\n\n\t\ta)LINEAR
switch(ch)
{
case 'a':int arr[10],arr1[10],mul=0;
cout<<"\n\n\n\nYOU CHOSE LINEAR ARRAY ";
star:
cout<<"\n\n\nNO. OF ROWS OF 1st ARRAY :"; //no.of columns of 2nd
array=no. of rows of 1st array
cin>>n;
if(n<=0)
{
cout<<"\n\nwrong choice ";
Page
30
goto star;
}
clrscr();
{
cout<<"\n\t\t"<<arr[i]<<"\t\t\t\t\t"<<arr1[i];
}
{
cout<<"wrong choice (insert values < 10 )";
goto start;
Page
32
clrscr();
cout<<"\n\n\t2nd ARRAY";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<r;j++)
cout<<"\t"<<a1[i][j];
}
for(i=0;i<n;i++)
{
cout<<"\n\t";
for(j=0;j<r;j++)
{
for(int s=0;s<m;s++)
mul1[i][j]+=a[i][s]*a1[s][j];
cout<<mul1[i][j]<<" ";
}
}
break;
case 'q':clrscr();
exit();
Page
35
OUTPUT :
Page
36
Page
37
ALGORITHM:
This algorithm deals with the insertion in the beginning of the linked list.
Step 1: ptr=start;
Step 2: newptr= new node;
Step 3: if newptr=NULL
Print no space available! aborting!!
Step 4: else
{
Newptr->info=ITEM.
Newptr->link=NULL.
}
Step 5: if start=NULL then
Start=newptr
Step 6: else
{
Save=start
Start=newptr
newptr->link=save
}
Step 7: exit
TOPIC: Insertion of a new node at the end of a linked list.
ALGORITHM:
This algorithm deals with the insertion of a new node at the end of the linked list.
Step 1: declare pointers START, PTR, NEWPTR, REAR.
Step 2: ptr=START.
Step 3: NEWPTR= new node
Step 4: if NEWPTR=NULL
Print no space available! Aborting!!
exit
Step 5: else
{
NEWPTR->LINK=NULL
}
Step 6: if START=NULL then
Page
39
{
Start=newptr
Rear=newptr
}
Step 7: REAR->LINK=NEWPTR
Step 8: REAR=NEWPTR
Step 9: exit.
DESIGNED BY-
SUSHANT RANJAN
DATE-
28TH SEPTEMBER,2011
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<process.h>
#include<ctype.h>
struct node {
Page
40
*/
int data;
node *next;
} *start,*ptrr,*newptr,*ptr,*save,*rear;
node * newnode(int inf)
{
ptr=new node;
ptr->data=inf;
ptr->next=NULL;
return ptr;
}
void print(node *np)
{
cout<<"\n\nLINKED LIST : \n";
while(np!=NULL)
{
cout<<" "<<np->data;
np=np->next;
}
}
void insertbeg(node *np)
{
if(start==NULL)
start=rear=np;
Page
41
else
{
save=start;
start=np;
np->next=save;
}
}
void insertend(node *np)
{
if(start==NULL)
start=rear=np;
else
{
rear->next=np;
rear=np;
}
}
void insertbet(int pos,node *np)
{
ptrr=start;
if(start==NULL)
{
start=rear=np;
}
Page
42
else if(pos==1)
insertbeg(np);
else
{
for(int i=0;i<pos-2;i++)
{
ptrr=ptrr->next;
}
np->next=ptrr->next;
ptrr->next=np;
}
}
void main()
{
char ch,ch1,ch2,ch3,c=175;
int i=0,info;
start=rear=NULL;
menu:
do
{
clrscr();
cout<<"\n\n\n\t\t**********MENU**********";
cout<<"\n\n\ta)-CREATE A NEW NODE\n\tb)-PRINT THE LINKED LIST";
cout<<"\n\n\t\tenter ur choice : ";
Page
43
ch=getche();
delay(500);
clrscr();
switch(ch)
{
case 'a':cout<<"\n\n\n\t\tU CHOSE INSERTING A NEW NODE IN THE LINKED
LIST ";
cout<<"\n\n\n\t\t*******INSERTION MENU*******";
cout<<"\n\n\ta)-INSERTION AT THE BEGINING\n\tb)-INSERTION AT
THE END";
cout<<"\n\tc)-INSERTION IN BETWEEN THE LIST\n\tr)-MAIN MENU";
cout<<"\n\n\t\tenter ur choice : ";
ch1=getche();
delay(500);
clrscr();
switch(ch1)
{
case 'a':char chinb;
cout<<"\n\n\nU CHOSE INSERTION AT THE BEGINING";
do
{
i++;
cout<<"\n\ndata of "<<i<<" node : ";
cin>>info;
newptr=newnode(info);
Page
44
insertbeg(newptr);
print(start);
cout<<"\n\n\tU WANNA ENTER MORE NODES(Y/N) : ";
chinb=getche();
delay(500);
}while(chinb=='Y'||chinb=='y');
break;
Page
45
default:for(int i=0;i<10;i++)cout<<"\n";
cout<<"\n\t\t\tThank you for using the program!\n\n";
cout<<"\t\t\t "<<c<<"---> RAUSHAN <---"<<c;
cout<<"\n\n\t\t\tPlease wait while program exits.\n\t\t\t";
for(i=0;i<10;i++)
{
cout<<c<<c<<c;
delay(200);
}
exit(0);
}
cout<<"\n\n\tU WANNA CONTINUE(Y/N) : ";
ch3=getche();
delay(500);
ch3=toupper(ch3);
if(ch3!='Y')
Page
47
{
clrscr();
for(int i=0;i<10;i++)cout<<"\n";
cout<<"\n\t\t\tThank you for using the program!\n\n";
cout<<"\t\t\t "<<c<<"---> RAUSHAN <---"<<c;
cout<<"\n\n\t\t\tPlease wait while program exits.\n\t\t\t";
for(i=0;i<10;i++)
{
cout<<c<<c<<c;
delay(200);
}
exit(0);
}
}while(ch3=='y'||ch3=='Y');
getch();
}
Page
48
OUTPUT :
Page
49
Page
50
Page
51
Page
52
Page
53
Page
54
Page
55
Page
56
Page
57
/*
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<ctype.h>
#include<process.h>
struct node{
int data;
node *next;
} *start,*newptr,*rear,*ptr,*save;
node * newnode(int n)
{
ptr=new node;
ptr->data=n;
ptr->next=NULL;
return(ptr);
}
*/
{
if (start==NULL)
start=rear=ptr;
else
rear->next=ptr;
rear=ptr;
}
void delbeg()
{
if(start==NULL)
cout<<"\n\n\tUNDERFLOW";
else
ptr=start;
Page
59
start=ptr->next;
delete ptr;
}
void delend()
{
ptr=start;
if(start==NULL)
cout<<"\n\n\tUNDERFLOW";
else if(start->next==NULL)
delbeg();
else
{
while(ptr!=rear)
{
if(ptr!=rear)
save=ptr;
ptr=ptr->next;
}
}
rear=save;
rear->next=NULL;
delete ptr;
}
Page
60
void main()
{
clrscr();
start=rear=NULL;
Page
61
int info,i=1;
char ch,ch2,ch1,ch3,chd;
clrscr();
do
{
start:
cout<<"\n\nU WANNA ENTER A NODE(y/n) : ";
ch2=getche();
ch2=tolower(ch2);
while(ch2=='y')
{
cout<<"\n\nDATA FOR "<<i++<<" RECORD : ";
cin>>info;
newptr=newnode(info);
insback(newptr);
print(start);
cout<<"\n\nWANNA ENTER MORE RECORDS (y/n) : ";
ch2=getche();
ch2=tolower(ch2);
delay(500);
}
clrscr();
Page
62
menu:
cout<<"\n\n\t\t\t********MENU********";
cout<<"\n\n\ta)-DELETE A NODE\n\tb)-PRINT";
cout<<"\n\n\t\tenter ur choice : ";
ch=getche();
ch=tolower(ch);
delay(500);
clrscr();
switch(ch)
{
case 'a': cout<<"\n\n\t\t********DELETION MENU********";
cout<<"\n\n\ta)-DELETE FROM BEGINING\n\tb)-DELETE AT THE
END";
cout<<"\n\tc)-DELETE IN BETWEEN\n\tr)-RETURN TO MAIN
MENU";
cout<<"\n\n\t\tenter ur choice : ";
chd=getche();
chd=tolower(chd);
delay(500);
clrscr();
switch(chd)
{
case 'a':char chdbeg;
cout<<"\n\n\nU CHOSE DELETION AT THE BEGINING";
do
Page
63
{
i--;
delbeg();
print(start);
cout<<"\n\nU WANNA DELETE MORE NODES(Y/N) : ";
chdbeg=getche();
delay(500);
}while(chdbeg=='Y'||chdbeg=='y');
break;
}while(chdbet=='Y'||chdbet=='y');
break;
break;
exit(0);
Page
67
OUTPUT:
Page
68
Page
69
Page
70
Page
71
Page
72
Page
73
Step 2: ptr=START.
Step 3: NEWPTR= new node
Step 4: if NEWPTR=NULL
Print no space available! Aborting!!
exit
Step 5: else
{
NEWPTR->LINK=NULL
}
Step 6: if START=NULL then
{
Start=newptr
Rear=newptr
}
Step 7: REAR->LINK=NEWPTR
Step 8: REAR=NEWPTR
Step 9: exit.
TOPIC: Searching a node in a linked list by comparing data elements of each node
with the given item.
ALGORITHM: This algorithm finds the location LOC of the nodes N which
contains the ITEM. If ITEM doesnt appear in the list, then procedure sets
LOC=NULL.
Step 1: If START=NULL, then:
Set LOC=NULL and return;
Step 2: If INFO->START=ITEM, then:
Set LOC=START and return;
Step 3: Set PTR=START and PTR=LINK[PTR].
Step 4: Repeat steps 5 and 6 while PTR!=NULL.
Step 5: If INFO[PTR]=ITEM, then:
Set LOC=PTR and return.
Step 6: PTR=LINK[PTR].
Step 7: Set LOC=NULL [search unsuccessful].
Step 8: Exit.
Page
75
/*
WRITE A PROGRAM IN C++ TO CREATE A NEW NODE,INSERT
THE NEW NODE AT THE END AND SEARCH THE NODE.
DESIGNED BY-
SUSHANT RANJAN
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<process.h>
#include<dos.h>
struct node{
Page
76
*/
int data;
node *next;
} *start,*newptr,*ptr,*rear;
int flag=0,n=0,j=0,c=0;
node * newnode(int n)
{
ptr=new node;
ptr->data=n;
ptr->next=NULL;
return(ptr);
}
void insback(node* ptr)
{
if (start==NULL)
start=rear=ptr;
else
rear->next=ptr;
rear=ptr;
}
while(ptr!=NULL)
{
cout<<" "<<ptr->data;
ptr=ptr->next;
}
}
j++;
arr[j]=ncount;
}
ptr=ptr->next;
}
}
if(flag==1)
{
cout<<"\n\n\tSEARCH SUCCESSFUL";
cout<<"\npresent at position : ";
for(i=1;i<=j;i++)
cout<<arr[i]<<" ";
}
else
cout<<"\n\nsearch unsuccessful";
}
void main()
{
clrscr();
start=rear=NULL;
int arr[10],num,info,i=0;
char ch,ch2;
clrscr();
do
Page
79
{
cout<<"\n\nU WANNA ENTER A NODE(y/n) : ";
ch=getche();
ch=tolower(ch);
while(ch=='y')
{
c++;
cout<<"\n\nDATA FOR "<<++i<<" RECORD : ";
cin>>info;
newptr=newnode(info);
insback(newptr);
print(start);
cout<<"\n\nWANNA ENTER MORE RECORDS (y/n) : ";
ch=getche();
ch=tolower(ch);
delay(500);
}
clrscr();
cout<<"\n\nNO. TO BE SEARCHED : ";
cin>>num;
search(num);
ch2=getche();
}while(ch2=='y'||ch2=='Y');
cout<<"\n\nLINKED LIST IS : \n";
print(start);
getch();
}
OUTPUT
Page
81
Page
82