Lab #7 Due: 11/3, Credit: 30 pts 1. VSC (Very simple calculator) – write a progr
ID: 3758205 • Letter: L
Question
Lab #7 Due: 11/3, Credit: 30 pts
1. VSC (Very simple calculator) – write a program using switch statement and user-defined functions to perform the following calculations: Prompt the user to input two integer numbers, then according to the user’s choice for the operator (+, -, x, /, %) to perform the corresponding operation, then output the result.
Write a function for each operation, the five functions are declared as:
int add( int x, int y);
int subtract( int x, int y);
int multiply( int x, int y);
int divide( int x, int y);
int modulus( int x, int y);
For example: Run-1:
If user input two integer 123 7
If user choose ‘+’ as operator
then by calling function add( ) the program will output Result: 123 + 7 = 130
Run-2:
If user input two integer 123 7 If user choose ‘%’ as operator
then by calling function modulus( ) the program will output Result: 123 % 7 = 4
2. The following formula gives the distance between two points (x1, y1) and (x2, y2) in the Cartesian plane:
(x x)2 +(y y)2 21 21
Now, if one point resides on the center of a circle, and the other is on the circle, then the above formula can be used to compute the radius of the circle.
Here is the main program:
#include <iostream> #include <cmath> using namespace std;
// Write the function prototype here.
int main( ) {
double x1,y1 ; double x2, y2 ; double radius ;
// coordinates of center of the circle // coordinates of 1 point on the circle // radius of the circle
cout << "Enter 4 numbers corresponding to the coordinates of the two points"; cin >> x1 >> y1 >> x2 >> y2;
radius = calculateRadius( x1, y1, x2, y2 ) ; // Call function to compute the radius
cout << "The radius is: " << radius << endl ; // Print the value of the radius
return 0; }
// Write the function definition here.
1. Type in the above code. Create the function prototype and function body code for calculateRadius and put them in the appropriate places. The function accepts 4 double numbers and return one double number. The function should use the predefined functions: sqrt( ) and pow( ).
2. Write another function called findCircumference, that takes the radius of the circle and returns the circle’s circumference. The formula for a circumference is: 2 r , here = 3.14 , r is the radius.
3. Write another function called findArea that takes the radius of the circle and returns the area of the circle. The formula for the area of the circle is: r2
Explanation / Answer
1.simple calculator
//set of function declarations
int add( int x, int y);
int subtract( int x, int y);
int multiply( int x, int y);
int divide( int x, int y);
int modulus( int x, int y);
//for other cases other than the ones mentioned in problem
//definition to add
int add( int x, int y)
{
c=x+y;
return (c);
}
//definition to calculate difference
int subtract( int x, int y)
{
c=x-y;
return (c);
}
//definition to get product
int multiply( int x, int y)
{
c=x*y;
return (c);
}
//definition to perform division
//float is preffered as datatype here.I used int data type here just as mentioned in problem
int divide(int x, int y)
{
c=x/y;
return (c);
}
//definition to get remainder
int modulo( int x, int y)
{
c=x%y;
return (c);
}
2.C++ program to find
i)radius(using distance formula)
ii)find circumference
iii)find area
using namespace std;
#include <iostream>
#include <cmath>
#define pi 3.14; //from here on the value of variable pi will be 3.14
double calculateRadius( double x1,double y1, double x2, double y2 ) ;
double findCircumference(double radius);
double findArea(double radius);
int main( ) {
double x1,y1 , x2, y2 , radius,circumference,area ;
// coordinates of center of the circle // coordinates of 1 point on the circle // radius of the circle
cout << "Enter 4 numbers corresponding to the coordinates of the two points";
cin >> x1 >> y1 >> x2 >> y2;
//1. call to find radius
radius = calculateRadius( x1, y1, x2, y2 ) ; // Call function to compute the radius
cout << "The radius is: " << radius << endl ; // Print the value of the radius
//2. call to find circumference
circumference=findCircumference(radius);//call function to compute circumference
cout << "The circumference is: " << circumference << endl ; // Print the value of the circumference
//3. call to find area
area=findArea(radius);//call function to compute area
cout << "The area is: " << area << endl ; // Print the value of the area
return 0;
}
// 1.definition to find the radius using distance formula
double calculateRadius( double x1,double y1, double x2, double y2 )
{
return(sqrt(pow((x2-x1),2)-pow((y2-y1),2))); //calculating radius using distance formula
}
//2.function definition to find circumference
double findCircumference(double radius)
{
return(2*pi*r);
}
//3.function definition to find area
double findArea(double radius)
{
return(r*pi*r);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.