Saturday, 20 January 2018

CPP-Slip26



Slip26

Q.1)     Create a C++ class Mindata to perform following functions:
           int min( int, int) – returns the minimum of two integer arguments.
float min(float, float,   float) – returns the minimum of three float arguments.
int min( int [ ] ,int) – returns the minimum of all elements in an array of size ‘n’.
#include<iostream.h>
#include<conio.h>
class mindata
{
public:
void min(int,int);
void min(float,float,float);
void min(int arr[],int n);

};
void mindata::min(int a,int b)
{
if(a<b)
cout<<"\nminimum int number is\t"<<a;
else
cout<<"\nminimum int number is\t"<<b;
}
void mindata::min(float c,float d,float e)
{
if(c<d &&c<e)
cout<<"\nminimum float number is\t"<<c;
else if(d<c && d<e)
cout<<"\nminimum float number is\t"<<d;
else
cout<<"\nminimum float number is\t"<<e;
}
void mindata:: min(int g[],int n)
{       int a,i;
int smallest;
for(i=0;i<n;i++)
{
cout<<i;
cout<<"\t\t";
cin>>g[i];
}
smallest = g[0];

for (i=0; i<n; i++)
{
if (g[i]<smallest)
{
smallest=g[i];
}
}
cout<<"\nsmallest number in an array is \t"<<smallest;
}

void main()
{
int a,b,n,g[10];
float c,d,e;
clrscr();
mindata x,y,z;
cout<<"\nenter two integer  numbers\n";
cin>>a>>b;
x.min(a,b);
cout<<"\nenter three float number\n";
cin>>c>>d>>e;
y.min(c,d,e);
cout<<"\nhow many elements you want to enter \n";
cin>>n;
z.min(g,n);
getch();

}                                                                                                                          

Q.2)     Write a C++ program which will accept ‘n’ integers from user through command line argument. Store Prime numbers in file “Prime.txt” and remaining numbers in “Others.txt”.                                                                                                               
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>

void main(int argc,char *a[])
{
char ch[4];
ofstream fp,fo;int i,j,k,no,flag;
fp.open("prime.txt",ios::out|ios::app);
fo.open("other.txt",ios::out|ios::app);
for(i=1;i<argc;i++)
{
strcpy(ch,a[i]);                
no=atoi(a[i]);
flag=0;
for(j=2;j<=no/2;j++)
{
if(no%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
for(k=0;ch[k]!='\0';k++)
fp.put(ch[k]);
fp.put(' ');
}
else
{
for(k=0;ch[k]!='\0';k++)
fo.put(ch[k]);
fo.put(' ');
}
                  
}
fp.close();
fo.close();
getch();
}