27.8.15

Define a function template for finding the minimum value contained in an array

Define a function template for finding the minimum value contained in an array. Write main( ) function to find the minimum value of integer array and minimum value of floating point numbers in an array.

#include<iostream.h>

#include<conio.h> template<class T>

T minimum(T a[],int size)

{

T min=a[0];

for(int i=0;i<size;i++)

{

if(a[i]<min)

min=a[i];

}

return min;

}

int main()

{

int a[10],size,i,min1; float b[10],min2; clrscr();

cout<<"enter the size value:\n"; cin>>size;
cout<<"enter the integer aray elements\n"; for(i=0;i<size;i++)

cin>>a[i];

cout<<"enter the floating array elements\n"; for(i=0;i<size;i++)

cin>>b[i];

min1=minimum(a,size);

min2=minimum(b,size);


cout<<"The minimum integer elements is:\n"; cout<<min1;
cout<<"\nThe minimum floating elements is :\n";

cout<<min2;

getch();

return 0;

}

************************************OUTPUT******************************

enter the size value: 5

enter the integer aray elements 20 30 10 38 28



 enter the floating array elements 20.4 19.7 14.8 1.7 2.6


The minimum integer elements is: 10
The minimum floating elements is : 1.7

26.8.15

Write a C++ program that displays the size

Write a C++ program that displays the size (in bytes) of a given file. The name of

the file is specified as command line argument.

#include<iostream.h>

#include<fstream.h>

#include<conio.h>

void main(int arge,char*argv[])

{

int bcount=0; char cl; ifstream testfile; clrscr();

cout<<"enter the file name\n"; cin>>argv[1]; testfile.open(argv[1],ios::in); while(testfile)
{

testfile.read((char*)&cl,sizeof(cl));

++bcount;

}

testfile.close();

cout<<"size of the given file:"<<argv[1]<<"is"<<bcount<<"bytes"<<endl; getch();
}


*********************************OUTPUT********************************


enter the file name

ph.cpp


size of the given file:is2308bytes

Write an interactive, menu-driven program

Write an interactive, menu-driven program that will access the file created in program No.17 and implement the following tasks:
i)   To determine the telephone numbers of the specified person.

ii)   To determine the name if a telephone number is given.

iii)   To update the telephone number whenever there is a change.

#include<iostream.h>

#include<fstream.h>

#include<iomanip.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h> class phone

{

public: long int no; char name[10]; void getdata()

{

cout<<endl<<"enter the name :"; cin>>name;
cout<<endl<<"enter the telephone number :"; cin>>no;

}

void putdata()

{

cout<<endl<<setw(10)<<name<<endl<<setw(10)<<no<<endl;

}

};


int main()

{


phone ph; fstream f; clrscr();
int n; f.open("pp1.txt",ios::trunc|ios::in|ios::out|ios::ate); cout<<"Enter the number of records :";

cin>>n;

for(int i=0;i<n;i++)

{

ph.getdata();

f.write((char *)&ph,sizeof(ph));

}

f.seekg(0);

cout<<endl<<"conents of the file are as follows "; while(f.read((char *)&ph,sizeof(ph))) ph.putdata();

int choice; while(1)

{

cout<<"\n1.get name from telephone number"; cout<<"\n2.get telephone number from name"; cout<<"\n3.alter the telephone number"; cout<<"\n4.EXIT";
cout<<"\nenter ur choice "; cin>>choice;
f.seekg(0);

f.clear();

switch(choice)

{

case 1:cout<<"\nEnter the telephone number to get the name "; int no;

cin>>no;

f.seekg(0);

f.clear();

while(f.read((char *)&ph,sizeof(ph)))

{
if(ph.no==no)

{

ph.putdata();

}

}

break;

case 2:cout<<"\nEnter the name to get the telephone number"; char search_name[10];

cin>>search_name; while(f.read((char *)&ph,sizeof(ph)))

{

if(strcmp(ph.name,search_name)==0) ph.putdata();

}

break;

case 3:cout<<"\nEnter the name to modify the telephone number "; char searchname[10];

cin>>searchname; while(f.read((char *)&ph,sizeof(ph)))

{

if(strcmp(ph.name,searchname)==0)

{

ph.putdata();

cout<<endl<<"Enter the new details to modify :"; ph.getdata();

int loc=sizeof(ph); int p=f.tellp(); cout<<endl<<p; f.seekp(p-loc);

f.write((char *)&ph,sizeof(ph));

}

}

cout<<endl<<"After modification the content of the file is as follows ";

f.clear();

f.seekg(0);

while(f.read((char *)&ph,sizeof(ph)))

ph.putdata();

break;

case 4:exit(0);

}

}

}

******************************OUTPUT**********************************

Enter the number of records :3 enter the name :aaa

enter the telephone number :11111 enter the name :bbb

enter the telephone number :22222 enter the name :ccc

enter the telephone number :33333 conents of the file are as follows

aaa 11111 bbb 22222 ccc 33333
1.get name from telephone number 2.get telephone number from name 3.alter the telephone number 4.EXIT
enter ur choice 1

Enter the telephone number to get the name 11111 found
aaa 11111
not foundnot foundnot found 1.get name from telephone number 2.get telephone number from name 3.alter the telephone number 4.EXIT

enter ur choice 2

Enter the name to get the telephone numberccc ccc

33333

1.get name from telephone number 2.get telephone number from name 3.alter the telephone number 4.EXIT
enter ur choice 3

Enter the name to modify the telephone number ccc ccc

33333

Enter the new details to modify : enter the name :ccc
enter the telephone number :44444 42

After modification the content of the file is as follows aaa
11111 bbb 22222 ccc 44444

1.get name from telephone number 2.get telephone number from name 3.alter the telephone number 4.EXIT
enter ur choice 4

C++ program to read the file and output the list in the tabular format

A file contains a list of names and telephone numbers in the following form:

Name             Tel. No.

Write a C++ program to read the file and output the list in the tabular format. The name should be left-justified and numbers right-justified. Use a class object to store each set of data.

#include<iostream.h>

#include<conio.h>

#include<iomanip.h>

#include<fstream.h> class phone

{

public:

char name[20]; long int phone_no; void getdata()

{

cout<<"Enter the name and phone_no"<<"\n"; cin>>name>>phone_no;

}

void putdata()

{

cout<<endl<<setw(20)<<setiosflags(ios::left)<<name<<setw(10) <<setiosflags(ios::right)<<phone_no<<endl;

}

};

void main()

{

fstream f; phone ph; int n;


f.open("phonefile",ios::out|ios::in|ios::app);
clrscr();

cout<<"\nEnter the total no_of records:"; cin>>n;
for(int i=0;i<n;i++)

{

ph.getdata();

f.write((char *)&ph,sizeof(ph));

}

f.seekg(0);

cout<<"\n\nThe content of the file is:\n\n"; cout<<setw(20)<<setiosflags(ios::left)<<"NAME"

<<setw(10)<<setiosflags(ios::right)<<"TELEPHONE_NO"; while(f.read((char *)&ph,sizeof(ph)))

ph.putdata();

f.close();

getch();

}


**********************************OUTPUT********************************

Enter the total no_of records:3 Enter the name and phone_no RAGHU
2456570

Enter the name and phone_no RAJEEV
2457859

Enter the name and phone_no RANJU
2451230

The content of the file is:

NAME                TELEPHONE_NO

RAGHU                  2456570

RAJEEV                2457859

RANJU                   2451230

C++ program that uses a single file

Write a C++ program that uses a single file for both reading and writing the data.

#include<iostream.h>

#include<conio.h>

#include<fstream.h> int main()
{

clrscr();

ofstream outf("ITEM.txt"); cout<<"Enter the filename:"; char name[30];

cin>>name;

outf<<name<<"\n"; cout<<"Enter ITEM cost:"; float cost;

cin>>cost;
outf<<cost;
outf.close();

ifstream inf("ITEM.txt"); inf>>name;
inf>>cost;

cout<<"The name of the item is:"<<name; cout<<"\nItem cost is :"<<cost; inf.close();

getch(); return 0;
}

**************************************OUTPUT****************************

Enter the ITEM name:vicks

Enter ITEM cost:20.3
The name of the item is:vicks

Item cost is :20.299999

Design your own manipulator

Design your own manipulator to provide the following output specification for printing money value:
1)  10 columns width

2)  The character '$' at the beginning

3)  Showing '+' sign.

4)  Two digits precision

5)  Filling of unused spaces with ' * '

6)  Trailing zeros shown


#include<iostream.h>

#include<conio.h>

#include<iomanip.h>

ostream &moneyshow(ostream &output)

{

cout<<'$'; cout.fill('*');

cout.setf(ios::showpos);

cout.setf(ios::showpoint);

cout.width(10);

cout.precision(2); return output;

}

void main()

{

double amount; clrscr();

cout<<"enter the value:"; cin>>amount;

cout<<"\nyour money value:"; cout<<moneyshow<<amount; getch();

}

  


*************************OUTPUT 1***************************************

enter the value:12.2

your money value:$****+12.20

*************************OUTPUT 2***************************************

enter the value:34

your money value:$****+34.00