SOLUTION-XII B COMPUTER SCIENCE-2ND MONTHLY-AUGUST 2013

KENDRIYA VIDYALAYA NO. 1 , NAUSENABAUGH, VISAKHAPATNAM
Monthly Test (August 2013) Subject : Computer Science
Class :  XII      Section : B      Duration: 90 Min.           Max Marks :50
Attempt all questions.
Q1. Write a function in C++ to print the count of the word “the” (ignore case while matching) as an independent word in a text file STORY.TXT.     (5Marks)
For example, if the content of the file STORY.TXT is
There was a monkey in the park.
The monkey was very naughty.
The name of the park was VUDA Park.
Then the output of the program should be 4.

ANSEWER:::A COMPLETE PROGRAM:::::
#include<fstream.h>
#include<stdlib.h>
#include<stdio.h>
void main()
 {                 
    ofstream fout;
    fout.open("text.txt",ios::out);
    char a[500];
    cout<<"\n  Enter content of file:";
    gets(a);
    fout<<a;
    fout.close();
    ifstream fin("text.txt");
    char word[50];
    int count=0;
    if(fin)
    {
       while(!fin.eof())
       {
          fin>>word;
                    if(strcmpi(word, "the") == 0)  //i--ignore case while comparing
             count++;
       }
       fin.close();   /* close the input file */
    }
    cout<<”\n No of occurrences :  “<<count;
    system("PAUSE"); //Press any key to continue...
   }
*******************************************EXACT ANSWER********
int count_word_occurence(char filename[], char pattern[])
 {
    ifstream fin(filename);
    char word[50];
    int count=0;
    if(fin)
    {
       while(!fin.eof())
       {
          fin>>word;
          if(strcmpi(word,pattern) == 0)
          count++;
       }
       fin.close();
    }
    return count;

   }

Q2. Assume a text file “Test.txt” is already created. Using this file, write a function to create three files “LOWER.TXT” which contains all the lowercase vowels and “UPPER.TXT” which contains all the uppercase vowels and “DIGIT.TXT” which contains all digits.       ********************COMPLETE PROGRAM*************************
#include<fstream.h>
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
  void divide_content(char filename[]);
void main()
 {
 divide_content("test.txt");
 system("PAUSE"); //Press any key to continue...
 }

 void divide_content(char filename[])
 {
    ifstream fin(filename);
    ofstream Lfile("LOWER.TXT");
    ofstream Ufile("UPPER.TXT");
    ofstream Dfile("DIGIT.TXT");
    char c;
       if(fin)
    {
       while(!fin.eof())
       {
          fin.get(c);
          if(islower(c))    Lfile.put(c);
          if(isupper(c))  Ufile.put(c);
          if(isdigit(c))   Dfile.put(c);
                 }
       }
       fin.close();
       Lfile.close();
       Ufile.close();
       Dfile.close();

   }
****************** EXACT ANSWER********************
void divide_content()
 {
    ifstream fin("Test.txt"));
    ofstream Lfile("LOWER.TXT");
    ofstream Ufile("UPPER.TXT");
    ofstream Dfile("DIGIT.TXT");
    char c;
       if(fin)
    {
       while(!fin.eof())
       {
          fin.get(c);
          if(islower(c))    Lfile.put(c);
          if(isupper(c))  Ufile.put(c);
          if(isdigit(c))   Dfile.put(c);
                 }
       }
       fin.close();
       Lfile.close();
       Ufile.close();
       Dfile.close();

   }

Q3. Write a function in a C++ to count the average word size in a text file “BOOK.txt” (5M)
*******************A COMPLETE PROG********************************
#include<fstream.h>
#include<stdlib.h>
#include<stdio.h>
void main()
 {         
    ofstream fout;
    fout.open("BOOK.txt",ios::out);
    char a[500];
    cout<<"\n  Enter content of file:";
    gets(a);
    fout<<a;
    fout.close();
    ifstream fin("BOOK.txt");
    char word[50];
    int size=0,count=0;
    if(fin)
    {
       while(!fin.eof())
       {
          fin>>word;
          if(strlen(word)) // to avoid spaces used after the end of last word
          {                 //  "the  the abc    " has 3 words ...not 4 words
             size=size+strlen(word);
             count++;
           } 
       }
       fin.close();   /* close the input file */
    }
    cout<<"\n No of words :  "<<count;
    cout<<"\n Total size  of all words :  "<<size;
    cout<<"\n Average size of word :  "<< (float)size/count;
    // (float)size/count  OR  (float)size/(float)count  OR (float)size/(float)count
    system("PAUSE"); //Press any key to continue...
   }
*********************Exact  Answer**********************
void averageWordSize()
    {
    ifstream fin("BOOK.txt");
    char word[50];
    int size=0,count=0;
    if(fin)
    {
       while(!fin.eof())
       {
          fin>>word;
          if(strlen(word)) // to avoid spaces used after the end of last word
          {                 //  "the  the abc    " has 3 words ...not 4 words
             size=size+strlen(word);
             count++;
           }
       }
       fin.close();   /* close the input file */
    }
    cout<<"\n No of words :  "<<count;
    cout<<"\n Total size  of all words :  "<<size;
    cout<<"\n Average size of word :  "<< (float)size/count;
      }

Q4. Write a function in C++ to add new objects at the bottom of a binary file STUDENT.DAT”,  assuming the binary file is containing the objects of the following class.                    (5M)
class STUD {
int Rno;
char Name[20];
public: void Enter() { cin>>Rno;gets(Name);}
void Display(){cout<<Rno<<Name<<endl;}   };
*************************************EXACT ANSWER******
Void addATend()
  {   STUD s;
       ofstream  f("student.dat",ios::binary|ios::ate);  //u can use ios::app also
       s.enter();
      f.write((char*)&s,sizeof(s));
       f2.close();
}

Q5. Observe the program segment carefully and answer the question that follows: (2M)
class item {
int item_no;
char item_name[20];
public:
void enterDetail( );
void showDetail( );
int getItem_no( ){ return item_no;} };
void modify(item x, int y ) {
fstream File;
File.open( “item.dat”, ios::binary | ios::in | ios::out) ;
item i;
int recordsRead = 0, found = 0;
while(!found && File.read((char*) &i , sizeof (i))){
recordsRead++;
if(i . getItem_no( ) = = y ){
_________________________//Missing statement
File.write((char*) &x , sizeof (x));
found = 1;}}
if(! found)
cout<<”Record for modification does not exist” ;
File.close() ; }
If the function modify( ) is supposed to modify a record in the file “ item.dat “, which item_no is y, with the values of item x passed as argument, write the appropriate statement for the missing statement using seekp( ) or seekg( ), whichever is needed, in the above code that would write the modified record at its proper place.
****************ANSWER****************
File.seekp(sizeof(item)*(recordsRead-1));  OR File.seekp(File.tellg( ) – sizeof(item));

Q6. Observe the program segment carefully and answer the question that follows: (2M)
class item {int item_no;
char item_name[20];
public:
void enterDetails( );
void showDetail( );
int getItem_no( ){ return item_no;}};
void modify(item x )
{fstream File;
File.open( “item.dat”, _______________ ) ; //parameter missing
item i;
while(File .read((char*) & i , sizeof (i)))
{if(x . getItem_no( ) = = i . getItem_no( ))
{File.seekp(File.tellg( ) – sizeof(i));
File.write((char*) &x , sizeof (x));
} else
File.write((char*) &i , sizeof (i));
}  File.close() ;
}  If the function modify( ) modifies a record in the file “ item.dat “ with the values
of item x passed as argument, write the appropriate parameter for the missing
parameter in the above code, so as to modify record at its proper place.
************************ANSWER***************
File.open( “item.dat”, ios::in| ios::out | ios::binary);

Q7.  Observe the program segment carefully and answer the question that follows:  (5M)
class item {int item_no;
char item_name[20];
public:
void enterDetail( );
void showDetail( );
int getItem_no( ){ return item_no;}};
void modify(item x ){
fstream File;
File.open( “item.dat”, ios::binary|ios::in|ios::out ) ;
item i;
while(File .read((char*) & i , sizeof (i)))//Statement 1
{ if(x . getItem_no( ) = = i . getItem_no( )){File.seekp(File.tellg( ) – sizeof(i));
File.write((char*) &x , sizeof (x));
}} File. close () ;}
If the function modify ( ) modifies a record in the file “ item.dat” with the values of item x passed as argument, rewrite statement 1 in the above code using eof( ), so as to modify record at its proper place.
*****************ANSWER****************************
while(!File .eof())) {read((char*) & i , sizeof (i)));…………………………………….


Q8.   Explain the following:  get pointer, put pointer, seekg(),seekp(),tellp() ,tellg() (6 Marks)
Q9.   What is difference between Text File and Binary File ?                                  (5 Marks)
Q10. Discuss various  File opening modes for Text and Binary Files separately. (5Marks)
Q11. Write a function that concatenates content of one existing Text file with the content of another Text file. Don’t use any temporary third file.                                              (5Marks)
***************Exact answer****************************
void concat2Files()
 {
    ofstream fout;
    fout.open("FIRST.txt",ios::app);
    ifstream fin;
    fin.open("SECOND.txt");

       while(!fin.eof())
       {  char c;
          fin.get(c);
          fout.put(c);
       }
       fin.close();
       fout.close();
  }

****************************END*************************************  

Popular posts from this blog

XII- CS-- SOLVED SQL queries (SQL PRACTICAL ASSIGNMENS)

SQL--HOME ASSIGNMENTS(XII Class)

Python-MySQL Connectivity