Академический Документы
Профессиональный Документы
Культура Документы
(a) Write the definition of a function Change(int P[], int N) in C++, which should
change all the multiples of 10 in the array to 10 and rest of the elements as 1. For
example, if an array of 10 integers is as follows:
After executing the function, the array content should be changed as follows:
(b) A two dimensional array ARR[50][20] is stored inthe memory along the two
with each of its elements occupying 4 bytes. Find the address of the element
ARR[30][10], if the element ARR[10][5] is stored at the memory location 15000.
(c) Write the definition of a member function Push() in C++, to add a new book in
a dynamic stack of BOOKS considering the following code is already included
in the program:
struct BOOKS
{
char ISBN[20], TITLE[80];
BOOKS*Link;
};
class STACK
{
BOOKS *Top;
public:
STACK() { Top=NULL; }
void PUSH();
void POP();
~STACK();
};
(d) Write a function REVROW(int P[][5], int N, int M) in C++ to display the content
of a two dimensional array, with each row content in reverse order.
For example, if the content of array is as follow:
The function should display output as:
(e) Convert the following Infix expression to its equivalent Postfix expression,
showing the stack content for each step of conversion:
U+V+R/(S–T)
[2+3+4+3+2=14]
AnswerS
(a)
Output-
(b)
Element size =4 bytes
Lr=Lc=0;
B=?
ARR[10][5]=15000
ARR[30][10]=?
ARR[I][J]= B + W[n(I-Lr) + (J-Lc)]
15000 = B + 4[20(10-0)+(5-0)]
15000 = B + 4[ 200+5]
15000 = B + 4[205]
B = 15000 – 820
B= 14180
Now, put the value of base address to evaluate the address of cell ARR[30][10]
ARR[30][10]= 14180 + 4[20(30-0) + (10-0)]
= 14180 + 4[600+10]
= 14180 + 2440
ARR[30][10]= 16620 Ans.
(c)
void STACK::PUSH()
{
BOOKS *temp;
temp=new BOOKS;
if(temp==NULL)
cout<<"Stack is Full";
else
{
cout<<"Enter ISBN:";
cin>>temp->ISBN;
cout<<”Enter TITLE: ”:
cin>>temp->TITLE;
temp->Link=top;
top=temp;
}
}
(d)
(e)
U * V + R / (S – T)
Stack status for infix to postfix conversion is shown below-
(U * V) + (R / (S – T))
2014
(a) Write code for a function offEven(int s[], int N) in C++, to add 5 in all the odd values
and 10 in all the even values of the array S.
(b) An array T[25][20] is stored along the row in the memory with each element requiring
2 bytes of storage. If the base address of array T is 42000, find out the location of
T[10][15]. Also, find the total number of elements present in this array.
(c) Write a user-defined function SumLast3(int A[][4], int N, int M) in C++ to find and
display the sum of all the values, which are ending with 3 (i.e., units place is 3). For
example if the content of array is:
33 13 92
99 3 12
(e) Write a function POPBOOK() in C++ to perform delete operation from a Dynamic
Stack, which contains Bno and Title. Consider the following definition of NODE, while
writing your C++ code.
struct NODE
{
int Bno;
char Title[20];
NODE *Link;
};
[3+3+2+2+4=14]
ANSWERS
#include< iostream.h>
#include< conio.h>
void oddEven(int s[], int N)
{
for(int i=0;i
{
if(s[i]%2==0)
s[i]=s[i]+10;
else
s[i]=s[i]+5;
}
}
void main()
{
clrscr();
int i, arr[5];
cout<< "Enter five elements in the array";
for(i=0;i<5;i++)
cin >> arr[i];
getch();
}
(b)
Base Address = 42000
Element size W = 2 bytes
Rows in Array = 25
Columns in Array = 20
Lr, Lc=0,0
Row Major:
T[I][J]=B+W[C(I-Lr)+ (J-Lc)]
(c)
#include < conio.h>
#include < iostream.h>
void SumLast3(int A[][3], int N, int M)
{ int num,last,sum=0;
for(int i=0; i
{
for(int j=0; j
{
num=A[i][j];
last=num%10;
if(last==3)
sum=sum+num;
}
}
cout<< "Sum of the digits ending with 3 is: "<< sum;
}
void main()
{
clrscr();
int arr[2][3] ={{33, 13, 92}, {99, 3, 12}};
SumLast3(arr,2,3);
getch();
}
(d)
The Given postfix notation:
F, T, NOT, AND, F, OR, T, AND
Push each element in the stack bucket one by one.
(e)
struct NODE
{
int BNo;
char Title[20];
NODE *link;
};
void STACK::POPBOOK()
{
NODE *temp;
//structure pointer top is holding the address of the top node
if(top==NULL) // Value NULL is initially assigned to top
cout<< "Stack is Empty";
else
{
temp=top;
temp=temp->link;
top=temp;
cout<< "The element is popped out.";
}
}
2013
(a) Write a code for a function void ChangeOver(int P[ ], int N) in C++, which re-
positions all the elements of the array by shifting each of them to the next
position and by shifting the last element to the first position.
For example: If the content of the array is:
0 1 2 3 4
12 15 17 13 21
The changed content will be:
0 1 2 3 4
21 12 15 17 13
(b) An array T[15][10] is stored along with the row in the memory with each
element requiring 8 bytes of storage. If the base address of the array T is 14,000,
then find out the location of T[10][7].
12 20 13
2 10 30
20 10 30
(e) Write a function QDELETE() in C++ to perform the delete operation on a Linked
Queue, which contains the Passenger no. and Passenger name. Consider the
following definition of node in the code:
struct node
{
char Pname[20];
node *Link;
};
[3+3+2+2+4 = 14]
ANSWERS
Answer:
(a).
#include< iostream.h >
#include< conio.h >
void ChangeOver(int P[], int N)
{
int temp;
temp=P[N-1];
for(int i=N-1;i>0;i--)
{
P[i]=P[i-1];
}
P[0]=temp;
}
void main()
{
clrscr();
int arr[20],range,i;
ChangeOver(arr,range);
getch();
}
(b)
B= 14000
E= 8 Bytes
Lr=Lc=0
T[10][7]=?
Row Major:
P[10][7] =B+E[C(I-Lr)+(J-Lc)]
= 14000 + 8[10(10-0) + (7-0)]
= 14000 + 7(107)
= 14000 + 749
= 14749 Ans
(c) #include< iostream.h >
#include< conio.h >
if(L[i][j]%10==0)
void main()
clrscr();
int M[4][4],r,c,i,j;
cin >> r;
cin >> c;
cout <<"nn";
DispNTen(M,r,c);
getch();
(d) 5, 2, *, 50, 5, /, 5, -, +
(e)
void Queue::QDELETE()
{
node *temp;
if(startPtr==NULL)
cout<<”Queue is Empty (underflow…)”;
else
{
temp= startPtr;
startPtr=startPtr->link;
cout << ”nThe record deleted is: ”;
cout << ”n PNo: ”<< temp->Pno;
cout << ”n Name : ”<< temp->Pname;
delete temp;
}
______________________________________________________________________
__________2012
a) Write a function SWAP2BEST (int ARR[ ],int Size) in C++ to modify the content
of the array in such a way that the elements, which are multiples of 10 swap with
the values present in the very next position in the array.
For example:
If the content of Array ARR is
90, 56, 45, 20, 34, 54
The content of array ARR should become
56, 90, 45, 34, 20, 54
(b). B= ?
E=2 Bytes
Lr=Lc=0
S[2][9]=7600
Column Major:
S[I][J]=B+E[(I-Lr)+R(J-Lc)]
S[2][9] = B + 2[(2-0)+20(9-0)]
7600 = B + 2(182)
B = 7600-364
B = 7236
Now Address of S[5][10] is:
S[10][5] = B+ E[(I-Lr)+R(J-Lc)]
= 7236 + 2[(10-0] + 20(5-0)]
= 7236 + 2(110)
= 7236 + 220
= 7456 Ans
(c). // declare a global variables
void insert_in_CQ( )
{
if(front==0 && rear==size-1)||(front==rear+1))
cout<< ”Underflow”;
else if(rear==-1)
front=rear=0;
else
{
rear++;
cout<< ”Enter Accession No:”;
cin>>B[rear].acc;
cout<< ”Enter Title: ”;
cin>>B[rear].Title;
}
}
2011
a)Write a Get2From1() function in C++ to transfer the content from one array ALL[
] to two different arrays Odd[ ] and Even[ ]. The Odd[ ] array should contain the
values from odd positions(1,3,5…) of ALL[ ] and Even[ ] array should contain the
values from even positions (0,2,4,…) of ALL[].
Example :
If the ALL[ ] array contains
12,34,56,67,89,90
The Odd[ ] array should contain
34,67,90
And the Even[ ] array should contain
12,56,89
b) An array G[50][20] is stored in the memory along the row with each of its
elements occupying 8 bytes. Find out the location of G[10][15], if G[0][0] is stored
at 4200.
c) Write a function in C++ to perform Delete operation on a dynamically allocated
Queue containing Members details as given in the following definition of
NODE:
struct NODE
{
long Mno; //Member Number
char Mname[20]; //Member Name
NODE *Link;
};
d)Write a DSUM() function in C++ to find sum of Diagonal Elements from a NxN
Matrix.
(Assuming that the N is a odd number)
e)Evaluate the following postfix notation of expression: True
,False,NOT,AND,True,True,AND,OR
[ 3+3+4+2+2 =14 ]
ANSWERS
Answer:
(a)
void Get2From1()
{
int All[]={12,34,56,67,89,90};
int Odd[3];
int Even[3];
int i,j,k;
j=k=0;
for(i=0;i<6;i++)
{
if(i%2==0)
{
Even[j++]=All[i];
}
else
Odd[k++]=All[i];
}
cout << "All Array :n";
for(j=0;j<6;j++)
cout<
cout << "Even Index Array :n";
for(j=0;j<3;j++)
cout<
cout << "Odd Index Array :n";
for(j=0;j<3;j++)
cout<
}
(b) Given:
W=8,N=20,B=4200
G[10][15]=4200+8(20(10-0)+(15-0))
=4200+8(200 + 15)
=4200+8(215)
=4200+1720
=5920
(d)
void DSUM()
{
int a[n][n], sum=0;
for ( int i = 0 ; i < n ; i ++)
{
for( int j = 0 ; j < n ; j ++)
{
cin >> a[i][j];
}
}
for ( int i = 0 ; i < n ; i + +)
{
for( int j = 0 ; j < n; j + +)
{
if( i==j )
{
sum = sum +a[i][j];
}
}
}
cout << " Sum of dialgonal elements is: " << sum;
}
(e)
______________________________________________________________________
_________
2010
a) Write a function REASSING() in C++ which accepts an array of integers and its
size as parameters and divide all those array elements by 5 which are divisible by
5 and multiply other array elements by 2. (3)
Sample Input Data of the array
20 12 15 60 32
Content of the array after calling REASSIGN() function
4 24 3 12 64
b) An array T [ 90 ] [ 100 ] is stored in the memory along the column with each of
the elements occupying 4 bytes. Find out the memory location for the element T [
10 ] [40 ] , if the Base Address of the array is 7200. (3)
c) Write a complete program in C++ to implement dynamically allocated Queue
containing names of cities. (4)
d) Write a function int ALTERSUM (int B [ ] [ 5 ] ,int N, int M) in C++ to find and
return the sum of elements from all alternate elements of a two- dimensional
array starting from B [ 0 ] [ 0 ] . (2)
Hint:
If the following is the content of the array.
c) Program:
# include < iostream.h >
# include < stdio.h >
# include < conio.h >
# include < stdlib.h >
# include < ctype.h >
# include < string.h >
Struct node
{
char City [ 20 ] ;
node * link;
};
class queue
{
node * front, *rear;
public:
queue () { front = rear = NULL; }
void add_Q; //Add queue
void del_Q; //Delete queue
void show_Q; //Show queue
};
void queue::add_Q()
{
node*temp;
char ct [ 20 ] ;
temp = new node;
cout << “Enter City”;
gets(ct);
strcpy(temp -> City, ct);
temp -> link=NULL;
rear-> link=temp;
rear=temp;
if(front == NULL)
front = rear;
}
// Function for deletion
void queue::del_Q()
{
node *temp;
char ct [ 20 ];
if (front == NULL)
{
cout << ”Queue Empty”;
}
else
{
temp = front;
front = front -> link;
strcpy(ct,temp -> City);
temp -> link=NULL;
cout << ”Delete value is ” << ct;
delete temp;
}
if (front == NULL)
rear = front;
}
//Function for show elements
void queue :: show_Q()
{
Node * temp;
temp = front;
clrscr();
cout << ”The Queue value are”;
while (temp! = NULL)
{
cout << ”n” << temp -> city;
temp= temp -> link;
}
}
void main()
{
node *front,*rear;
int choice;
queue QUEUE;
char opt = ‘Y’;
front = rear = NULL;
clrsrc();
do
{
cout << ”ntt Main Menu”;
cout << ”nt1. Addition of Queue”;
cout << ”nt2. Deletion from Queue”;
cout << ”nt3. Traverse of Queue”;
cout << ”nt4. Exit from Menu”;
cout << ”nn Enter your choice from above”;
cin >> choice;
switch (choice)
{
case 1:
do
{
QUEUE.add_Q();
cout << ”nDo you want to add more element < Y/N > ?”;
cin >> opt;
}
while(toupper(opt)==’Y’);
break;
case2:
opt =’Y’;
do
{
QUEUE.del_Q();
cout << ”/n Do you want to add more elements < Y/N > ?” ;
cin >> opt;
}
while(toupper(opt)==’Y’);
break;
case3:
QUEUE.show_Q();
break;
case 4:
exit(0);
}
}
while (choice ! =4);
}
1001 300
Ravyank Kapur
1005 Farida Khan 289
(b) An array T [50][20] is stored in the memory along the column with each of the
elements occupying 4 bytes. Find out the base address and address of element T
[30][15], if an element T[25][10] is stored at the memory location
9800. [4]
(c) Write a function QUEDEL() in C++ to display and delete element from a
dynamically allocated Queue containing nodes the following given structure:
[4 ]
struct NODE
{
int Itemno;
char Itemname[20];
NODE *Link;
};
d) Define a function SWAPARR() in C++ to swap (interchange) the first row
elements with the last row elements, for a dimensional integer array passed as
the argument of function. [3]
Example: If the two dimensional array contains
5 6 3 2
1 2 4 9
2 5 8 1
9 7 5 8
After swapping of the content of first row and last row, it should be as follows:
9 7 5 8
1 2 4 9
2 5 8 1
5 6 3 2
ANSWERS
(a) void SORTSCORE (Examinee E[], int N)
{
Examinee temp;
for ( int i=0; i < N; i++)
for (j=0; j < N-i-1; j++)
if ( E[j]. Score < E[j+1]. Score)
{
temp= E[j];
E[j] = E[j+1];
E[j+1] = temp;
}
}
(b ) Given,
W=4
N=50
M=20
Loc(T [25][10])=9800
Column Major Formula:
Loc(T [I][J]) =Base(T)+W*(N*J+I)
Loc(T[25][10]) =Base(T)+4*(50*10+25)
9800 =Base(T)+4*(500+25)
Base(T) =9800- 2100
Base(T) = 7700
Loc(T[30][15]) =7700 +4*(50*15 + 30)
=7700 +4*(750 +30)
=7700 +3120
=10820
(c) #include < iostream.h >
class linkedQueue
{
private :
struct NODE
{
int Itemno;
char Itemname [20];
NODE *Link;
};
NODE *front, *rear;
public :
int remove (void);
}
int linkedQueue:: remove (void)
{
int num;
NODE * temp;
if (front==NULL)
{
cout<<” QUEUE EMPTY!! “ ;
return (-1);
}
else
{
temp= front;
front= front ->Link;
num=temp -> Itemno;
delete temp;
if (front==NULL) && (rear=NULL))
return num;
}
}
(d) void SWAPARR ( int X[100] [ ],int M, int N )
{
int temp, i;
for (i=0;i
temp= X [0][i];
X[0][i] = X[N-1][i];
X[N-1][i]= temp;
}
}
(e) A + B * (C – D) / E
symbol scanned stack expressiony
A ( A
+ (+ A
B (+ AB
* (+ * AB
( (+ * ( AB
C (+*( ABC
- (+*( - ABC
D +*( - ABCD
) (+* ABCD–
/ (+/ ABCD-*
E (+/ ABCD-*E
) A B C D-* E / +
The equivalent postfix expression is
ABCD-*E/+
2008
a) Write a function in C++, which accepts an integer array and its size as
parameters and rearranges the array in reverse. Example: if an array of nine
elements initially contains the elements as
4,2,5,6,7,8,12,10
then the function should rearrange the array as
10,12,8,7,6,5,2, [4]
(b) An array Arr[40][10] is stored in the memory along the column with each element
occupying 4 bytes. Find the address of the location Arr[3][6], if the location Arr[30][10] is
stored at the address 9000. [4]
(c) Write a function in C++ to insert an element into a dynamically allocated Queue where
each node contains a name (of type string) as data. [4]
(e) Evaluate the following postfix notation of expression (Show status of Stack after
execution of each operation): [2]
4,10,5,+,*,15,3,/,-
ANSWERS
(a)
# include < iostream.h >
# include < conio.h >
# define n 9
void Reverse(int[], int);
void main()
{
int num[n], i;
cout << "Enter nine elements" << endl;
for ( i=0; i < n;i++)
{
cin >> num[i];
}
cout << "Initially the array is:" << endl;
for(i=0; i < n; i++)
{
cout << num[i] << endl;
}
Reverse(num, i-1);
getch();
}
void Reverse(int num[], int i)
{
int index, a, j, temp, k;
index=i;
a=i/2;
if(i%2==1)
a=a+1;
for(k=0;k < a;k++,i--)
{
temp=*(&num[i]);
num[i]=num[k];
*(&num[k])=temp;
}
cout << "After reversing" << endl;
for(j=0; j < = c; j++)
{
cout << num[j] << endl;
}
}
Therefore,
Arr[30][10]= Arr[0][0] + 2[40(10-0)+(30-0)]
9000= Arr[0][0] + 2[40 x 10 + 30]
9000=Arr[0][0] + 2(400 +30)
9000=Arr[0][0] + 2 x 430
9000= Arr[0][0] + 960
Arr[0][0] = 9000 – 960
Arr[0][0] = 8040
Now, to find
Arr[3][6] = 8040 + 2[40(6-0) + (3-0)]
= 8040 + 2[40 x 6 + 3]
= 8040 + 2[240 + 3]
= 8040 + (2 x 243)
= 8040 + 486
= 8526
(c)
# include < iostream.h >
# include < conio.h >
# include < stdlib.h >
struct THENODE
{
char Name[20];
THENODE *LINK;
};
class MyQueue
{
THENODE * Rear, * Front;
public:
MyQueue()
{
Rear=NULL;
Front=NULL;
}
void Insert();
};
void MyQueue:: Insert(void)
{
Rear= new NODE;
cout << "Insert name" << endl;
cin >> Rear-> Name;
if ( Front == NULL)
{
Front = Rear;
Front->Next=NULL;
}
else
{
Rear-> Next=Front;
Front=Rear;
}
}
(d)
void Arrayproduct(int a[][], int n_row, int n_col)
{
int product=1, x,y;
for(int x=0; x < c; x++)
{
for(product=1, y=0; y < r, y++)
{
product = product * a[y][x];
}
cout << "n Product of column: " << x << "=" << product;
}
}
(e)
(b) An array Array [20][15] is stored in the memory along the column with each element occupying
8 bytes. Find out the Base Address and address of the element Array[2][3] if the element Array [4]
[5] is stored at the address 1000. [4]
(c) Write a function in C++ to delete a node containing Book’s information, from a dynamically
allocated Stack of Books implemented with the help of the following structure.
[4]
struct Book
}
int BNo;
char BName[20];
Book *Next;
};
(d) Write a function in C++ which accepts a 2D array of integers and its size as arguments and
displays the elements which lie on diagonals. [2]
[Assuming the 2D Array to be a square matrix with odd dimension
i.e. 3×3, 5×5, 7×7, etc.]
Example, if the array content is
543
678
129
Output through the function should be :
Diagonal One : 5 7 9
Diagonal Two : 3 7 1
Given is:
A[4] [5] = B + W [20 [5-0] + [4-0] ]
1000 = B + 8 [20 * 5 + 4]
1000= B+ 8 (100 + 4 )
1000= B + 832
1000-832 = B
B= 168
(b) An array MAT[30[10] is stored in the memory column wise with each element occupying 8
bytes of memory. Find out the base address and the address of element MAT[20][5], if the location
of MAT[5][7] is stored at the address 1000. [4]
(d) Write a function in C++ to perform push operation on a dynamically allocated stack containing
real number. [3]
(b) An array Arr[15][35] is stored in the memory along the column with each of its
elements occupying 8 bytes. Find out the base address and the address of an element
Arr[2][5], if the location Arr[5][10] is stored at the address 4000. [4]
(d) Write a function in C++ to print the sum of all the values which are either divisible
by 2 or are divisible by 3 present in a two dimensional array passed as the argument to
the function. [3]
(b) The formula to find the address of an array element in column major form is:
A[I][J]= B + W[N(J-0) + (I-0)]
Where,
B is the base address,
W is the element size,
N is the number of rows.
Given:
An array Arr[15][35] with W=8,
Address of Arr[5][10]=4000
Now,
Arr[5][10]=4000
B + W [N (J-0) + (I-0)] =4000
B + 8 [15 (10-0) + (5-0)]= 4000
B+ 8[15 X 10 +5] =4000
B + 8(150+5)=4000
B+ 8 X 155=4000
B + 1240=4000
B=4000-1240
B=2760
Address of Arr[2][5]= B + W[N(J-0) +(I-0)] Arr[2][5]= 2760 + 8[15(5-0) +
(2-0)]
Arr[2][5]=2760+ 8[15 x 5 + 2]
Arr[2][5] = 2760 + 8[75+2]
Arr[2][5]=2760 + (8 x 77)
Arr[2][5]=2760+ 616
Arr[2][5]=3376
(c)
STACK :: PUSH()
{
Node *Temp;
Temp=new Temp;
if(Temp)
{
cout << “n Enter the value of X:”;
cin >> TempX;
cout << “n Enter the value of Y:”;
cin >> TempY;
Temp Link=Top;
Top=Temp;
}
else
cout << “n Error in memory allocation”;
}
(d)
void Sum_of_numbers(int arr[][], int row, int column)
{
int I=0, J=0, Sum=0;
for(I=0;I
{
for(J=0;J
{
if((arr[I][J]%2==0) || (arr[I][J]%3==0))
sum=sum+arr[I][J];
cout << “Sum_of_numbers = “ << sum;
}
}
}
(e)
MODEL PAPER 1
a) Write a function in C++, which accepts an integer array and its size as
arguments/parameters and exchanges the values of first half side elements with
the second half side elements of the array.
b) An array Arr[15][25] is stored in the memory along the column with each of its
elements occupying 4 bytes. Find the base address and the address of Arr[4][5].
If Arr[5][10] is located at 2600.
d) Write a function in C++ to find the sum of rows elements of a 2D array A[8][8]
containing integers
e) Evaluate the following postfix notation of expression:
10 20 + 25 15 - * 30 / [4+4+2+2+2=14]
ANSWERS
nswer:
a)
void Exchange (int A [ ], int N)
{
for (int I=0;I< N/2;I++)
{
int Temp=A[I];
A[I]=A[N/2+I];
A[N/2+I]=Temp;
}
}
b)
LOC(Arr[5][10])=Base(Arr)+W*(i+No. of Rows*j)
LOC(Arr[3][7])=Base(Arr)+4*(5+15*10)
2600=Base(Arr)+4*155
2600=Base(Arr)+620
Base(Arr)=2600-620
Base(Arr)=1980
Base Address=1980
c)
struct Node
{
float data;
Node * next;
};
void push (Node *Top, float num)
{
Node*nptr = new Node;
nptr -> data = num;
nptr -> next = NULL;
if(Top == NULL)
Top = nptr;
else
{
nptr -> next = Top;
Top = nptr
}
}
e)
MODEL PAPER 2
a) Write a function in C++ which accepts a 2D array of integers and its size as
arguments and displays the elements of the middle row and the elements of
middle column.
Example if the array content is
354
769
218
Output should be -
Middle row: 769 Middle column: 5 6 1
b) An array Arr[35][15] is stored in the memory along the row with each of its
elements occupying 4 bits. Find out the base address and the address of the
element Arr[20][5], if the location Arr[4][4] is stored at the address 2800.
e)Evaluate the following postfix expression using a stack. Show the contents of
stack after execution of each operation:
20, 8, 4, /, 2, 3, +, *, -
[4+2+4+2+2=14]
ANSWERS
a)
#include < stdio.h>
#include < iostream.h>
#include < conio.h>
const M = 10;
const N = 10;
void display_RowCol(int Array[M][N], int r, int c)
{ int row = r / 2;
int col = c / 2;
// Finding the middle row
cout << "Middle Row : ";
for(int j=0; j< c; j++)
cout << Array[row][j] << " ";
cout << endl;
// Finding the middle column
cout << "Middle Column : ";
for(j=0; j< c; j++)
cout << Array[j][col] << " ";
getch();
}
void main()
{ int Array[M][N];
int i, j;
int r, c;
cout << "Enter total no. of rows: ";
cin >> r;
cout << "Enter total no. of columns: ";
cin >> c;
if ((r == c) && ((r%2==1) && (c%2==1)))
{ cout << "Input steps";
cout << "nEnter the element in the arrayn";
for(i=0; i< r; i++)
for(j=0; j< c; j++)
{ cin >> Array[i][j]; }
}
else
{ cout << "Input row and column not valid";
getch();
return;
}
display_RowCol(Array, r, c);
}
b) Base address is
LOC(Arr[i][j])=Base(Arr)+W*(i+No. of Rows*j)
LOC(Arr[4][4])=Base(Arr)+4*(4+35*4)
2800=Base(Arr)+4*144
2800=Base(Arr)+576
Base(Arr)=2800-576
Base(Arr)=2224
LOC(Arr[i][j])=Base(Arr)+W*(i+No. of Rows*j)
LOC(Arr[20][5])=2224+4*(20+35*5)
LOC(Arr[20][5])=2224+4*195
LOC(Arr[20][5])=2224+780
LOC(Arr[20][5])=3004
c)
struct STACK
{
int rollno, age;
STACK *next;
} *top, *nptr, *ptr;
void pop()
{
if (!pop) { cout << ”nUnderflow!!” ; exit(1); }
else
{ cout << ’n’ << top -> rollno << ’t’ << top -> age;
ptr = top;
top = top -> next;
delete ptr;
}
}
void push()
{
nptr = new stack; //allocate memory
cout << “n Enter roll number and age:“ ;
cin >> nptr-> rollno >> nptr->age ;
nptr -> next = NULL;
if (!top) top = nptr;
else
{
ptr -> next = top;
top = nptr
}
}
e)
Operand/Operator Stack Status
20 20
8 20,8
4 20,8,4
/ 20,2
2 20,2,2
3 20,2,2,3
+ 20,2,5
* 20,10
- 10
Answer is 10
MODEL PAPER 3
a) An array salary[50] contains the salary of 50 employees in a company. Write a
function in C++ to find the number of employees getting salary:
i) more than 10000 ii) less than 6000
b) An array Arr[15][25] is stored in the memory along the column with each of its
elements occupying 8 bytes. Find the base address and the address of Arr[2][5].
If Arr[5][10] is located at 3800.
TOP is a pointer to the topmost node of the STACK. Write the following function:
PUSH() – TO push a node in to the stack which is allocated dynamically.
POP() – Te remove a node from the stack and to release the memory.
e) Evaluate the following postfix using a stack and show the content of the stack
after the execution of each
operation:
5,11,-,6,8,+12,*,/
[4+2+4+2+2=14]
Answer:
a)
The function is:
void sal()
{
int num1=0,num2=0;
for(i=0;i< 50;i++)
{
if(salary[i]>10000)
{
num1=num1+1;
}
if(salary[i]< 6000)
{
num2=num2+1;
}
}
cout<< ”Number of employees getting salary more than 10000=”<< num1<< endl;
cout<< ”Number of employees getting salary less than 6000=”<< num2;
}
b)
LOC(Arr[5][10])=Base(Arr)+W*(i+No. of Rows*j)
LOC(Arr[3][7])=Base(Arr)+8*(5+15*10)
3800=Base(Arr)+8*155
3800=Base(Arr)+1240
Base(Arr)=3800-1240
Base(Arr)=2560
Base Address=2560
c)
struct STACK
{
int rollno, age;
STACK*next;
} *top, *nptr, *ptr;
void pop()
{
if (!pop)
{
cout << ”n Underflow!!” ;
exit(1);
}
else
{ cout << ’n’ << top -> rollno << ’t’ << top -> age;
ptr = top;
top = top -> next;
delete ptr;
}
}
void push()
{
nptr = new stack; //allocate memory
cout << “n Enter roll number and age to be inserted : “ ;
cin >> nptr-> rollno >> nptr->age ;
nptr -> next = NULL;
if (!top) top = nptr;
else
{
ptr -> next = top;
top = nptr
}
}
d)
void convert(int Arr[5][5])
{
int i,j;
cout<< ”The array is:-“<< endl;
for(i=0;i< 5;i++)
{
for(j=0;j< 5;j++)
{
cout<< Arr[i][j];
}
cout<< endl;
}
for(i=0;i< 5;i++)
{
for(j=0;j< 5;j++)
{
if(Arr[i][j]%2==0)
Arr[i][j]=1;
}
}
cout<< ”The array is after conversion:-“<< endl;
for(i=0;i< 5;i++)
{
for(j=0;j< 5;j++)
{
cout<< Arr[i][j];
}
cout<< endl;
}
}
e)
b) An array Arr[35][15] is stored in the memory along the row with each of its elements occupying
4 bits. Find out the base address and the address of the element Arr[20][5], if the location Arr[4][4]
is stored at the address 2800.
e) Write the equivalent infix for 19, 12, +, 19, 5,*, +, 29, +
ANSWERS
a)
#include < iostream.h > void Merge(int [ ],int, int[ ], int, int[ ]) ; //
Function for merging 2 arrays void main() { int A[50], B[50], C[50], MN=0,
M, N; cout << "How many elements do you want to create first array with?
(max = 50)" ; cin >> M ; cout << "nEnter First Array’s elements
[ascending]… n" ; for(int i=0 ; i cin >> A[i] ; cout << "nHow many
elements do you want to create second array with? (max = 50)" ; cin >> N ;
MN = M + N ; // array C’s size calculated cout <<
"nEnter Second Array’s elements [descending]…n" ; for(i=0 ; i
Merge(A,M, B,N, C) ;
cout << "nnThe Merged array is as shown below… n" ; cin >> B[i] ; for(i = 0 ; i <
MN ; i++) { cout << C[i] << " "; } } void Merge (int A [ ], int M, int B[ ],
int N, int C[ ]) //function to perform merging {
int a,b,c ; //M – size of array A ; N – size of array B ; c – resultant array
for(a=0, b=N-1, c=0 ; a< M && b>=0 ;) //notice the initial values of
control vars. a,b,c {
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--] ; } }
How many elements do you want to create first array with? (max = 50)…5
Enter First Array’s elements [ascending]…
2 5 8 9 12
How many elements do you want to create second array with? (max = 50)…7
Enter Second Array’s elements [descending]..
16 12 10 8 7 3 1
The Merged array array is as shown below…
1 2 3 5 7 8 8 9 10 12 12 16
b)
Row Major:
A[i][j] = B + W [ C ( I – Lr ) + ( J – Lc ) ]
B=? , W=4 , Lr=0, Lc=0
A[4][4] = B + 4[ 15(4-0) + (4-0)]
2800 = B + 4[ 60 + 4 ]
2800 = B + 4*64
2800 = B + 256
B= 2800 – 256
B= 2544
A[20][5]= ?
A[i][j] = B + W [ C ( I – Lr ) + ( J – Lc ) ]
A[20][5] = 2544 + 4[ 15( 20-0) +(5-0)]
= 2544 + 4 [ 300 +5]
= 2544 + 4*305
= 2544 + 1220
= 3768
c)
class stack
{
int element;
stack* next;
public:
stack* push(stack*,int);
}*head,object;
e)