- Click below to download Unit 4 programs in pdf file: -
Unit 4
/* 1 Write a program to create a function template for finding maximum value contained in an array. */
#include<iostream>
using namespace std;
template<typename T>
T max(T a[], int s)
{
T large=a[0];
for(int i=0;i<s;i++)
{
if(a[i]>large)
{
large=a[i];
}
}
return large;
}
int main()
{
int n;
cout<<"How many size of array you want : "<<endl;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cout<<"enter the "<<i+1<<" value:";
cin>>a[i];
}
cout<<endl;
cout<<"maximum value in array is:"<<max(a,n);
}
/* 2 Write a program to create a class template for the ‘Array’ class */
#include<iostream>
using namespace std;
template <class T>
class arry
{
T *arr;
public:
arry(T a[],T s)
{
for(int i=0;i<s;i++)
{
arr[i]=a[i];
}
}
void print(T s)
{
cout<<"Your array is : ";
for(int i=0;i<s;i++)
{
cout<<arr[i]<<" ";
}
}
};
int main()
{
int n;
cout<<"How many size of array you want : ";
cin>>n;
int a1[n];
for(int i=0;i<n;i++)
{
cout<<"enter the "<<i+1<<" value:";
cin>>a1[i];
}
arry <int> a(a1,n);
cout<<endl;
a.print(n);
}
/* 3 Create a template for the bubble sort function. */
#include<iostream>
using namespace std;
template<class T>
T bubble(T a[], int s)
{
for(int i=0;i<s;i++)
{
for(int j=0;j<s-i;j++)
{
if(a[j]>a[j+1])
{
int tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
for(int i=0;i<s;i++)
{
cout<<a[i]<<" ";
}
}
int main()
{
int n;
cout<<"How many size of array you want : ";
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cout<<"enter the "<<i+1<<" value:";
cin>>a[i];
}
cout<<endl;
cout<<"\n----Array after sorting----"<<endl;
bubble(a,n);
}
/* 4 Write a program to create a function template for swapping the two value. */
#include<iostream>
using namespace std;
template <class T>
T swap(T a,T b)
{
cout<<"----Before swapping----"<<endl;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
T tmp;
tmp=a;
a=b;
b=tmp;
cout<<endl;
cout<<"----After swapping----"<<endl;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
}
int main()
{
swap(45,55);
}
/* 5 Write a program to illustrate the use of put(), get() and getline() functions for Text mode Input/Output. */
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char a='B',b,c[15];
ofstream fout("file1.txt");
fout.put(a);
ifstream fin("file2.txt");
fin.get(b);
cout<<"content of get is :"<<b<<endl;
fin.close();
ifstream file("file2.txt");
file.getline(c,15);
cout<<"content of getline :"<<c<<endl;
}
/* 6 Write a program to read character, integer and string from keyboard and write it in “data.txt” file and read from file in text mode. */
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int i,i1;
char ch,ch1;
char s[5],s1[5];
cout<<"----Write the data into file----"<<endl;
cout<<"Enter your roll no : "<<endl;
cin>>i;
cout<<"Enter your grade : "<<endl;
cin>>ch;
cout<<"Enter your first name : "<<endl;
cin>>s;
ofstream fo("data.txt");
fo<<i<<endl;
fo<<ch<<endl;
fo<<s<<endl;
fo.close();
cout<<endl;
cout<<"----Read from the data from file in text mode----"<<endl;
ifstream fi("data.txt");
fi>>i1>>ch1>>s1;
cout<<i1<<endl;
cout<<ch1<<endl;
cout<<s1<<endl;
fi.close();
}
/* 7 Write a program to read your name and roll number from keyboard and write it in “mydata.txt “ file and read from file in text mode. */
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int i,i1;
char c[10],c1[10];
cout<<"----Read the data form the keyboard----"<<endl;
cout<<"Enter your roll no:";
cin>>i;
cout<<"Enter your name:";
cin>>c;
ofstream fo("mydata.txt");
fo<<i<<endl;
fo<<c<<endl;
fo.close();
cout<<endl;
cout<<"----Read the data from file----"<<endl;
ifstream fi("mydata.txt");
fi>>i1>>c1;
fi.close();
cout<<"roll no:"<<i1<<endl;
cout<<"name:"<<c1<<endl;
}
/* 8 Write a program to read product name and product price from keyboard and write it in “product.txt” file and read from file in text mode. */
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int i,i1;
char c[10],c1[10];
cout<<"----Read the data form the keyboard----"<<endl;
cout<<"Enter product name:";
cin>>c;
cout<<"Enter product price:";
cin>>i;
ofstream fo("product.txt");
fo<<c<<endl;
fo<<i<<endl;
fo.close();
cout<<endl;
cout<<"----Read the data from file----"<<endl;
ifstream fi("product.txt");
fi>>c1>>i1;
cout<<"Product name:"<<c1<<endl;
cout<<"Product price:"<<i1<<endl;
fi.close();
}
/* 9 Write down a program to create a file temp.txt, write into the specific file than read the same data from the file */
#include<iostream>
#include<fstream>
#define MAX 50
using namespace std;
int main()
{
int n,arr[MAX],arr1[MAX];
cout<<"How many numbers you want to enter : ";
cin>>n;
ofstream fo("tmp.txt");
for(int i=0;i<n;i++)
{
cout<<"enter the "<<i+1<<" number:";
cin>>arr[i];
fo<<arr[i]<<endl;
}
fo.close();
cout<<"----Read the data form the file----"<<endl;
ifstream fi("tmp.txt");
for(int i=0;i<n;i++)
{
fi>>arr1[i];
cout<<arr[i]<<endl;
}
fi.close();
}
/* 10 Write a program to create num.txt file which stores number. Find max value from a file nums.txt and print it on standard output device. */
0 Comments