C++ Programming
Slip10
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. [Marks 15]
Solution
#include<iostream.h>
#include<conio.h>
inline void add(float a,float b)
{
cout<<"\n Addition ="<<a+b;
}
inline void sub(float a,float b)
{
cout<<"\n Substraction ="<<a-b;
}
inline void multiple(float a,float b)
{
cout<<"\n multiplication ="<<a*b;
}
inline void divide(float a,float b)
{
cout<<"\n Division ="<<a/b;
}
void main()
{
float a,b;
clrscr();
cout<<"Enter the value of a:";
cin>>a;
cout<<"\nEnter the value of b:";
cin>>b;
cout.width(15);
cout.precision (2);
add(a,b);
sub(a,b);
multiple(a,b);
divide(a,b);
getch();
}
Output
Enter the value of a:9.8
Enter the value of b:3.4
Addition =13.2
Substraction =6.4
multiplication =33.32
Division =2.88
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 [Marks 25]
Solution
#include<iostream.h>
#include<conio.h>
class Vector
{
int *a;
int n;
public:
void accept(int);
void display();
void multiply(int);
void modify(int,int);
Vector() { n = -1;}
};
void Vector :: modify(int pos,int x)
{
if(pos >= 0 && pos < n)
a[pos] = x;
else
cout<<"\n\t Position is not valid";
display();
}
void Vector :: multiply(int x)
{
for(int i=0;i<n;i++)
a[i] *= x;
display();
}
void Vector :: display()
{
int i;
cout<<"\n\tVector is (";
for(i=0;i<n-1;i++)
cout<<" "<<a[i]<<",";
cout<<" "<<a[i]<<")";
}
void Vector :: accept(int size)
{
n = size;
a = new int[n];
for(int i=0;i<n;i++)
{
cout<<"\n\tEnter a["<<i<<"]=";
cin>>a[i];
}
}
int main()
{
Vector obj;
int ch,n,pos,elem,x;
do
{
cout<<"\n\t 1.Initialize";
cout<<"\n\t 2.Display";
cout<<"\n\t 3.Multiply with Scalar";
cout<<"\n\t 4.Modify";
cout<<"\n\t 5.Exit";
cout<<"\n\tEnter your choice:";
cin>> ch;
switch(ch)
{
case 1:
cout<<"\n\tEnter size of vector:";
cin>>n;
obj.accept(n);
break;
case 2:
obj.display();
break;
case 3:
cout<<"\n\tEnter scalar:";
cin>>x;
obj.multiply(x);
break;
case 4:
cout<<"\n\tEnter pos:";
cin>>pos;
cout<<"\n\tEnter new element:";
cin>>elem;
obj.modify(pos,elem);
break;
case 5:
break;
default:
cout<<"\n\tInvalid choice";
}
}while(ch != 5);
getch();
return 0;
}
Output
1.Initialize
2.Display
3.Multiply with Scalar
4.Modify
5.Exit
Enter your choice:1
Enter size of vector:3
Enter a[0]=3
Enter a[1]=5
Enter a[2]=7
1.Initialize
2.Display
3.Multiply with Scalar
4.Modify
5.Exit
Enter your choice:2
Vector is ( 0)
1.Initialize
2.Display
3.Multiply with Scalar
4.Modify
5.Exit
Enter your choice:3
Enter scalar:3
Vector is ( 0)
1.Initialize
2.Display
3.Multiply with Scalar
4.Modify
5.Exit
Enter your choice:4
Enter pos:2
Enter new element:9
Position is not valid
Vector is ( 0)
1.Initialize
2.Display
3.Multiply with Scalar
4.Modify
5.Exit
Enter your choice:2
Vector is ( 0)
1.Initialize
2.Display
3.Multiply with Scalar
4.Modify
5.Exit
Enter your choice:5
Tags:
c++