24
1 Worksheet Pointers 1. Give the output of the following program : # include <iostream.h> # include <string.h> void main( ) { char *str=”CALIFORNIA”; for (int i=0; str[i]; i++) { for (int j=0; j<=i; j++) cout<<str[i]<<endl; } } 3. Give the output of the following program : void strfun ( char **s) { char *n=”Dylan”; *s=n; } void main( ) { char *str=”Lenon”; strfun(&str); cout<<str<<endl; } 5. Give the output of the following program : # include <iostream.h> int a=3; void demo(int &x, int y, int *z) { a+=x; y*=a; *z=a+y; cout<<a<<’\t’<<x<<’\t’<<y<<’\t<<z; } void main( ) { int a=2, b=5; demo(::a,a,&b); cout<<::a<<” “<<a<<” “<<b; } 7. Give the output of the following program : # include <iostream.h> void main( ) { float x= 5.999; float *y, *z; y=&x; z=y; cout<<x<<’\t’<<*(&x)<<’\t’<<*y<<’\t’<<*z; } 8. Give the output of the following program : # include <iostream.h> void main( ) { static int[]={0,1,2,3,4}; 2. Give the output of the following : # include <iostream.h> int main( ) {int arr[]= 10,11,12,13,14}; int i,*p; for (p=arr, i=0; p+i <=arr+4;p++,i++) cout<<*(p+i); cout<<”\n”; return 0; } 6. Give the output of the following : # include <iostream.h> void main( ) { int a[]={3,5,7,6}; int *p,**q,***r,*s,*t,**ss; p=a; s=p+1; q=&s; t=(*q+1); ss=&t; r=&ss; cout<<*p<<’\t’<<**q<<’\t’<<***r; } 4. Give the output of the following : # include <iostream.h> void junk(int a, int *b) { a= a*a; *b=*b * *b; } void main( ) { int i=6,j=-4; junk(i,&j); cout<<i<<’\t’<<j; } 9. Give the output of the following : # include <iostream.h> void main( ) { void func(int, int*); int a[5]={2,4,6,8,10}; int i,b =5; for(i=0;i<5;i++) { func(a[i], &b); cout<<a[i]<<’\t’<<b<<endl; }

Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include # include void main( ) ... 14. What is

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

1

Worksheet

Pointers

1. Give the output of the following program :

# include <iostream.h>

# include <string.h>

void main( )

{ char *str=”CALIFORNIA”;

for (int i=0; str[i]; i++)

{

for (int j=0; j<=i; j++)

cout<<str[i]<<endl;

}

}

3. Give the output of the following program :

void strfun ( char **s)

{ char *n=”Dylan”;

*s=n;

}

void main( )

{ char *str=”Lenon”;

strfun(&str);

cout<<str<<endl;

}

5. Give the output of the following program :

# include <iostream.h>

int a=3;

void demo(int &x, int y, int *z)

{ a+=x;

y*=a;

*z=a+y;

cout<<a<<’\t’<<x<<’\t’<<y<<’\t’<<z;

}

void main( )

{ int a=2, b=5;

demo(::a,a,&b);

cout<<::a<<” “<<a<<” “<<b;

}

7. Give the output of the following program :

# include <iostream.h>

void main( )

{ float x= 5.999;

float *y, *z;

y=&x; z=y; cout<<x<<’\t’<<*(&x)<<’\t’<<*y<<’\t’<<*z;

}

8. Give the output of the following program :

# include <iostream.h>

void main( )

{

static int[]={0,1,2,3,4};

2. Give the output of the following :

# include <iostream.h>

int main( )

{int arr[]= 10,11,12,13,14};

int i,*p;

for (p=arr, i=0; p+i <=arr+4;p++,i++)

cout<<*(p+i);

cout<<”\n”;

return 0;

}

6. Give the output of the following :

# include <iostream.h>

void main( )

{ int a[]={3,5,7,6};

int *p,**q,***r,*s,*t,**ss;

p=a;

s=p+1;

q=&s;

t=(*q+1);

ss=&t;

r=&ss;

cout<<*p<<’\t’<<**q<<’\t’<<***r;

}

4. Give the output of the following :

# include <iostream.h>

void junk(int a, int *b)

{ a= a*a; *b=*b * *b;

}

void main( )

{ int i=6,j=-4;

junk(i,&j);

cout<<i<<’\t’<<j;

}

9. Give the output of the following :

# include <iostream.h>

void main( )

{ void func(int, int*);

int a[5]={2,4,6,8,10};

int i,b =5;

for(i=0;i<5;i++)

{ func(a[i], &b);

cout<<a[i]<<’\t’<<b<<endl;

}

Page 2: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

2

static int *p[]= {a,a+2,a+1,a+4,a+3};

int **ptr;

ptr=p;

**++ptr;

cout<<**ptr<<ptr-p<<*ptr-a;

}

10. Give the output of the following program :

# include <iostream.h>

void main( )

{ char str[]=”Pointers and Strings”;

cout<<*(str[2])<<’\n’;

cout.write(str+5,15).put(‘\n’);

cout.write(str,20).put(‘\n’);

cout<<*(str+3)<<end;

}

12. Give the output of the following program :

# include <iostream.h>

void main( )

{ int a=32, *ptr = &a;

char ch= ‘A’, &cho=ch;

cho+=a;

*ptr+=ch;

cout<<’\n<<a<<’ ‘<<ch<<endl;

}

14. What is the difference between Heap Area and Memory Leak ?

15. Difference between Dynamic and Static Memory Allocation ?

16. What is a Self Referential Structure. Explain ?

17. What is the difference and similarity between Pointer and Array ?

18. What is the difference between New and Delete operators ?

19. Explain the working of This pointer in brief ?

20. What do you understand by Free storage Pool ?

Inheritance

Main Paper 1998

Consider the following declaration and answer the question given below :

Class PPP

{ int H;

protected :

int S;

public : void input( );

void out( );

};

class QQQ : private PPP

{ int T;

11. Give the output of the following :

# include <iostream.h>

void main( )

{ char str[ ]=”Virendra Sir”;

char *s;

s= &str[5]-5;

while(*s)

cout<<*s++;

cout<<”Thanks\n”;

}

13. Give the output of the following:

# include <iostream.h>

void main( )

{ char *ptr =”abcd”;

char ch;

ch= ++*ptr++;

cout<<ch

}

Page 3: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

3

protected :

int U;

public : void in_data( );

void out_data( );

};

class RRR : public QQQ

{ int M;

public : void Disp( );

};

(i) Name the base class and derived class of QQQ

(ii) Name the data members that can be accessed from Disp( )

(iii) Name the member function(s), which can be accessed from the object of RRR

(iv) Is the member function out( ) accessible by the objects of class QQQ

Compartment Paper 1998

Consider the following declaration and answer the question given below :

Class WORLD

{ int H;

protected :

int S;

public : void INPUT(int);

void OUTPUT( );

};

class COUNTRY : private WORLD

{ int T;

protected :

int U;

public : void INDATA( );

void OUTDATA( );

};

class STATE : public COUNTRY

{ int M;

public : void Disp( );

};

(i) Name the base class and derived class of class COUNTRY

(ii) Name the data members that can be accessed from DISPLAY( )

(iii) Name the member function(s), which can be accessed from the object of STATE

(iv) Is the member function OUTPUT( ) accessible by the objects of class COUNTRY

Main Paper 1999

Consider the following declaration and answer the question given below :

Class vehicle

{ int wheels;

protected :

int passenger;

public : void inputdata (int , int);

void outputdata( );

};

class heavy_vehicle : protected vehicle

{ int disel_petrol;

protected :

int load;

Page 4: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

4

public : void readdata (int, int );

void writedata( );

};

class bus : public heavy_vehicle

{ char make[20];

public : void fetchdata( );

void displaydata( );

};

(i) Name the base class and derived class of class heavy_vehicle

(ii) Name the data members that can be accessed from displaydata( )

(iii) Name the member function(s), which can be accessed from the object of bus class

(iv) Is the member function outputdata( ) accessible by the objects of heavy_vehicle class

Compartment Paper 1999

Consider the following declaration and answer the question given below :

Class living_being

{ char name[20];

protected :

int jaws;

public : void inputdata (char , int);

void outputdata( );

};

class animal : protected living_being

{ int tail;

protected :

int legs;

public : void readdata (int, int );

void writedata( );

};

class cow : private animal

{ int horn_size;

public : void fetchdata( );

void displaydata( );

};

(i) Name the base class and derived class of class animal

(ii) Name the data members that can be accessed from displaydata( )

(iii) Name the member function(s), which can be accessed from the object of cow class

(iv) Is the member function outputdata( ) accessible by the objects of animal class

Sample Paper

Class Data; // Forward declaration

int x;

Class Sample

{ int x;

int y;

protected :

int no;

char ch;

void processval( );

public :

Page 5: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

5

int cl;

char ab;

void putval( );

friend int check(Sample,Data);

};

Class Data

{ int i;

public :

void getvalue( );

friend int check(Sample,Data);

};

What data members can be accessed from the following functions :

Processval( ) putval( ) check( ) getvalue ( )

Main Paper 2000

b) Consider the following C++ declaration and answer the question given below :

class A

{ void anyval( );

protected :

int x, y;

void procval ( );

public :

void getvalA( );

void putvalA( );

};

class B : protected A

{ int a, b;

protected :

int c, d;

void getvalB ( );

public :

void putvalB( );

};

class C : private B

{ int p ;

protected :

int q;

void getval ( );

public :

void showval( );

};

(i) Name all the member functions, which are accessible by the object of class C

(ii) Name all protected members of class B

(iii) Name the base class and derived class of class B

(iv) Name the data members, which are accessible from member function of class C.

Compartment Paper 2000

Consider the following C++ declaration and answer the question given below :

class ALPHA

{

int x, y;

protected :

void putvalA( );

public :

Page 6: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

6

void getvalA( );

};

class BETA : private ALPHA

{

int m, n;

protected :

void putvalB( );

public :

void getvalB( );

};

class GAMMA : protected BETA

{

int a;

public :

void getdata ( );

void showdata( );

};

i) Write t+e names of the member functions, which are accessible from the

objects of class GAMMA

ii) Write the names of the data members, which are accessible from the members functions of

class BETA

iii) Name the base class and derived class of class BETA

iv) Name the private member of class GAMMA

Main Paper 2001

Give the output of the following program : 4

# include <iostream.h>

#include <string.h>

class per {

char name [20];

float salary;

public:

per(char * s, float a)

{ strcpy(name, s); salary = a; }

per * GR(per & x)

{ if(x.salary >= salary ) return &x;

else return this;

}

void display( )

{ cout<<“Name :”<<name<<'\n';

cout<<“Salary”:<<salary<<'\n'

}

};

void main( )

{ per PI(“REEMA”, 10000), P2(“KRISHNAN”, 20000),

P3(“GEORGE”, 5000);

per *P;

P=P1.GR(P3); P->display( );

P=P2.GR(P3); P->display( );

}

Main Paper 2002

Give the output of the following program : 4

# include <iostream.h>

class Counter

{ Private :

unsigned int count;

Page 7: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

7

Public :

Counter( ) { count=0;}

void inc_count( ) { count++; }

int get_count( ) { return count; }

};

void main( )

{ Counter C1, C2;

Cout<<"\n C1 =" <<C1.get_count( );

Cout<<"\n C2 =" <<C2.get_count( );

C1.inc_count( );

C2.inc_count( );

C2.inc_count( );

Cout<<"\n C1 =" <<C1.get_count( );

Cout<<"\n C2 =" <<C2.get_count( );

}

Main Paper 2003

Consider the following code and answer the questions given below 4

class MNC

{ char Cname[25];

protected :

char Hoffice[25];

public :

MNC( );

char Country( );

void EnterData( );

void DisplayData( );

};

class Branch : public MNC

{ long NOE;

char City[25];

protected :

void Accociation( );

public :

Branch( );

void Add( );

void Show( );

};

class Outlet: public Branch

{ char State[25];

public :

Outlet( );

void Enter( );

void Output( );

};

(i) Which class constructor will be called first at the time of declaration of an object of

class Outlet ?

(ii) How many Bytes does an object belonging to class Outlet require?

(iii) Name the member function(s), which are accessed from the object(s) of class Outlet

(iv) Name the data member(s), which are accessible from the object(s) of class Branch

Page 8: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

8

Main Paper 2004

Consider the following class definition answer the questions that follows 4

class livingbeing

{ char specification[20];

int averageage;

public :

void read( );

void show( );

};

class age : private livingbeing

{ int no_of_organs, no_of_bones;

protected :

int iq_level;

public :

void readape( );

void showape( );

};

class human: public ape

{ char race[20];

char habitation[30];

public :

void readhuman( );

void showhuman( );

};

(i) Name the members which can be accessed from the member function of class human

(ii) Name the members, which can be accessed from the object of class ape

(iii) Name the members, which can be accessed from the object of class human

(iv) What will be size of an object (in bytes) of class human

Class and Object

I Given the following code fragment

int x, b;

class X

{ int x;

float y;

char z;

void init(void)

{ x = y = z = 0; }

public :

int a;

int b;

void sqr(int i)

{ cout<<”Private sum is”<<(x + y)*i<<endl;

cout<<”Public sum is”<<(a + b)*i<<endl;;

} void start(int, float, char);

friend void sum(void);

};

void X :: start(int I, float j, char k)

{ init( );

x = i;

y = j;

z = k;

} void sum (void)

Page 9: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

9

{ cout<<”The sum is”<<x+y+a+b<endl; }

X o1;

void check (void)

{ int a = 10;

cout<<a<<o1.b<<x;

}

What all variables can be accessed by the following functions ?

sqr( ), start( ), sum( ), check( )

II what will be the output of following program

# include <iostream.h>

class X

{ int x;

float y;

public :

void init( )

{ x = y= 0; }

void getval(int i, float j)

{ x = i; y = j; }

void prn( )

{ cout<<”x= “<<x;

cout<<”y= “<<y;

} };

void main( )

{ X O1, O2;

O1.init( );

O2.init( );

O1.getval(15,12.77);

O1.prn( );

O2.prn( );

}

III Consider the following segment

int x = 5;

int y = 7;

class Outer

{ int x, a;

static int s;

class inner // Class inner defination starts

{ int a;

public :

void fun(int i)

{ x = s = y = a = i; }

}; // Inner definition over

inner L1; // Inner Object

void get( int i)

{ x = y = a = s = i; }

}; // Outer definition over

Outer O1;

void main( )

{ O1.L1.fun (6); // Statement 1

O1.get (12); // Statement 2

} What will be the output of ::x, ::y, Outer::x, Outer::a, Outer::s, inner::a after statement1 and

statement 2 of above code

IV what will be the output of following program

# include <iostream.h>

class X

Page 10: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

10

{ int a, b;

public :

int x, y;

void init(int i, int j)

{ a = i; b = j; }

void print(X); // Forward decleration

friend void printit(X); // Forward decleration

};

void X:: print(X O1)

{ cout<<O1.x<<O1.y<<O1.a<<O1.b; }

void printit(X O2)

{ cout<<O2.x<<O2.y; }

void printval(X O3)

{ cout<<O3.y<<O3.b; }

void main( )

{ X Ob;

Ob.init(5,6);

Ob.x = 12;

Ob.y = 23;

Ob.print(Ob);

priniit(Ob);

printval(Ob);

}

Data Structure 1. a) Why arrays are called static data structure ?

b) Write the post-fix and pre-fix expressions for the following expressions.

(A + B) / C

c) Given an Array x[6[16] whose base address is 100. Calculate the location

x[2][5] if each element occupies 4 bytes and array is stored row-wise.

d) Given an Array named A with following elements

3, -5, 1, 3, 7, 0, -15, 3, -7, -8

Write a C++ function to shift all the negative numbers to left and positive

numbers to right (do not sort it) so that the resultant array will look like

:-

5, -15, -7, -8, 3, 1, 3, 7, 0, 3

e) Write a C++ function to delete an element from a circular queue housed

in an array. Pointers FRONT and REAR represent the front and rear of

the queue.

f) Evaluate the following postfix expression with the help of stack and show

the contents of the array after each operation100, 8, 3, *, 50, 2, -, +, -

2. a) What is a pointer

b) What is a Queue

c) Write the sequence of steps required to sort an array NUMARR of 5

elements given below with the help of insertion sort

NUMARR : 22 34 45 22 11

d) ARR1 is a one dimension array of integers arranged in an ascending

order. Write a function in C++ to search for a data sdata from ARR1

with the help of binary search. If the sdata is present in the array the

function should return the position of sdata or –1 otherwise

[1]

[1]

[3]

[4]

[4]

[2]

[1]

[1]

[2]

[4]

[3]

[2]

[2]

Page 11: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

11

e) A two dimension array x[5][4] is stored in row-wise in the memory. The

first element of the array is stored in location 500. Find the memory

location of x[3][2] if each element of array requires 4 memory locations.

f) Write the equivalent Postfix expression for the infix expression given

below (i) A+B-D/X

(i) (X+Y)/(Z*U)-R

g) Consider the following sequence of numbers 1,2,3,4 are supposed to

operated through a stack to produce the following sequence of numbers

2,1,4,3 List the push and pop operations to get the required output

3. a) Suppose A, B, C are arrays of integers of size M, N, M+N respectively.

The numbers in array A and B appear in ascending order. Give the

necessary declaration for array A, B and C in C++. Write a user defined

function in C++ to produce the third array C by merging the arrays A and

B in ascending order. Each element of array A and B should be visited

only once.

b) Each element of an array DATA[20][50] requires 4 bytes of storage. Base

address of DATA is 2000, determine the location of DATA[10][10] when

the array is stored as a) Row major b) Column major.

c) Is it possible to apply binary search for any sorted data ? justify

d) Differentiate between a LIFO and FIFO list.

e) Evaluate the following postfix expression, using a stack and show the

contents of stack after execution of each operation :

True, false, true, false, NOT, OR, true, OR, OR, AND

f) Give the necessary declaration of a linked implemented queue containing

integers. Write a user defined function in C++ to insert an integer in the

queue and delete an integer from the queue.

Main Paper 1998

1. a) Suppose a one dimension array ARR containing integers in ascending

order. Write a user defined function in C++ to search for an integer from

ARR with the help of binary search method. The function should return a

integer 0 to show the absence of the number and integer 1 to show the

presence of the number.. The function should have three parameters as i)

The array ARR ii) The number to be searched iii) number of elements N

b) An Array s[15[10] is stored in memory with each element requiring 4

bytes of storage. If the base address of s is 1000 determine the location of

s[8][9]. When the array is stored as (i) Row Major (ii) Columns Major

c) Write a user defined function in C++ to display the sum of Row elements

of a two dimension array M[5][6] containing integers.

d) Evaluate the following postfix expression with the help of stack and show

the contents of the array after each operation 100, 40,8, +, 20,10, -, +, *

e) Give the necessary declaration of a linked implemented Stack containing

float type numbers. Write a user defined function in C++ to push a float

type number in the stack.

Compartment Paper 1998

[3]

[1]

[1]

[2]

[4]

[4]

[3]

[2]

[2]

[4]

[4]

[3]

Page 12: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

12

1. a) Suppose a one dimension array P containing float in ascending order.

Write a user defined function in C++ to search for an integer from P with

the help of binary search method. The function should return a integer 0

to show the absence of the number and integer 1 to show the presence of

the number.. The function should have three parameters as i) The array P

ii) The number DATA to be searched iii) number of elements N

b) An Array T[15[10] is stored in memory with each element requiring 2

bytes of storage. If the base address of s is 2000 determine the location of

s[7][8]. When the array is stored as (i) Row Major (ii) Columns Major

c) Write a user defined function in C++ to display the sum of Column

Elements of a two dimension array R[7][7] containing integers.

d) Evaluate the following postfix expression with the help of stack and show

The contents of the array after each operation 50, 40,+,18,14, -,4,*,+

e) Give the necessary declaration of a linked implemented Stack containing

Integer type numbers. Write a user defined function in C++ to pop a

Integer type number from this stack.

Main Paper 1999

1. a) Suppose a one dimension array AR containing integers in ascending

order. Write a user defined function in C++ to search for an integer from

AR with the help of Linear search method. The function should return a

integer 0 to show the absence of the number and integer 1 to show the

presence of the number.. The function should have three parameters as i)

The array AR ii) The number to be searched iii) number of elements N

b) An Array A[15[25] is stored in memory with each element requiring 4

bytes of storage. If the base address of s is 1000 determine the location of

A[5][7]. When the array is stored as (i) Row Major (ii) Columns Major

c) Write a user defined function in C++ to display the sum of Row elements

of a two dimension array M[5][6] containing integers.

d) Evaluate the following postfix expression with the help of stack and show

The contents of the array after each operation 5, 6,9,+,80,5, *,-,/

e) Give the necessary declaration of a linked implemented Queue containing

Float type numbers. Write a user defined function in C++ to add a float

type number in the queue.

Compartment Paper 1999

3. a) Suppose a one dimension array AR containing integers is arranged in

ascending order. Write a user defined function in C++ to search for an

integer from AR with the help of binary search method, returning a

integer 0 to show the absence of the number and 1 to show the presence

of the number in the array. Function should have three parameters : (I)

array AR (ii) The number to be searched and (iii) the total number of

elements

b) An Array a[10][20] is stored in the memory, each element requiring 2 bytes

of storage. If the base address of the array is 400 determine the location of

a[8][13]. When the array is stored as (i) Row major (ii) Column Major

c) Write a user defined function in C++ to display the multiplication of row

element of two dimension array a[4][6] containing integers

d) Evaluate the following postfix expression using a stack and show the

[4]

[3]

[2]

[2]

[3]

[4]

[3]

[2]

[2]

[4]

Page 13: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

13

Contents of the stack after execution of each operation: 5,11,-,6,8,+,12,*,/

e) Give the necessary declaration of a linked list implemented queue

Containing float type values. Also write a user defined function in C++ to

delete a float type number from the queue.

Main Paper 2000

3. a) Suppose X, Y, Z are arrays of integers of size M, N and M+N respectively.

The numbers of X and Y appear in descending order. Write a user defined

function in C++ to produce third array Z by merging X and Y in descending

order.

b) Each element of an array DATA[1..10][1..10] requires 8 bytes of storage. If

base address of array DATA is 2000, determine the location of DATA

[4][5], when the array is stored (i) Row wise (ii) Column wise

c) What are the pre conditions for applying binary search algorithm.

d) Differentiate between a LIFO list and FIFO list.

e) Evaluate the postfix expression using a stack and show the contents of

the stack after execution of each operation :

TRUE, FALSE, TRUE, FALSE, NOT, OR, TRUE, OR, OR, AND

f) Give the necessary declaration of a queue containing integers. Write a user

defined function in C++ to delete an integer from the queue. The queue is

to be implemented as a linked structure.

Compartment Paper 2000

3. a) Write a C++ function to sort an array of integers using insertion sort. The

function should have two parameters name of the array and number of

elements in the array

b) P is a one-dimension array of integers arranged in ascending order. Write

a C++ function to efficiently search for a data VAL from P. If VAL is

present in the array then the function returns value 1 and 0 otherwise.

c) X[1..160][1..10] is a two dimension array. The first element of the array is

stored at location 100. Each element of the array occupies 6 bytes. Find

the memory location of X[2][4] when the array is stored as i) Row wise

ii) Column wise

d) Evaluate the following postfix expression showing stack status after

execution of each step : 10, 40, +, 8, 2, +, *, 10, -

e) Give the necessary declaration of linked implemented queue containing

integers. Write a C++ function to remove an element from the queue.

Main Paper 2001

3. a) Given two arrays of integers X and Y of sizes m and n respectively. Write

a function named MERGE( ) which will produce a third array named Z, such

that the following sequence is followed.

(i) All odd numbers of X from left to right are copied into Z from left to right

(ii) All even numbers of X from left to right are copied into Z from right to left

(iii) All odd numbers of Y from left to right are copied into Z from left to right

(iv) All even numbers of Y from left to right are copied into Z from right to

left X, Y and Z are passed as arguments to MERGE( ).

Eg : X is {3, 2, 1, 7, 6, 3} and Y is {9, 3, 5, 6, 2, 8, 10} then the

[2]

[4]

[4]

[2]

[3]

[2]

[4]

[4]

[2]

[3]

Page 14: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

14

resultant array Z is {3, 1, 7, 3, 9, 3, 5, 10, 8, 2, 6, 6, 2}

b) An array X[10][20] is stored in memory with each element requiring 4 bytes

of storage. If the base address of the array is 1000, calculate the location of

X[5][15] when the array X is stored using column major order.

c) Write a user defined function named Lower_half( ) which takes a two

dimensional array A, with size N rows and N columns as argument and prints

the lower half of the array. :

d) Write an algorithm to convert an infix expression to postfix expression.

e) Each node of a STACK contains the following information, in addition to

pointer field :

(i) Pin code of city (ii) Name of city

Give the structure of node for the linked STACK in question. TOP is a pointer

that points to the topmost node of the STACK. Write the following functions :

(i) PUSH( ) -> To push a node in to the STACK, which is allocated dynamically

(b) POP( ) ->To remove a node from the STACK, and release the memory

Stack & Queue

1. 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. Assume the following definition of THENODE for the same.

struct THENODE { char Name[20]; THENODE *Link; };

2. Evaluate the following postfix notation of expression (Show status of stack after execution of each operation ): 4, 10, 5, +, *, 15, 3, /, -

3. Write a function in C++ to Delete an element into a dynamically allocated Queue where each node contains a real number as data. Assume the following definition of MYNODE for the same:

struct MYNODE { float NUM; MYNODE * Link; }; Solution: struct MYNODE { float NUM; MYNODE *Link; };

4. Evaluate the following postfix notation of expression (Show status of stack after execution of each operations): 5, 20, 15, -, *,25, 2, *, + 2

5. 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. struct Book { int BNo ; char BName[20] ;

Page 15: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

15

Book *Next ; } ;

6. Evaluate the following postfix notation of expression : 25 8 3 - / 6 * 10 + (2)

7. Write a function in C++ to delete a node containing customer’s information, from a dynamically allocated Queue of Customers implemented with the help of the following structure:

struct Customer { int CNo ; char CName[20] ; Customer *Link ; } ;

8. Evaluate the following postfix notation of expression : 15 3 2 + / 7 + 2. * .2

9. Write a function in C++ to perform a PUSH operation on a dynamically llocated stack containing real number.

struct Node { float Number ; Node *Link ; } ; class STACK { Node *Top ; public : STACK( ) {Top = NULL ;} void PUSH( ) ; void POP( ) ; ~STACK( ) ; } ;

10. Write the equivalent infix expression for a, b, AND, a, c, AND, OR.

11. Write a function in C++ to perform Insert operation in dynamically allocated Queue containing names of students.

struct NODE { char Name[20]; NODE *Link; };

12. Write the equivalent infix expression for 10, 3, *, 7, 1, --,*, 23, +

13. Write a function in C++ to perform a PUSH operation in a dynamically allocated stack considering the following :

struct Node { int X,Y ; Node *Link ; } ; class STACK { Node *Top ; public : STACK( ) {Top = Null ;} void PUSH( ) ; void POP( ) ;

Page 16: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

16

~STACK( ) ; } ;

14. Evaluate the following postfix notation of expression : 10 20 + 25 15 - * 30 /

15. Write a function in C++ to perform a DELETE operation in a dynamically allocated queue considering the following description :

struct Node { float U, V ; Node *Link ; } ; class QUEUE { Node *Rear, *Front ; public : QUEUE( ) {Rear = NULL ; Front = NULL ;} void INSERT( ) ; void DELETE( ) ; ~ QUEUE( ) ; } ;

16. Evaluate the following postfix notation of expression : 20 10 + 5 2 * - 10 /.

17. Obtain the postfix notation for the following infix notation of expression showing the contents of the stack and postfix expression formed after each step of conversion : (P—Q)/(R*(S—T)+U).

18. Define member functions queins( ) to insert nodes and quedel ( ) to delete nodes of the linked list implemented class queue, where each node has the following structure:

struct node { char name[20] ; int age ; node *Link ; } ; class queue { node *rear, *front ; public : queue( ) { rear = NULL; front = NULL} ; void queins( ) ; void quedel( ) ; } ;

19. Evaluate the following postfix expression using a stack and show the contents of stack after execution of each operation: 20, 45, +, 20, 10, -, 15, +, *.

20. Consider the following portion of a program, which implements passengers Queue for a train. Write the definition of function. Insert (whose prototype is shown below); to insert a new node in the queue with required information.

struct NODE { long Ticketno; char PName[20];//Passengers Name NODE * Next;

Page 17: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

17

}; class Queueoftrain { NODE * Rear, * Front; public : Queueoftrain( ) { Rear = NULL; Front = NULL:} void Insert( ); void Delete( ); ~Queueoftrain( ); } ;

21. Given the following class, char *msg[ ]={“over flow”,”under flow”};

class Stack { int top; //the stack pointer int stk[5]; //the elements void err_rep(int e_num) { cout<<msg[e_enum]; //report error message } public: void init( ) { top=0; } //initialize the stack pointer void push(int); //put new value in stk void pop( ); //get the top value. };

Define pop outside the Stack. In your definition take care of under flow condition. Function pop should invoke err_rep to report under flow.

22. Change the following infix expression into postfix expression. (A+B)*C+D/E-F.

23. Each node of a STACK contains the following information, in addition to pointer field:

(i).Pin code of city (ii).Name of city

Give the structure of node for the linked STACK in question. TOP is a pointer that points to the topmost node of the STACK. Write the following functions:

a) PUSH( ) – To push a node into the STACK, which is allocated dynamically. b) POP( ) – To remove a node from the STACK, and release the memory.

24. Evaluate the following postfix expression using a stack. Show the contents of stack after execution of each operation: 20, 8, 4, /, 2, 3, +, *, -

25. Give necessary declarations for a queue containing float type numbers; also write a user defined function in C++ to insert a float type number in the queue. You should use linked representation of queue.

26. Evaluate the following postfix expression using a stack and show the contents of the stack after execution of each operation 5,11,-,6,8,+,12,*,/

Page 18: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

18

27. Give the necessary declaration of a linked list implemented queue containing float type elements. Also write a user defined function in C++ to delete a float type number from the queue.

struct MYNODE { float NUM; MYNODE * Link; };

28. Evaluate the following postfix expression using a stack and show the contents of stack after execution of each operation: 50, 40, +, 18, 14, -, 4, *, +

29. Give the necessary declaration of a linked implemented stack containing integer type numbers; also write a user defined function in C++ to pop a number from this stack.

30. Write the definition of a member function push() for a class Library in C++ to insert a book information in a dynamically allocated stack of books considering the following code is already written as a part of the program: struct book { int bookid; char bookname[20]; book *next; }; class Library { book *top; public: Library() { top=NULL; } void push(); void pop(); void disp(); ~Library();

};

31. Evaluate the following POSTFIX expression. Show the status of Stack after execution of each operation separately: 45, 45, +, 32, 20, 10, /, -,* 32. Write the definition of functions for the linked implemented queue containing passenger informationas follows: struct NODE { int Ticketno; char PName[20]; NODE * NEXT; }; class Queueofbus { NODE *Rear, *Front; public: Queueofbus() { Rear = NULL;

Page 19: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

19

Front = NULL; }; void Insert(); void Delete(); ~Queueofbus() { cout<<"Object destroyed"; } } 33. 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(); }; 34. Give the necessary declaration of a linked implemented stack containing integer type

numbers also write a user-defined function in C++ to pop a number from this stack.

35. Evaluate the following postfix expression using a stack and show the contents of stack

after execution of each operation :

50,40,+,18, 14,-, *,+

36. Evaluate the following postfix expression using a stack and show the contents of the

stack after execution of each operation.

5,11, 6, 8, +, 12, *, /

37. Give the necessary declaration of a linked list implemented queue containing float type

values. Also write a user-defined function in C++ to delete a float type number from the

queue.

38. Give necessary declarations for a queue containing float type numbers; also write a user-

defined function in C++ to insert a float type number in the queue. You should use linked

representation of queue.

39. Evaluate the following postfix expression using a stack. Show the contents of stack after

execution of each operation.

20, 8, 4,/, 2,3,+,*,-

40. Each node of a STACK contains the following information, in addition to required

pointer field :

i) Roll number of the student

ii) Age of the student

Give the structure of node for the linked stack in question TOP is a pointer which points to

the topmost node of the STACK. Write the following functions.

i) PUSH() - To push a node to the stack which is allocated dynamically

ii) POP() - To remove a node from the stack and release the memory.

Page 20: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

20

41. Convert the expression (True && False) || !(False || True) to postfix expression. Show

the contents of the stack at every step.

42. Define Queue and Stack.

Given the following class :

char *msg[ ] = {"overflow” , ”under flow"}

class Stack

{

int top; // the stack pointer

stk[5]: //the elements

void err_rep(int e_num)

{

cout << msg[e_enum];

}// report error message

public :

void init()

{

top=0;

} // initialize the stack pointer

void push(int); // put new value in stk

void pop(); // get the top value

};

Define push outside the Stack. In your definition take care of overflow condition. Function

push has to invoke err_rep to report over flow.

43. Use a stack to evaluate the following postfix expression and show the content of the

stack after execution of each operation. Don't write any code. Assume as if you are using

push and pop member functions of the stack.

AB - CD + E * + (where A=5, B=3, C=5, D =4, and E=2)

44. Give the necessary declaration of a linked implemented stack containing integer type

numbers also write a user-defined function in C++ to pop a number from this stack.

45. Give the necessary declaration of a linked implemented stack containing integer type

numbers also write a user-defined function in C++ to pop a number from this stack.

46. Evaluate the following postfix expression using a stack and show the contents of stack

after execution of each operation:

50,40,+,18, 14,-, *,+

45. Define member functions queins( ) to insert nodes and quedel( ) to delete nodes of

the linked list implemented class queue, where each node has the following

structure : 4 struct node

{

char name[20];

int age;

node *Link;

};

class queue

{

node * rear,* front;

public:

queue(){rear=NULL;front=NULL};

void queins();

void quedel();

};

46. Obtain the postfix notation for the following infix notation of expression showing

the

Page 21: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

21

contents of the stack and postfix expression formed after each step of conversion :

A * B + (C — D/F)

47. Write a function in C++ to perform a PUSH operation in a dynamically allocated

stack considering the following: 4 struct Node

{

int X, Y;

Node *Link;

};

class STACK

{

Node *Top;

public:

STACK() {Top=NULL;}

void PUSH();

void POP() ;

~STACK();

};

48. Evaluate the following postfix notation of expression: 10 20 + 25 15 - * 30 /

49. 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;

};

50. Evaluate the following postfix notation of expression :

25 8 3 - / 6 * 10 +

File Handling

1. a) A Data file contains only integers values. Which of the following functions :

Read() ii) Write() will you use to read from the file justify your answer.

b) Declare a structure in C++ telerec, containing name (20 characters) and

telephone number. A binary file “Tele.Dat” stores data of type Telerec. Write

function in C++ to do the following

i) To append records in the file

ii) Display the name for a given telephone number. If the telephone number

does not exist then display message “Record not found”.

3. a) Distinguish between serial file and sequential file

b) Write a C++ program that reads a text file and creates another file that is

identical except that every sequence of consecutive blank space is replaced by a

single space.

4. a) Differentiate between Read( ) and Get( ) function of iostream class.

b) Write a C++ program to store student in a binary file. Include function to store

data in the file and to read and display data from the file. Call the function from

the main program.

5. a) Explain the purpose of the following function with reference to the stream class :

Write( ) and Read( )

b) A Data file contains two fields name and telephone number. Write a C++

program to do the following :

i) Add records to a file

Page 22: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

22

ii) Search a Telephone number for a given name

6.a) Write C++ statements to do the following :

i) To open a file named “Results.Dat” for output.

ii) To go to the 50th byte in the file.

b) A school maintains a data file that contains students name, Admission number,

class and section. Write a C++ program to perform the following functions :

i) Add a record

ii) Display all Records

Main Paper 1998 4. a) Write name of two member functions belonging to Fstream class

b) Assuming the class Employee given below. Write function in C++ to

perform following :

i) Write the objects of Employee to a binary file

ii) Read the objects of Employee from binary file and display them

on screen

Compartment Paper 1998 4. a) Differentiate between ifstream class and ofstream class

b) Assuming the class “Stock” Write function in C++ to perform following :

i) Write the objects of Stock to a binary file

ii) Read the object of Stock from binary file and display them on

screen

Main Paper 1999

4. a) Differentiate between getline( ) and get( ) function

b) Assuming the class Drink

Class Drink

{ int quantity;

char name[10];

Public :

void getdata()

{ cin>>quantity; gets(name); }

void showdata()

{ cout<<quantity<<” “<<name<<endl;}

};

Write function in C++ to perform the following :

i) Write the objects of Drink to a binary file

ii) Read the objects of Drink from binary file and display them on

screen

Compartment Paper 1999 4. a) Differentiate between read( ) and write( ) functions

b) Assuming the class floppybox, write functions to perform the following :

(i) Write object of floppybox to a binary file

(ii) Read objects of floppybox from binary file and display it

Main Paper 2000 4. a) Name the stream classes supported by C++ for file input and output.

b) Consider the following class declaration :

Class employee

{ int code;

char name[20];

float salary;

public :

void input( ) { cin>>code>>name>>salary; }

[1]

[4]

[1]

[4]

[1]

[4]

Page 23: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

23

void show( ) { cout<<code<<name<<salary; }

float retsal ( ) { return salary; }

};

Give function declaration to do the following :

(i) Write the object of employee to binary file

(ii) Read the object from binary file and display all the object

On the screen where salary is between 10,000 and Rs. 20,000.

Main Paper 2001

4. a) Distinguish between ios :: out and ios :: app. `

b) Consider the class declaration

Class FLIGHT

{ int flight_no;

char destination [20];

float distance;

public:

void INPUT( ); // To read an object from the keyboard

void Write_File( ); // To write N objects in to a file, where N is

passed as argument

void OUTPUT( ); // To display the file contents on

the monitor

};

Complete the member functions definitions.

Main Paper 2002

4. a) Distinguish between put( ) and Write( ) [1]

b) Write a C++ program, which initializes a string variable to the content. "Time is a [4]

great teacher but unfortunately it kills all the pupils. Berloiz" and outputs the string

one character at a time to the disk file "OUT.TXT". You have to include all the header

file if required.

Main Paper 20033

4. a) Write a user defined function in C++ to read the contents from a test file NOTES.TXT,

[2]

count and display the number of blank spaces present in it

b) Assuming a binary file FUN.DAT is containing object belonging to a class [3]

LAUGHTER (as defined below). Write a user defined function in C++ to add more objects

belonging to class LAUGHTER at the bottom of it

class LAUGHTER

{ int Idno;

char Type[25];

char Desc[255];

public :

void Newentry( )

{ cin>>Idno; gets(type); gets(Desc); }

void Showonscreen( )

{ cout<<Idno<<”:”<<Type<<endl<<Desc<<endl; }

};

Main Paper 20043

Page 24: Worksheet Pointers · 2017. 4. 29. · Pointers 1. Give the output of the following program : # include  # include  void main( ) ... 14. What is

24

4. a) Assume that a text file names FIRST.TXT contains some text written into it, write a

function names vowelword( ), that reads the file FIRST.TXT and creates a new file

names SECOND.TXT, to contain only those words from the file FIRST.TXT which

starts with a lowercase vowel (i.e. with ‘a’, ‘e’, ‘i, ‘o’, ‘u’) For example if the file

FIRST.TXT contains

Carry umbrella and overcoat when it rains

Then the file SECOND.TXT shall contain

umbrella and overcoat it

c) Assuming the class Computer as follows :

Class computer

{ char chiptype[10];

int speed;

public :

void getdetails( )

{ gets(chiptype);

cin>>speed;

}

void shwdetails( )

{ cout<<”Chip”<<chiptype<<” Speed “<<speed;

}

};

Write a function readfile( ) to read all the records present in a already existing binary file

SHIP.DAT and display them on the screen also count the number of records present in it

[3]

[2]