MENU











Wednesday, 29 January 2014

Friday, 17 January 2014

oops week 20

Week 20 solutions

20a) Write a C++ program to write a function void to_lower(char* s) 
that replaces all uppercase characters. 
Don't use any standard library functions.

Program:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
void to_lower(char* s);
int main()
{
 string st;
 char* s;
 cout<<"Enter any string\n";
 cin>>st;
 s = strdup(st.c_str());
 
 to_lower(s);
 cout<<"\n Original string is :"<<st;
 cout<<"\n modified string is :"<<s; 
 return 0;
}
void to_lower(char* s)
{
 for(int i=0;s[i]!='\0';i++)
  s[i] = (s[i]>='A' && s[i]<='Z')?(s[i]+32):s[i];
}
output:


Enter any string
C++PROGRAMMING

 Original string is :C++PROGRAMMING
 modified string is :c++programming


  


20b) b) Write a C++ program to write a function,char* findx(const char* s, const char* x), 
that finds the first occurrence of the string x in s.

Program:

#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;

char* findx(const char* s,const char* x);

int main()
{
 string st,st1;
 char* a;
  char* b;
 cout<<"Enter any string\n";
 getline(cin,st);
 cout<<"Enter string to search\n";
 cin>>st1;
 a = strdup(st.c_str());
 b = strdup(st1.c_str());
 
 findx(a,b);
 
 return 0;
}
char* findx(const char* s,const char* x)
{
 char* check=new char[10];
 int j=0;
 int count=0;
 for(int i=0;i<=strlen(s);i++)
 {
  if(isspace(s[i])||s[i]=='\0')
  {
   check[j]='\0';
   j=0;
      if(strcmp(check,x)==0)
      {
    cout<<x<<" is present in the string at index :"<<i-strlen(x)<<endl;
    count++;
    
             }
  }
  else
   check[j++]=s[i];
  
 }
 
 if(count==0)
  cout<<x <<" is not present in the string \n";
  
}

output: 
 
Enter any string
Ramu is a naughty boy and he is also intelligent
Enter string to search
is
is is present in the string at index :5
is is present in the string at index :29

C:\Users\Personal\Desktop>



oops week 21

Week 21 solutions

21a) Write a C++ program that reads characters from cin into an array 
that you allocate on thefree store. Read individual characters until 
an asterisk (*) is entered. Do not use a std::string.
Program:

#include<iostream>
using namespace std;
int main()
{
 int max = 10;           // no longer const
 char* a= new char[max]; //  allocated on heap
 int n = 0;

 cout<<"Enter the characters of an array\n";   
 while (cin >> a[n]&&a[n]!='*')    //--- Read into the array
 {
  n++;
 }
 cout<<"Contents in the array are:\n";
 for (int i=0; i<n; i++) 
  cout<<a[i]<<"  ";
}

output:


Enter the characters of an array
abcdefg*

Contents in the array are:
a  b  c  d  e  f  g




21b) Write a C++ program to write a function, char* strdup(const char*) that copies a string
into memory it allocates on the free store. Use the dereference operator * instead.

Program:

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
char* strdup(const char* s)
{
 size_t len = 1+strlen(s);
 char *p = (char*)malloc(len);
 memcpy(p, s, len);
 return p ;
}

int main()
{
 char* n=new char[20];
 char* copy;
 cout<<"enter a string\n";
 cin>>n;
 copy=strdup(n);
        cout<<"Copied string is :  "<<copy<<endl;
 return 0;
}
output: 
 


enter a string
HelloWorld
Copied string is :  HelloWorld




oops week 22

Week 22 solutions

22a) a) Write a C++ program to write a function 
char* findx(const char* s, const char* x) that
find the first occurrence of the string x in s.
 Use dereference operator * instead.

   
   Same as 20a

  


22b) Write a C++ program to write a function 
string cat_dot(const string& s1, const string&s2), 
that concatenates two strings with a dot in between.

program:

#include<iostream>
#include<string.h>
using namespace std;

string cat_dot(const string&s1,const string&s2);
int main()
{
 string s1,s2,s3;
 cout<<"Enter string 1\n";
 cin>>s1;
 cout<<"Enter string 2\n";
 cin>>s2;
 s3=cat_dot(s1,s2);
 cout<<"After concatenation:\n";
 cout<<s3;
 
}
string cat_dot(const string&s1,const string&s2)
{
 string a;
 a=s1+"."+s2;
 return a;
}

output: 


Enter string 1
C++
Enter string 2
Programming
After concatenation:
C++.Programming





22c) Write a template function that adds a vector of elements of an 
object of any type to which elements can be added.
program:

#include<iostream>
#include<vector>
using namespace std;
template<class T>
T sum(vector<T>v) 
{
   T result;
   for (int i = 0; i < v.size(); ++i)
      result+=v[i];
  
 return result;
}
int main()
{
 
 vector<int>v;
 int x,res;
 cout<<"Enter the elements of vector\n";
 while(cin>>x)
 {
  v.push_back(x);
 }
 res=sum(v); 
 cout<<"sum of all elements is :"<<res<<endl;
}

output: 


Enter the elements of vector
1 2 3 4 5 6 7 8 9 10 11 11 *
sum of all elements is :77

 

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