Sponsor Area
T[25][30] is a two dimensional array, which is stored in the memory along the row with each of its element occupying 2 bytes, find the address of the element T[10][15], if the element T[5][10] is stored at the memory location 25000.
LOC(T[10][15]) = LOC(T[5][10]) + 2(30*(10-5) + (15-10))
= 25000 + 2(150 + 5)
= 25000 + 2(155)
= 25000 + 310
= 25310
Write the definition of a member function ADDMEM() for a class QUEUE in C++, to add a MEMBER in a dynamically allocated Queue of Members considering the following code is already written as a part of the program.
struct Member
{
int MNO;
char MNAME[20];
Member *Next;
};
class QUEUE
{
Member *Rear,*Front;
public:
QUEUE(){Rear=NULL;Front=NULL;}
void ADDMEM();
void REMOVEMEM();
~QUEUE();
};
void QUEUE::ADDMEM()
{
Member *T;
T=new Member;
cin>>T->MNO;
gets(T->MNAME);
T->Next=NULL;
if (Rear==NULL)
{
Rear=T;Front=T;
}
else
{
Rear->Next=T;
Rear=T;
}
}
T[20][50] is a two dimensional array, which is stored in the memory along the row with each of its element occupying 4 bytes, find the address of the element T[15][5], if the element T[10][8] is stored at the memory location 52000.
Loc(T[I][J])
=BaseAddress + W [( I – LBR)*C + (J – LBC)]
(where
W=size of each element = 4 bytes,
R=Number of Rows=20, C=Number of Columns=50)
Assuming LBR = LBC = 0
LOC(T[10][8])
52000 = BaseAddress + W[ I*C + J]
52000 = BaseAddress + 4[10*50 + 8]
52000 = BaseAddress + 4[500 + 8]
52000 = BaseAddress + 4 x 508
BaseAddress = 52000 2032
= 49968
LOC(T[15][5])= BaseAddress + W[ I*C + J]
= 49968 + 4[15*50 + 5]
= 49968 + 4[750 + 5]
= 49968 + 4 x 755
= 49968 + 3020
= 5298
Write the definition of a member function INSERT() for a class QUEUE in C++, to insert an ITEM in a dynamically allocated Queue of items considering the following code is already written as a part of the program.
struct ITEM
{
int INO; char INAME[20];
ITEM *Link;
};
class QUEUE
{
ITEM *R,*F;
public :
QUEUE() {R=NULL;F=NULL;}
void INSERT();
void DELETE();
~QUEUE();
};
void QUEUE::INSERT()
{
ITEM *T = new ITEM;
cin>>T->INO;
gets(T->INAME);
T->Link = NULL;
if(R==NULL)
{
F=T; R=T;
}
else
{
R->Link=T; R=T;
}
}
Write a definition for a function SHOWMID (int P[][5], int R, int C) in C++ to display the elements of the middle row and middle column from a two-dimensional array P having R number of rows and C number of columns. For example, if the content of the array is as follows:
115 | 112 | 116 | 101 | 125 |
103 | 101 | 121 | 102 | 101 |
185 | 109 | 109 | 160 | 172 |
The function should display the following as output :
103 101 121 102 101
116 121 109
void SHOWMID(int P[][5],int R,int C)
{
if(R%2!=0)
{
for (int J=0;J<C;J++)
cout<<P[R/2][J]<< ' ';
}
else
cout<<'No Middle Row';
cout<<endl;
if(C%2!=0)
{
for (int I=0;I<R;I++)
cout<<P[I][C/2]<< ' ';
}
else
cout<<'No Middle Column';
}
A two-dimensional array P[20] [50] is stored in the memory along the row with each of its element occupying 4 bytes, find the address of the element [10] [30], if the element P[5] [5] is stored at the memory location 15000.
LOC(P[10][30])
= Loc(P[5][5])+ W[(ILBR)*C+(JLBC)]
= 15000 + 4[(105)*50 + (305)]
= 15000 + 4[ 5*50 + 25]
= 15000 + 4 *275
= 15000 + 1100
= 16100
Write the definition of a member function Pop() in C++, to delete a book from a dynamic stack of TEXTBOOKS considering the following code is already included in the program.
struct TEXTBOOKS
{
char ISBN[20]; char TITLE[80];
TEXTBOOKS *Link;
};
class STACK
{
TEXTBOOKS *Top;
public:
STACK(){Top=NULL;}
void Push();
void Pop();
~STACK();
};
void STACK::POP()
{
if (Top!=NULL)
{
TEXTBOOKS *Temp;
Temp=Top;
cout<ISBN<<Top>TITLE<<'deleted'<<endl;
Top=Top->Link;
delete Temp;
}
else
cout<<'Stack Empty'<<endl;
}
Write a function REVCOL (int P[] [5], int N, int M) in C++ to display the content of a two-dimensional array, with each column content in reverse order.
Note: Array may contain any number of rows.
For example, if the content of the array is as follows:
15 | 12 | 56 | 45 | 51 |
13 | 91 | 92 | 87 | 63 |
11 | 23 | 61 | 46 | 81 |
The function should display output as:
11 | 23 | 61 | 46 | 81 |
13 | 91 | 92 | 87 | 63 |
15 | 12 | 56 | 45 | 51 |
void REVCOL(int P[][5],int N,int M)
{
for(int I=0;I<N/2;I++)
{
for(int J=0;J<M;J++)
{
int T = P[I][J];
P[I][J] = P[NI1][J];
P[NI1][J] = T;
}
}
for(I=0;I<N;I++)
{
for(int J=0;J<M;J++)
cout<<P[I][J];
cout<<endl;
}
}â
Write a function ALTERNATE (int A[][3],int N,int M) in C++ to display all alternate element from two-dimensional array A (starting from A[0][0]).
For example:
If the array is containing:
23 | 54 | 76 |
37 | 19 | 28 |
62 | 13 | 19 |
The output will be:
23 | 76 | 19 | 62 | 19 |
#include <iostream.h>
#include <conio.h>
void process_Array(int Arr[][3],int x, int y);
void process_Array(int A[][3],int N, int M)
{
clrscr();
for (int R = 0; R < N; R++)
{
if(R%2==0)
{
for (int C = 0; C < M; C=C+2)
{
cout<< A[R][C]<<' ';
}
}
else
{
for (int C = 1; C < M;C=C+2)
{
cout<< A[R][C]<<' ';
}
}
}
cout<<endl;
cout<<endl;
for (int I = 0; I < N; I++)
{
for (int J = 0; J < M; J++)
{
cout << A[I][J]<<' ';
}
cout<<endl;
}
}
int main ()
{
int arr[3][3] ={{23, 54, 76},
{37, 19, 28},
{62, 13, 19},
};
process_Array(arr,3,3);
return 0;
}
Sponsor Area
Sponsor Area