Professional Documents
Culture Documents
Exercise 1: A one-dimensional array P[100] is stored in memory with base address as 5000 .
Find out the memory address of P[15] and P[40], if each element of the this array requires 4
bytes.
Base (P) = 5000
W = 4
Loc (P[I]) = Base (P) + W*I
Loc (P[15]) = 5000 + 4*15
= 5000 + 60
= 5060
Loc (P[40]) = 5000 + 4*40
= 5000 + 160
= 5160
Exercise 2: A one-dimensional array A[-5..25] is stored in memory which each element
required 2 bytes. If the base address is 8000, find out the following:
a) Address of A[5] and A[-3]
b) Total no. of elements present in the array
Given, Base (A) = 8000
W = 2
Loc(A[I]) = Base (A) + W*(I-LB)
Loc (A[5]) = 8000 + 2 * (5 – (-5))
= 8000 + 20
= 8020
Loc (A[-3]) = 8000 + 2 * (-3 – (-5))
= 8000 + 4
= 8004
Total No. of Elements = UB-LB + 1 =25 – (-5) + 1 = 31
Exercise 3: A two-dimensional array Q[5][15] is stored in memory along the row with each
element requiring 2 bytes. If the base address is 6500, find out the following:
a) Address of Q[3][10]
b) Total no. of elements present in array
Given, Base (Q) = 6500
W = 2
M = 15
Row Major, Loc (Q[I][J]) = Base (Q) + W * (M*I + J)
Loc (Q[3][10]) = 6500 + 2 * (15*3 + 10)
= 6500 + 110
= 6610
Total No. of Elements = N*M = 5*15 = 75
Exercise 4: R[-4..4, 7..17] is a two-dimensional array, stored in memory along the column
with each element requiring 4 bytes. If the base address is 5000, find out the following:
c) Address of R[2][10]
d) Total no. of elements present in array
Given, Base (R) = 5000
W = 4
N = UBI-LBI+1 = 4-(-4)+1 = 9
M = UBJ-LBJ+1 =17-7+1 = 11
Column Major, Loc (R[I][J]) = Base (R) + W * (N* (J-LBJ)+ (I-LBI))
{
Temp = ARR[I];
ARR[I] = ARR[J];
ARR[J] = Temp;
}
}
}
Function to Search for an element from ARR by Binary Search
void BinarySearch(int ARR[], int L)
{
int Data, Found=0, LB=0, UB=L-1, Mid;
cout<<”Enter Data to be searched: “; cin>>Data;
while( LB<=UB && !Found)
{ Mid = (UB+LB)/2;
if(ARR[Mid] == Data)
Found++;
else if(ARR[Mid] > Data)
UB = Mid-1;
else if(ARR[Mid] < Data)
LB = Mid+1;
}
if(Found)
cout<<”Data Found at : “<<Mid<<endl;
else
cout<<”Data Not Found in the array”<<endl;
}
Function to Merge X and Y arrays of Lengths N and M (Both Store in Ascending order)
void Merge(int X[], int Y[], int ARR[], int N int M, int &L)
{
int I=0, J=0;
L=0;
while( I<N && J<M)
{ if(X[I] < Y[J])
ARR[L++] = X[I++];
else if (Y[J] < X[I])
ARR[L++] = Y[J++];
else
{
ARR[L++] = X[I++];
ARR[L++] = Y[J++];
}
}
while(I<N)
ARR[L++] = X[I++];
while(J<M)
ARR[L++] = Y[J++];
}
SumC=0;
for(R=0; R<N; R++)
{
SumC += A[R][C];
}
cout<<”Column ( “<<C<<”)= “<<SumC<<endl;
}
}
Function to find sum of diagonal elements of a square matrix A
void Diagonal ( int A[][20], int N, int &RDiag, int &LDiag)
{
int I;
RDiag=0;
for( I=0; I<N; I++)
RDiag += A[I][I];
LDiag=0;
for( I=0; I<N; I++)
LDiag += A[N-I-1][I];
}
STACK
Stack is non-primitive linear data structure in which insertion and deletion of elements takes
place from only one end, known as Top. It is also known as LIFO (Last In First Out) data
structure.
#include<conio.h>
const int Max=10;
void Push ( int S[], int &T)
{
if (T == Max-1) //Check for Stack Full
cout<<”Stack is Full !”<<endl;
else
{ T++;
cout<<”Data : “;
cin>>S[T];
}
}
void Pop ( int S[], int &T)
{
if (T == -1) // Check for Stack Empty
cout<<”Stack is Empty !”<<endl;
else
{ cout<<S[T]<<” Deleted !”<<endl;
T--;
}
}
void StackDisp ( int S[], int T)
{
for( int I=T; I>= 0; I--)
cout<<S[I]<<endl;
}
void main()
{ int Stack[Max], Top=-1;
int ch;
do
{ clrscr();
cout<<”\n\t Stack Menu “;
cout<<”\n\t 1. Push “;
cout<<”\n\t 2. Pop”;
cout<<”\n\t 3. Show”;
cout<<”\n\t 4. Quit”;
cout<<”\n\t Enter choice [1-4] : ";
cin>>ch;
switch(ch)
{ case 1:
Push (Stack, Top);
break;
case 2:
Pop (Stack, Top);
break;
case 3:
StackDisp ( Stack, Top);
break;
case 4:
cout<<”\n\ Quitting …..”;
break;
default:
cout<<”\n\t Invalid Choice !!! “;
}
getch();
}
while( ch != 4);
}
Queue
It is a non-primitive linear data structure in which insertion and deletion of elements takes
place from two opposite ends rear and front respectively. It is also known as FIFO (First In
First Out) data structure.
// Static Queue ( Queue implemented using Array)
#include<iostream.h>
#include<conio.h>
const int Max = 10;
void Qinsert ( int Q[], int &R, int &F)
{
if( R == Max-1)
cout<<”Queue is Full”<<endl;
else if( R == -1)
{
F=R=0;
cout<<”Data : “; cin>>Q[R];
}
else
{
R++;
cout<<”Data : “; cin>>Q[R];
}
}
void Qdelete ( int Q[], int& R, int& F)
{
if( F == -1)
{
cout<<”Queue is empty !”;
}
else if( F == R)
{
cout<<Q[F]<<” Deleted ! <<endl;
F=R=-1;
}
else
{
void main()
{
int Queue[Max], Front=-1, Rear=-1;
int ch;
do
{ clrscr();
cout<<”\n\t Queue Menu “;
cout<<”\n\t 1. Insert “;
cout<<”\n\t 2. Delete”;
cout<<”\n\t 3. Show”;
cout<<”\n\t 4. Quit”;
cout<<”\n\t Enter choice [1-4] : ";
cin>>ch;
switch(ch)
{
case 1:
Qinsert(Queue, Rear, Front);
break;
case 2:
Qdelete(Queue, Rear, Front);
break;
case 3:
Qdisplay(Queue, Rear, Front);
break;
case 4:
cout<<”\n\ Quitting …..”;
break;
default:
cout<<”\n\t Invalid Choice !!! “;
}
getch();
}
while( ch != 4);
}
// Static Circular Queue ( Queue implemented using Array)
#include<iostream.h>
#include<conio.h>
const int Max = 10;
void Qinsert ( int Q[], int &R, int &F)
{
if( (R+1)%Max == F)
cout<<”Queue is Full”<<endl;
else
{ R= (R+1)%Max;
cout<<”Data : “; cin>>Q[R];
}
}
void Qdelete ( int Q[], int& R, int& F)
{
if( R != F)
{ cout<<Q[F]<<” Deleted !”<<endl;
F = (F+1)%10;
}
else
{ cout<<”Queue is empty !”;
}
}
void Qdisplay( int Q[], int R, int F)
{
int I=F;
while( I != R)
{ cout<<Q[I]<<endl;
I = (I+1)%Max;
}
}
void main()
{
int Queue[Max], Front=0, Rear=0;
int ch;
do
{ clrscr();
cout<<”\n\t Circular Queue Menu “;
cout<<”\n\t 1. Insert “;
cout<<”\n\t 2. Delete”;
cout<<”\n\t 3. Show”;
cout<<”\n\t 4. Quit”;
cout<<”\n\t Enter choice [1-4] : ";
cin>>ch;
switch(ch)
{
case 1:
Qinsert(Queue, Rear, Front);
break;
case 2:
Qdelete(Queue, Rear, Front);
break;
case 3:
Qdisplay(Queue, Rear, Front);
break;
case 4:
cout<<”\n\ Quitting …..”;
break;
default:
cout<<”\n\t Invalid Choice !!! “;
}
getch();
}
while( ch != 4);
}
Dynamic allocation of memory
Pointer: Pointer is an address of a memory location. (A variable, which holds an address of a
memory location, is known as a Pointer variable/ Simple Pointer)
Declaration of a pointer variable
int *P; // Pointer to an integer
float *F; // Pointer to a float
char *ch; // Pointer to a Character
When a Simple variable is declare as
int Amt = 900;
It means Amt is a place in memory area that holds an integer type value.
The reference operator & return an address of a memory location of a variable to which it is
applied.
int Amt = 900;
int *Ptr;
Ptr = &Amt;
Amt += 100;
(*Ptr) -= 50;
cout<<”Amt=”<<Amt<<” *Ptr=”<<*Ptr<<endl;
cout<<”&Amt=”<<&Amt<<” Ptr=”<<Ptr<<endl;
Above program will display the following output as Ptr holds an address of Amt and hence
any change in Amt will be same as change in *Ptr.
Amt=950 *Ptr=950
&Amt=x8ffebab4 Ptr= x8ffebab4
Using new operator
new operator in C++ returns the address of a block of unallocated bytes (depending on data
type a pointer pointing to).
float *F, *G; // F and G point to float
F = new float; // Allocates storage for 1 float
*F = 58.75; // Assigns a float value to *F
Pointer to an array
A pointer, which stores an address of an array, is known as pointer to an array.
Example
int A[] = {90, 85, 68, 50};
int *Ptr;
Ptr = A; // Pointer to an array (same as Ptr =
&A[0])
cout<<”*Ptr=”<<*Ptr<<Endl;
Ptr+=2; // Increment of Ptr by 4 bytes
cout<<”*Ptr=”<<*Ptr<<Endl;
(*Ptr) -=10; // A[2] or *Ptr becomes 58
cout<<”A[2]=”<<A[2]<<endl;
Ptr--; // Decrement of Ptr by 2 bytes
cout<<”*Ptr=”<<*Ptr<<Endl;
Output
*Ptr=90
*Ptr=68
A[2]=58
*Ptr = 85
Array of pointers
An array, who’s each element is pointer type, is known as Array of pointers.
Example
int *A[3];
for( int I=0; I<3; I++)
{
A[I] = new int;
*A[I] = I*25;
}
for( I=2; I>=0; I--)
cout<<”*A[“<<I<<”]=”<<*A[I]<<endl;
for(I=0; I<3; I++)
delete A[I];
Output
*A[2] = 50
*A[1] = 25
*A[0] = 0
Pointer to Structure
A pointer, which stores the address of structure type data, is known as pointer to structure.
Example
#include<iostream.h>
struct Graph
{
Int X, Y;
};
void main()
{
Graph *G;
G = new Graph;
G->X = 25;
G->Y = G->X*2 -2;
cout<<”G->X=”<<G->X<<” G->Y=”<<G->Y<<endl;
delete G;
}
Output
G->X=25 G->Y=48
cout<<”\n\t 4. Quit”;
cout<<”\n\t Enter choice [1-4] : ";
cin>>ch;
switch(ch)
{
case 1:
S.Push ();
break;
case 2:
S.Pop ();
break;
case 3:
S.Disp ();
break;
case 4:
cout<<”\n\ Quitting …..”;
break;
default:
cout<<”\n\t Invalid Choice !!! “;
}
getch();
}
while( ch != 4);
}
Dynamic Queue (Queue implementation using Linked List)
#include<iostream.h>
#include<conio.h>
struct NODE
{
int Data;
NODE *Next;
};
class QUEUE
{
NODE *Front, *Rear;
public:
QUEUE()
{
Front=NULL;
Rear=NULL;
}
void Qinsert();
void Qdelete();
void QDisplay();
~QUEUE();
};
void QUEUE :: Qinsert()
{
NODE *Temp;
Temp = new NODE;
cout<<”\n\t Data : “;
cin>>Temp->Data;
Temp->Next=NULL;
if(Rear==NULL)
{
Front=Temp;
Rear=Temp;
}
else
{
Rear->Next=Temp;
Rear=Temp;
}
}
void QUEUE :: Qdelete()
{
if(Front == NULL)
cout<<”Queue is Empty !”;
else
{
NODE *Temp=Front;
cout<<Front->Data<<” Deleted”;
Front=Front->Next;
Delete Temp;
If(Front==NULL)
Rear=NULL;
}
}
void QUEUE :: Qdisplay()
{
NODE *Temp=Front;
while(Temp != NULL)
{
cout<<Temp->Data<<Endl;
Temp = Temp->Next;
}
}