0% found this document useful (0 votes)
11 views10 pages

CPP Final Module Lab Answer

The document contains multiple C++ programming tasks including the implementation of template functions for sorting and reversing data, operator overloading in a string class, deleting duplicate elements in an array, copying file data to multiple destination files, sorting words in a file, and reversing words of length <= 4. Each task is accompanied by code snippets demonstrating the required functionality. The document serves as an instructional guide for various C++ programming concepts and techniques.

Uploaded by

Neeraj M K
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

CPP Final Module Lab Answer

The document contains multiple C++ programming tasks including the implementation of template functions for sorting and reversing data, operator overloading in a string class, deleting duplicate elements in an array, copying file data to multiple destination files, sorting words in a file, and reversing words of length <= 4. Each task is accompanied by code snippets demonstrating the required functionality. The document serves as an instructional guide for various C++ programming concepts and techniques.

Uploaded by

Neeraj M K
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

WAP A CPP PROGRAM TO DESIGN TEMPLATE FUNCTION

a) SORT_DATA()templated function[ascending order]


b)REVERSE_DATA() templated functions[not just reverse printing ,reverse the content]

#include<iostream>
using namespace std;
template<class type>
class ARRAY{
type *s=0;
int i;
public:
ARRAY(int a){
s=new type[a];
if(s==0)
cout << "memory not allocated" << endl;
}
void set_data(int n)
{
cout<<"enter the "<<typeid(*s).name()<<" elements"<<endl;
for(int i=0;i<n;i++)
cin>>s[i];
}
void get_data(int n)
{
cout<<"-------------"<<endl;
for(i=0;i<n;i++)
cout<<s[i]<<" ";
cout<<endl;
}
void sort_data(){
int i,j;
type temp;
for(i=0;s[i];i++)
{
for(j=i+1;s[j];j++)
{
if(s[i]>s[j]){
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
}
void reverse_data(int n){
int i;
type temp;
for(i=0,n=n-1;i<n;i++,n--){
temp=s[i];
s[i]=s[n];
s[n]=temp;
}
}
};
int main(){
int n;
cout<<"enter the number of elements"<<endl;
cin>>n;

ARRAY<float> obj1(n);
obj1.set_data(n);
obj1.sort_data();
cout<<"after sorting the data"<<endl;
obj1.get_data(n);
obj1.reverse_data(n);
cout<<"after reversing the data"<<endl;
obj1.get_data(n);
}

2a)DESING A CLASS CALLED STRING,WITH THE DATA MEMEBERS AS CHAR *AND OVERLOAD
FOLLOWING OPERATORS
a) []SUBSCRIPT OPERATOR
b) >> EXTERTION OPERATOR
c) << INSERTION OPERATOR
d) + OPERATOR

#include<iostream>
#include<cstring>
using namespace std;
class abc{
char *p;
public:

/* void set_data()
{
int i;
p=new char[100];
cout<<"enter the string"<<endl;
cin>>p;
}*/
abc operator+(abc t2){
cout<<"operator+ overloaded"<<endl;
abc temp;
temp.p=new char [100];
strcpy(temp.p,this->p);
strcat(temp.p,t2.p);
return temp;
}
char& operator[](int i){
cout<<"[]operator overloaded"<<endl;
return p[i];
}
friend int main();
friend istream& operator>>(istream&in,abc &t);
friend ostream& operator<<(ostream &out,abc t);

};
istream& operator>>(istream &in,abc &t){
t.p=new char[100];
cout<<">>operator overloaded"<<endl;
in>>t.p;
return in;
}

ostream& operator<<(ostream &out,abc t){


cout<<"operator << overloaded"<<endl;
out<<t.p<<endl;
return out;
}
int main(){
int i;
abc obj1,obj2,obj3;
cin>>obj1;
cin>>obj2;
//obj1.set_data();
//cout<<"----------------"<<endl;
//obj2.set_data();
obj3=obj1+obj2; //addition operator overloaded
cout<<obj3<<endl;
cout<<obj3[5]<<endl; //subscript operator overloaded

3)DESING A CLASS ARRAY DELETE THE DUPLICATE ELEMENTS

#include<iostream>
using namespace std;
class ARRAY{
int *p;
public:
ARRAY(int n){
p=new int[n];
cout<<"constructor called"<<endl;
}
void set_data(int n){
int i=0;
cout<<"enter the elements"<<endl;
for(i=0;i<n;i++)
cin>>p[i];
}
void get_data(int n){
int i=0;
for(i=0;i<n;i++)
cout<<"p["<<i<<"]="<<p[i]<<endl;

}
~ARRAY(){
cout<<"destructor called"<<endl;
}

void delete_array(int &n){

int i,j,k;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
if(p[i]==p[j])
{
for(k=j;k<n;k++)
p[k]=p[k+1];
n--;
j--;
}
}
}
};
int main(){
int n;
ARRAY obj1(n);
cout<<"enter the number of elements"<<endl;
cin>>n;
obj1.set_data(n);
obj1.delete_array(n);
obj1.get_data(n);

4) WAP IN C++ TO COPY SOURCE FILE DATA INTO N NUMBER OF DESTINATION FILES.
destination files already present ask the user to overwrite or not?
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc,char **argv)
{
char ch;
int n;
n=argc;
ifstream fin(argv[1]);
try{
if([Link]())
{
throw "file is not present";
}
}
catch(const char *p)
{
cout<<p<<endl;
return 0;
}
for(int i=2;i<n;i++)
{
ofstream fout;
int op;
cout<<"1)overwrite 2)not"<<endl;
cin>>op;
if(op==1)
{
[Link](argv[i]);
if(fout.is_open()){
while((ch=[Link]())!=-1)
fout<<ch;
cout<<"data written to "<<argv[i]<<endl;
}
else
cout<<"error while opening"<<argv[i]<<endl;
}
else{
cout<<"no data written to this file"<<argv[i]<<endl;
}
[Link]();
[Link](0,ios::beg);
}
[Link]();

5a) WRITE A C++ PRPOGRAM TP READ THE DATA FROM THE FILE WORD BY WORD SORT IT IN
DESCENDING ORDER AND STORE THE RESULT IN THE SAME FILE
5b) WRITE A OWN WC COMMAND (WORD COUNT ,LINES COUNT,CHARACTER COUNT)

#include<iostream>
#include<fstream>
using namespace std;
int main(int argc,char **argv){
fstream fio;
[Link](argv[1]);
try{
if([Link]())
throw "file not present";

}
catch(const char *p){

cout<<p<<endl;
return 0;
}
string s;
int n,i,j;
char t;
while(fio>>s)
{
cout<<s<<endl;
n=[Link]();
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){

if(s[j]<s[j+1]){
t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}

}
}
[Link](-n,ios::cur);
fio<<s;

}
[Link]();
[Link](0,ios::beg);
int c=0,w=0,l=0;
while((t=[Link]())!=-1)
{
if(t!=’ ’)
c++;

}
[Link]();
[Link](0,ios::beg);
while(fio>>s){
w++;
}

[Link]();
[Link](0,ios::beg);
while(getline(fio,s)){

l++;
}
cout<<" count:"<<c<<" word count:"<<w<<" lines:"<<l<<endl;

6) DESIGN A CLASS CALLES ARRAY WITH DATA MEMBERS AS INT A[5].


IMPLEMENT FOLLOWING FUNCTIONS USING CLASS TEMPLATE
a) set_data()
b)reverse_data()
c) sort_data()
d)save_data()

#include<iostream>
#include<fstream>
using namespace std;
template<class type>
class Array
{
type a[5];
public:
Array()
{
cout<<"constructor"<<endl;
}
void set_data(type arr[5])
{
cout<<"set data"<<endl;
for(int i=0;i<5;i++)
a[i]=arr[i];
//cin>>a[i];
}
void reverse_data()
{
cout<<"reverse the data"<<endl;
int i,j,ele;
ele=sizeof(a)/sizeof(a[0]);
for(i=0,j=ele-1;i<j;i++,j--)
{
type t;
t=a[i];
a[i]=a[j];
a[j]=t;
}
/*for(j=ele-1;j>=0;j--)
cout<<a[j]<<endl;*/
}
void sort_data()
{
cout<<"sorting the data"<<endl;
int i,j,ele;
ele=sizeof(a)/sizeof(a[0]);
for(i=0;i<ele-1;i++)
{
for(j=0;j<ele-1-i;j++)
{
if(a[j]>a[j+1])
{
type t;
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
void save_data()
{
int ele;
cout<<"storing into file"<<endl;
ofstream fout("data");
ele=sizeof(a)/sizeof(a[0]);
for(int i=0;i<ele;i++)
fout<<a[i]<<" ";
cout<<"stored in a file"<<endl;
[Link]();
}
};
int main()
{
Array <int> obj;
int arr[5]={1,6,3,8,22};
obj.set_data(arr);
obj.save_data();
obj.sort_data();
obj.save_data();
obj.reverse_data();
obj.save_data();
}

7) WRITE A C++ PROGRAM TO REVERSE THE WORDS WHICH IS HAVING THE LENTGH <=4 AND
STORE THE RESULT INTO THE SAME FILE AND PRINT THE REVERSING WORD COUNT

#include<iostream>
#include<fstream>
using namespace std;
int main(int argc,char **argv){

fstream fio(argv[1]);
try{
if([Link]()){
throw "file not present";
}
}
catch(const char *p){
cout<<p<<endl;
return 0;
}
int i,n,j,c=0;
string s;
char t;
while(fio>>s){
n=[Link]();
if(n<=4){
c++;
for(i=0,j=n-1;i<j;i++,j--){

t=s[i];
s[i]=s[j];
s[j]=t;
}
[Link](-n,ios::cur);
fio<<s;
}

cout<<"count:"<<c<<endl;
}

You might also like