Sponsor Area

Data Structure

Question
CBSEENCO12011525

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.

Solution

LOC(T[10][15]) = LOC(T[5][10]) + 2(30*(10-5) + (15-10))
                       = 25000 + 2(150 + 5)
                       = 25000 + 2(155)
                       = 25000 + 310
                       = 25310

Question
CBSEENCO12011526

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();
};

Solution

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;
	}
}

Question
CBSEENCO12011619

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.

Solution

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

Question
CBSEENCO12011620

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();
};

Solution

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;
	}
}