C++ Programming
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’ .
[Marks 15]
Solution
#include<iostream.h>
#include<conio.h>
class mindata
{
int a,b,s[20],n,temp,i,j;
float x,y,z;
public:
void accept(int p,int q,float r,float s,float t)
{
a=p;
b=q;
x=r;
y=s;
z=t;
/* cout<<"Enter the integer value:";
cin>>a>>b;
cout<<"Enter the floating values:";
cin>>x>>y>>z; */
}
int min(int a,int b);
float min(float x,float y,float z);
int min(int s[],int n);
};
int mindata:: min(int a,int b)
{
if(a<b)
{
return a;
}
else
{
return b;
}
}
float mindata:: min(float x,float y,float z)
{
if(x<y && x<z)
{
return x;
}
else if(y<z && y<x)
{
return y;
}
else
{
return z;
}
}
int mindata:: min(int s[],int n)
{
int am;
for(i=0;i<n;i++)
{
if(s[i]<am)
{
am=s[i];
}
}
return am;
}
void main()
{
clrscr();
mindata m1,m2,m3;
int p,q,ans;
int i,n,e[20],d1;
float r,s,t,ans1;
cout<<"Enter the value:";
cin>>p>>q;
cout<<"Enter the value:";
cin>>r>>s>>t;
m1.accept(p,q,r,s,t);
ans=m1.min(p,q);
ans1=m1.min(r,s,t);
cout<<"Enter how many element in array:";
cin>>n;
cout<<"Enter array element:";
for(i=0;i<n;i++)
{
cin>>e[i];
}
cout<<"\n Minimum integer is:"<<ans;
cout<<"\n Minimum Float is:"<<ans1;
cout<<"\n Minimum Element is:"<<m1.min(e,n);;
getch();
}
Output
Enter the value:
5
9
Enter the value:
2.3
4.6
8.3
Enter how many element in array:
3
Enter array element:
5
3
9
Minimum integer is:5
Minimum Float is:2.3
Minimum Element is:3
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” . [Marks 25]
Solution
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<iostream.h>
#include<fstream.h>
void main(int ac,char *an[])
{
int no,i,j;
fstream f1,f2;
f1.open("Prime.txt",ios::out);
f2.open("Others.txt",ios::out);
for(j=1;j<ac;j++)
{
no=atoi(an[j]);
for(i=2;i<no;i++)
{
if(no%i==0)
break;
}
if(i==no)
{
f1.write(an[j],strlen(an[j]));
f1.put(' ');
}
else
{
f2.write(an[j],strlen(an[j]));
f2.put(' ');
}
}
f1.close();
f2.close();
getch();
}
Tags:
c++