Military Institute of Science and Technology: Application of Computer Programming For Optimization of Ship Design
Military Institute of Science and Technology: Application of Computer Programming For Optimization of Ship Design
Military Institute of Science and Technology: Application of Computer Programming For Optimization of Ship Design
Assignment No: 03
Submitted To:
Lecturer Kaniza Islam
NAME Department, MIST
Submitted By:
A.K.M. Samiu Haque Barnil
ID: 201824017
NAME Department
Level: 03
Q1. Write overloaded functions that calculate the volume of a sphere and a cylinder.
Solution:
Function overloading is the phenomena when two or more functions have the same name and return
type but have different parameters or set of parameters. These functions are differentiated by the
arguments which are used during the function call.
The following code demonstrates the use of overloaded functions that calculate the volume of a sphere
and a cylinder:
#include<iostream>
using namespace std;
#define PI 3.1416
double volume(double);
double volume(double,double);
int main()
{
double rs, rc, hc;
double volume(double r)
{
double v = (4*PI*r*r*r)/3;
return v;
}
double volume(double r, double h)
{
double v = PI*r*r*h;
return v;
}
#include <iostream>
using namespace std;
const double Pi = 3.1416;
void Area(double Radius, double Result)
{
Result = Pi * Radius * Radius;
}
int main()
{
cout << “Enter radius: “;
double Radius = 0;
cin >> Radius;
double AreaFetched = 0;
Area(Radius, AreaFetched);
cout << “The area is: “ << AreaFetched << endl;
return 0;
}
Solution:
The given code is supposed to calculate the area of a circle for a given radius. But it will show “The
area is: 0” for every given input.
The given code contains two logical error. At first the function used for the calculation of area named
‘Area’ has a return type of void, but it is supposed to return the parameter ‘Result’ for the calculated
area of a circle. On the other hand, in the main function the parameter ‘AreaFetched’ is set to zero as a
default value which is not the issue, but it is need to be overwritten with the return value of function
‘Area’. Thus, the given code should be corrected as follows:
#include <iostream>
using namespace std;
const double Pi = 3.1416;
int main()
{
cout << "Enter radius: ";
double Radius = 0;
cin >> Radius;
double AreaFetched = 0;
AreaFetched = Area(Radius, AreaFetched);
cout << "The area is: " << AreaFetched << endl;
return 0;
}
Solution:
The given function declaration contains two parameters in its declaration and among these one is
declared with a default value. As per the conventional practice, the parameters with default value are
declared last, so that the function can be called without passing the arguments for parameters with
default value. On the other hand, the well known ‘Pi’ is a constant and has a value of ‘3.14’. But during
the function call there is a possibility of overwriting this value, which will provide an incorrect result
for area.
Thus, the above function should be declared as follows:
double area(double Radius, double Pi = 3.14);
And if the constant ‘Pi’ is declared separately as a constant, as per the standard practice, then the
function declaration as follows:
double area(double Radius);
Q4. Write a function with return type void that still helps the caller calculate the area and
circumference of a circle when supplied the radius.
Solution:
The function with return type void which calculates the area and circumference of a circle for a given
radius is demonstrated in the following program:
#include <iostream>
using namespace std;
const double Pi = 3.1416;
cout << "The area of Circle = " << Area << endl;
cout << "The circumference of Circle = " << Circumference;
}
int main()
{
cout << "Enter radius: ";
double Radius = 0;
cin >> Radius;
Circle(Radius);
return 0;
}
Q5. Using Recursive Functions to Calculate a Number in the Fibonacci Series.
Solution:
The following code demonstrates the use of recursion for the calculation of a number in the Fibonacci
Series.
#include<iostream>
using namespace std;
int fibonacci(int n)
{
if (n==1 || n==2)
return (n-1); //Since 1st term is 0 and 2nd term is 1
else
return (fibonacci(n-1)+fibonacci(n-2));
}
int main()
{
int n; //No. of terms
return 0;
}