CBSE Guess > Papers > Important Questions > Class XII > 2012 > Computer Science > Computer Science Mr. Pradumna Singh
Computer Science- CBSE CLASS XII
Q. 13. Write functions to perform PUSH & POP operations in a dynamically allocated stack containing the objects of the following structure: For - 4 Marks
struct NODE
{ char name[30];
float fees;
NODE *next; };
ANS.:
NODE *top=NULL; //declaring global pointer & initializing it with NULL
void PUSH( )
{ NODE *p=new NODE; //creating new dynamic list to go on to stack
cout<<”\nEnter Name : “;
gets(p->name);
cout<<”\nEnter Fees : “;
cin>>p->fees;
p->next=NULL:
if(top= = NULL)
top=p;
else
{ p->next=top;
top=p }
cout<<”\nList inserted on the top of stack successfully…”;
getch( );
}
void POP( )
{ if(top= = NULL)
cout<<”\nStack Empty”;
else
{ NODE *temp=top;
top=top->next;
delete temp;
cout<<”\nList deleted from top of stack successfully…”;
getch( );
}
}
Q. 14. Consider the class: For - 2 Marks
class QUEUE
{
private:
int data[20],front,rear;
public:
QUEUE( )
{ front=rear=-1; }
void INSQ(int d); //to insert an element into queue
void DELQ( ); //to delete an element from the queue
void PRINTQ( ); //to print the current status of queue
};
Complete the definition of function DELQ( ) of above class.
Ans.:
void QUEUE::DELQ( )
{
if(front<0)
cout<<”\nQueue Empty”;
else
{
cout<<”\n”<<data[front]<<” has been removed from queue”;
for(int i=front;i<rear;i++)
data[i]=data[i+1];
rear--;
if(rear<0)
front=-1; }
}
Q. 15. Evaluate the following postfix notation of expression: For - 2 Marks
30, 6, 4, +, /, 14, +, 4, *
SOL. (by tabular method):
Steps INPUT ACTION STACK
1 30 Push #30
2 6 Push #30,6
3 4 Push #30,6,4
4 + Pop 4,6 &
Push 6+4=10 #30,10
5 / Pop 10,30 &
Push 30/10=3 #3
6 14 Push #3,14
7 + Pop 14,3 &
Push 3+14=17 #17
8 4 Push # 17,4
9 * Pop 4,17 &
Push 17*4=68 #68
Ans. 68
Q .16. Observe the program segment given below carefully and answer the question that follows : For - 1 Marks
class school
{ private :
char name[25];
int numstu;
public:
void inschool( );
void outschool( );
int retnumstu( )
{ return numstu; }
};
void modify(school A)
{ fstream INOUT;
INOUT.open(“school.dat”,ios::binary|ios::in|ios::ate);
school B;
int recread=0, found=0;
while(!found && INOUT.read((char*)&B,sizeof(B))
{ recread++;
if(A.retnumstu( )= = B.retnumstu( ))
{
____________________________//missing statement
INOUT.write((char*)&A,sizeof(A));
Found=1;
}
else
INOUT.write((char*)&B,sizeof(B));
}
if(!found)
cout<<”\nRecord for modification does not exist”; INOUT.close( );
� If the function modify( ) is supposed to modify a record in file school.dat with the values of school A passed to its argument, write the appropriate statement for missing statement using seekp( ) or seekg( ) , whichever needed, in the above code that would write the modified record at its proper place.
Ans. :
INOUT.seekp(-1*sizeof(school),ios::cur);
OR
INOUT.seekg(-1*sizeof(school),ios::cur);
Q. 17. Write a function to count the number of vowels stored in a text file “STRINGS.TXT”. For - 2 Marks
Ans.:
void countvowel( )
{ int c=0;
char ch;
ifstream fin(“STRINGS.TXT”);
while(!fin.eof( ))
{
fin.get(ch); OR fin>>ch;
if(!fin)
break;
switch(ch)
{
case ‘A’:
case ‘a’:
case ‘E’:
case ‘e’:
case ‘I’:
case ‘i’:
case ‘O’:
case ‘o’:
case ‘U’:
case ‘u’:c++;
}
}
cout<<”\nTotal vowels in the data file is “<<c;fin.close( ); }
Q. 18. Write a function to delete a record on the given model number for a TV from the binary file “TV.DAT” containing the objects of TV (as defined below) : For - 4 Marks
class TV
{
long model;
float size;
char brand[30],comp[30];
public:
long retmodel( )
{ return model; }
void Input( ) {cin>>model>>size; gets(brand); gets(comp); }
void Output( ) { cout<<model<<size<<brand<<comp<<endl; } };
Ans.:
void DELREC(long m)
{ TV ob;
ifstream fin("TV.DAT",ios::binary);
ofstream fout("temp.dat",ios::app|ios::binary);
int flag=0;
while(!fin.eof()) //for searching record
{ fin.read((char*)&ob,sizeof(TV));
if(!fin)
break;
if(ob.retmodel( )= =m)
{ flag=1;
break; }
}
if(!flag)
{ cout<<"\nRecord does not exist";
getch(); }
else //for deleting record
{ fin.seekg(0);
while(!fin.eof())
{ fin.read((char*)&ob,sizeof(TV));
if(!fin)
break;
if(ob.retmodel( )= =m)
fout.write((char*)&ob,sizeof(ob)); }
remove("TV.DAT");
rename("temp.dat","TV.DAT");
cout<<"\nRECORD DELETED SUCCESSFULLY........";
getch(); }
fin.close();
fout.close();}
Prepared By: Mr. Pradumna Singh
mail to: [email protected] |