MENU











Friday, 17 January 2014

oops week 23

Week 23 solutions

23a) Write a C++ program to write a function template 
for finding the minimum value contained in any array.

Program:

#include<iostream>
#include<vector>
using namespace std;
template<class T>
T min(T a[],T n) 
{
   T temp=a[0];
   for (int i = 0; i <n; ++i)
   {
    if(temp>=a[i])
   temp=a[i];
 }   
  
 return temp;
}
int main()
{
 int a[20];
 int n=0,res;
 cout<<"Enter the elements of array\n";
 while(cin>>a[n])
 {
  n++;
 }
 res=min(a,n); 
 cout<<"Minimum of all elements is :"<<res<<endl;
}


Output: 

Enter the elements of array
25
7
9
36
55
77
8
2
*
Minimum of all elements is :2

 


23b) Write a template function that takes a vector<T> vt and a vector<U> vu
as arguments and returns the sum of all vt[i]*vt[i]s.

program:

#include<iostream>
#include<vector>
using namespace std;
vector<double>vt,vu;
template<class T>
T value(vector<T>a,vector<T>b)
{
 T sum=0;
 for(int i=0;i<a.size();i++)
 {
  cout<<a[i]<<"\t"<<b[i]<<"\t"<<a[i]*b[i]<<endl;
  sum+=a[i]*b[i];
 }
 return sum;
}
int main()
{
 double p,w;
 double val;
 cout<<"Enter the elements of two vectors sinultaneously (enter character to quit)\n";
 cout<<"\n----------Reading values-------- \n";
 while(cin>>p&&cin>>w) 
 {
  vt.push_back(p);
  vu.push_back(w);
 }
 val=value(vt,vu);
 cout<<"\nSum of products is : ="<<val<<endl;
}

output: 

Enter the elements of two vectors sinultaneously (enter character to quit)

----------Reading values--------
1 2
2 3
3 4
4 5
6 10
*
1       2       2
2       3       6
3       4       12
4       5       20
6       10      60

Sum of products is : =100






23c)  Define a class Int having a single member of class int. Define 
constructors, assignment,and operators +,-,*,/ for it.

program:

#include <iostream>
using namespace std;
 
class Int
{
   public:
   int a;
};
int main()
{
 Int x;
 int b;
 cout<<"Enter an integer :\n";
 cin>>b;
 x.a=b;
 cout<<"\nvalue of a is:"<<x.a;
 x.a+=10;
 cout<<"\nAfter adding 10 value of a is:"<<x.a;
 
}

output: 

Enter an integer :
22

value of a is:22
After adding 10 value of a is:32


 



23d)  Implement vector::operator=() using an allocator for memory management

program:

#include <iostream>
#include <vector>
using namespace std;
int main ()
{
   vector<int> foo (3,0);
   vectorlt;int> bar (5,0);
 
   bar = foo;
   foo = vector<int>();
 
   cout << "Size of foo: " << int(foo.size()) << '\n';
   cout << "Size of bar: " << int(bar.size()) << '\n';
   return 0;
}
output: 

Size of foo: 0
Size of bar: 3



 

Thursday, 16 January 2014

oops week16

Week 16 solutions

16a) Write a C++ program that reads a text file and 
writes out how many characters of each character 
classification are in the file.

File: file.txt
  
 HELLO world

Program:

#include<iostream>
#include<fstream>
#include<ctype.h>
using namespace std;
int main()
{ 
 
        int v[26]={0},index=0; 
        ifstream file("file.txt");
        char c=file.get();
        while(!file.eof())
        {
             if(isalpha(c))
             {
                  c = tolower(c);
                  index = c - 'a';
                  v[index]++;
             }
           c=file.get();
       }
      file.close();
      cout<<"\nFrequency of each character \n";
      cout<<"Character Frequency\n\n";
      for(int i=0;i<26;i++)
      {
          if(v[i]>0)
          cout<<char(97+i)<<"\t  "<<v[i]<<"\n";
      }
      return 0;
}
output:


Frequency of each character
Character Frequency

d         1
e         1
h         1
l         3
o         2
r         1
w         1


  


16b) Write a C++ program draw a rectangle as a rectangle and as a 
polygon.Make the lines of the polygon red and the lines of the 
rectangle blue.


program:

#include<graphics.h>
#include<conio.h>
void poly();
int main()
{
      int gd = DETECT,gm;
      initgraph(&gd, &gm, "C:\\TC\\BGI");
      setbkcolor(WHITE);
      setcolor(BLUE);
      rectangle(100,30,200,80);
      poly();
      getch();
      closegraph();
      return 0;
}
void poly()
{
        setcolor(RED);
        line(250,100,120,150);
        line(250,100,120,300);
        line(120,150,120,300);
}

output:  




16c) Write a C++ program draw a 100-by-30 rectangle and place the 
text “PVPSIT” inside it.
program:

#include<graphics.h>
#include<conio.h>
 
int main()
{
        int gd = DETECT,gm;
        initgraph(&gd, &gm, "C:\\TC\\BGI");
        setbkcolor(WHITE);
        setcolor(GREEN);
        rectangle(100,30,200,80);
        setcolor(RED);
        outtextxy(120,40,"PVPSIT");
        getch();
        closegraph();
        return 0;
}

output: 

Tuesday, 7 January 2014

oops week14

Week 14 solutions

14 b) Write a C++ program that reads a text file and converts its
input to all lower case,producing a new file.

File(input): f1.txt
HELLO
WElcOME TO C++ PROGRAMMING

Program:

#include<iostream>
#include<fstream>
#include<ctype.h>
using namespace std;
int main()
{ 
 
 char ch;
 ifstream in("f1.txt");
 ofstream out("f2.txt");
 cout<<"\nContents of Input file \n";
 ch=in.get();
 while(in.good())
 {
  cout<ch;
  out<<char(tolower(ch));
  ch=in.get();
 }
 in.close();
 out.close();
 ifstream in2("f2.txt");
 cout<<"\nContents of Output file \n";
 cout<<in2.rdbuf();
 in2.close();
 return 0;
 
}

output:  

Contents of Input file
HELLO
WElcOME TO C++ PROGRAMMING
Contents of Output file
hello
welcome to c++ programming
  

oops week15

Week 15 solutions

15a) Write a C++ program that removes all vowels 
from a file.For example, once upon a time!
Becomes nc pn tm!.


File: vowel.txt
  
 once upon a time!

Program:

#include<iostream>
#include<fstream>
using namespace std;
int main()
{ 
 ifstream in("vowel.txt");
 ofstream file("15a_op.txt");
 char c;
 cout<<"\nFile before removing vowels is :\n";
 c=in.get();
 while(in.good())
  {
         cout<<c;
  if(c!='a'&&c!='e'&&c!='i'&&c!='o'&&c!='u')
   file<<c;
  c=in.get();
 }
 in.close();
 file.close();
 cout<<"\nFile after removing vowels is :\n";
 ifstream nh("15a_op.txt");
 cout<<nh.rdbuf();
 nh.close();
 return 0;
 
}
output:

nc pn tm!.
  


15 b)Write a C++ program that replaces punctuation with 
whitespace.For example,“don?t usethe as-if rule”
becomes dont use the asif rule”.
file:punct.txt

“don't usethe as-if rule”
program:

#include<iostream>
#include<fstream>
#include<cctype> or &#include<ctype.h>
using namespace std;
int main()
{
 ifstream in("punct.txt");
 ofstream file("15b_op.txt");
 char c;
 cout<<"\nFile before removing punctuations is :\n";
 c=in.get();
 while(in.good())
  {
         cout<<c;
  if(!ispunct(c))
   file<<c;
  c=in.get();
 }
 in.close();
 file.close();
 cout<<"\nFile after removing vowels is :\n";
 ifstream nh("15b_op.txt");
 cout<<nh.rdbuf();
 nh.close();
 return 0;
  
}

output:  (15b_op.txt)

dont use the asif rule



15 c) Write a C++ program to reverse the order of characters
in a text file.For example,asdfghjkl becomes lkjhgfdsa.


File:.txt

asdfghjkl

program:

#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int main()
{
 vector<char>v;
 ifstream in("15c.txt");
 cout<<"\nInput File :\n";
 char c=in.get();
 while(!in.eof())
  {
  cout<<c;
  v.push_back(c);
  c=in.get();
 }
 in.close();
 ofstream file("15c_op.txt");
 for(int i=v.size()-1;i>=0;i--)
 {
  file<<v[i];
 }
 file.close();
 cout<<"\nFile after reversing the contents :\n";
 ifstream nh("15c_op.txt");
 cout<<nh.rdbuf();
 nh.close();
 return 0;;
}

output:  (15c_op.txt)

lkjhgfdsa.

Monday, 6 January 2014

oops 13a

Week 13a solution

13 a) Write a C++ program that creates a file of data in the form 
of the temperature.Fill the file with at least 50 temperature readings. 
Call this program store_temps.cpp and the file it creates raw_temps.txt.

Program:

#include<iostream> #include<fstream> using namespace std; int main() { double temp; ofstream out("raw_temps.txt"); for(int i=1;i<=50;i++) { if(i<=25) temp=20+.1*i; else temp=20+.25*i; out<<temp<<"\n"; } out.close(); cout<<"Temparature readings in the file are \n"; ifstream in("raw_temps.txt"); cout<<in.rdbuf(); in.close(); return 0; }

Output:

Temparature readings in the file are 20.1 20.2 20.3 20.4 20.5 20.6 20.7 20.8 20.9 21 21.1 21.2 21.3 21.4 21.5 21.6 21.7 21.8 21.9 22 22.1 22.2 22.3 22.4 22.5 26.5 26.75 27 27.25 27.5 27.75 28 28.25 28.5 28.75 29 29.25 29.5 29.75 30 30.25 30.5 30.75 31 31.25 31.5 31.75 32 32.25 32.5

oops 13b

Week 13b solution

13 b) Write a C++ program that accepts two file 
names and produces new file that is the contents 
of the first file followed by the contents of the
second; that is, the program concatenates the two files.

file1.txt:

hello Welcome to c++ programming

file2.txt:

happy coding............

Program:

#include<iostream> #include<fstream> using namespace std; int main() { ifstream file1("file1.txt"); ifstream file2("file2.txt"); ofstream file("new.txt") ; file<<file1.rdbuf()<<"\n"<<file2.rdbuf(); cout<<"After conacatenation of two files \n"; file.close(); ifstream f("new.txt"); cout<<f.rdbuf(); f.close(); return 0; }

Output:

After conacatenation of two files hello Welcome to c++ programming happy coding............

Monday, 23 December 2013

week 12 oops

Week 12 solutions

12a) Write a C++ program to display the contents of a text file.


File: hello.txt
               Hello
**** Welcome to C++ programming ****


Program:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{ 
       ifstream in("hello.txt");
       cout<<in.rdbuf(); //rdbuf() function outputs all the contents of the file
       return 0;
}
output:
               Hello
**** Welcome to C++ programming ****


12 b) Write a C++ program that counts the characters, lines and words in the text file.

file:wish.txt
Hello
Welcome to C++ programming
happy coding

program:

#include<iostream>
#include<fstream>
#include<cctype>
using namespace std;
int main()
{
       int data,count=0,words=0,lines=0;
       ifstream file;
       file.open("wish.txt",ifstream::in);
       char c=file.get();
       cout<<"\nContents of the file\n";
       while(!file.eof())
       {
           cout<<c;
      
           if(isalnum(c))
              count++;
           if(isspace(c))
              words++;
           if(c=='\n')
              lines++;
           c=file.get();
       }
      lines++,words++;
      file.close();
      cout<<"\n\n Characters :"<<count;
      cout<<"\n Lines :"<<lines;
      cout<<"\n Words :"<<words;
      return(0);
}

output:
Hello
Welcome to C++ programming
happy coding

characters:37
Lines:3
words:7



12 c) Write a C++ program that produces the sum of all the numbers in a file of whitespace
separated integers.

File:num.txt
1 2 3 4 5 6 7 8 9 10

program:

#include<iostream>
#include<fstream>
using namespace std;
int main(void)
{
     int sum=0;
     ifstream file("num.txt");
     int x;
     while(!file.eof())
     {
          file>>x;
          cout<<x<<" ";
          sum+=x;
     }
     file.close();
     cout<<"\nSum of all integers is "<<sum;
}

output:
1 2 3 4 5 6 7 8 9 10
Sum of all integers is 55