Given the following four functions, how would they be put into a main function (
ID: 3846938 • Letter: G
Question
Given the following four functions, how would they be put into a main function (int main()). Use cout and cin statements. (C++ LANGUAGE ONLY PLEASE)
1.
float computeRoot(float root, int index)
{
float tp, mid;
float low = 0;
float high = root;
do
{
mid = (low + high) / 2;
if (Power(mid, index) > root)
{
high = mid;
}
else
{
low = mid;
}
mid = (low + high) / 2;
tp = (computePower(mid, index) - root);
if (tp < 0)
{//grab absolute value
tp = tp * (-1.0);
}
} while (tp > .000005);//accuracy of our root
return mid;
}
2.
float computePower(float x, int n)
{
float numProduct = 1;
int i;
for (i = 0; i < n; i++)
{
numProduct = numProduct * x;
}
return numProduct;
}
3.
int computeGCD(int a, int b)
{
while (a != b)
{
if (a > b)
{
a = a - b;
}
else
{
b = b - a;
}
}
return a;
}
4.
int computeLCM(int a, int b)
{
int tmp_lcm;
tmp_lcm = ((a * b) / GCD(a, b));
return tmp_lcm;
}
int computeMod(int num1, int num2)
{
float computePerc(float, float)
{
return 0.0f;
}
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
float computePower(float x, int n)
{
float numProduct = 1;
int i;
for (i = 0; i < n; i++)
{
numProduct = numProduct * x;
}
return numProduct;
}
float computeRoot(float root, int index)
{
float tp, mid;
float low = 0;
float high = root;
do
{
mid = (low + high) / 2;
if (computePower(mid, index) > root)
{
high = mid;
}
else
{
low = mid;
}
mid = (low + high) / 2;
tp = (computePower(mid, index) - root);
if (tp < 0)
{//grab absolute value
tp = tp * (-1.0);
}
} while (tp > .000005);//accuracy of our root
return mid;
}
int computeGCD(int a, int b)
{
while (a != b)
{
if (a > b)
{
a = a - b;
}
else
{
b = b - a;
}
}
return a;
}
int computeLCM(int a, int b)
{
int tmp_lcm;
tmp_lcm = ((a * b) / computeGCD(a, b));
return tmp_lcm;
}
int main(int argc, char const *argv[])
{
int b;float a;
cout<<"Enter a and b ";
cin>>a>>b;
cout<<" computeRoot for "<<a<<"&"<<b<<" is "<<computeRoot(a,b);
cout<<" computePower for "<<a<<"&"<<b<<" is "<<computePower(a,b);
cout<<" computeLCM for "<<a<<"&"<<b<<" is "<<computeLCM(a,b);
cout<<" computeGCD for "<<a<<"&"<<b<<" is "<<computeGCD(a,b);
return 0;
}
===============================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ fou.cpp -lm
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter a and b
2 3
computeRoot for 2&3 is 1.25992
computePower for 2&3 is 8
computeLCM for 2&3 is 6
computeGCD for 2&3 is 1akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter a and b
2 2
computeRoot for 2&2 is 1.41422
computePower for 2&2 is 4
computeLCM for 2&2 is 2
computeGCD for 2&2 is 2
================================================================
Let me know if you have any problem
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.