Q.1) Write a C++ program to read two float numbers. Perform arithmetic binary operations like +, - , *, / on these numbers using Inline Function. Display resultant value with a precision of two digits.
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
inline float add(float a,float b)
{
return a+b;
}
inline float sub(float a,float b)
{
return a-b;
}
inline float mult(float a,float b)
{
return a*b;
}
inline float div(float a,float b)
{
return a/b;
}
void main()
{
float a,b,ad,sb,multi,divi=0;
clrscr();
cout<<"\nEnter 1 st number : ";
cin>>a;
cout<<"\nEnter 2nd number : ";
cin>>b;
ad=add(a,b);
sb=sub(a,b);
multi=mult(a,b);
if(b==0)
{
cout<<"\nDivision not possible";
}
else
divi=div(a,b);
cout.precision(2);
cout<<"Addition = "<<ad<<endl<<"subtraction = "<<sb<<endl<<"multiplicaion = "<<multi<<endl<<"Divison = "<<divi;
getch();
}
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
inline float add(float a,float b)
{
return a+b;
}
inline float sub(float a,float b)
{
return a-b;
}
inline float mult(float a,float b)
{
return a*b;
}
inline float div(float a,float b)
{
return a/b;
}
void main()
{
float a,b,ad,sb,multi,divi=0;
clrscr();
cout<<"\nEnter 1 st number : ";
cin>>a;
cout<<"\nEnter 2nd number : ";
cin>>b;
ad=add(a,b);
sb=sub(a,b);
multi=mult(a,b);
if(b==0)
{
cout<<"\nDivision not possible";
}
else
divi=div(a,b);
cout.precision(2);
cout<<"Addition = "<<ad<<endl<<"subtraction = "<<sb<<endl<<"multiplicaion = "<<multi<<endl<<"Divison = "<<divi;
getch();
}
Q.2) Create a Vector class to represent a series of integer values. Allocate memory for Vector object using new operator. Write a C++ menu driven program with following member functions:i. To accept a vector ii. To display a vector in the form (10,20,30,…) iii. To multiply by a scalar value iv. To modify the value of a given position from vector
#include<conio.h>
#include<iostream.h>
class vector
{
int *a;
int n;
public:
void create()
{
int i;
cout<<"\nEnter the dimensions of the vector space: ";
cin>>n;
a=new int[n];
cout<<"\nEnter the vector: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
}
void modify()
{
int m,i;
cout<<"\nEnter the position of the co-ordinate to be changed: ";
cin>>i;
cout<<"\nEnter the new value: ";
cin>>m;
a[i-1]=m;
}
void scalar()
{
int x,i;
cout<<"\nEnter the scalar: ";
cin>>x;
for(i=0;i<n;i++)
{
a[i]=x*a[i];
}
}
void display()
{
int i;
cout<<"\nThe vector is: (";
for(i=0;i<n-1;i++)
{
cout<<a[i]<<",";
}
cout<<a[n-1]<<")";
}
};
void main()
{
vector v;
clrscr();
int ch;
do
{
cout<<"\n 1.Accpet vector \n 2.Display Vetor \n 3.Modify vector element \n 4.scalar operation \n 0.Exit";
cout<<"\n Enter choice : ";
cin>>ch;
switch(ch)
{
case 1 : v.create();
break;
case 2 : v.display();
break;
case 3 : v.modify();
break;
case 4 : v.scalar();
break;
case 0 : break;
}
}while(ch!=0);
getch();
}