Write a program to evaluate the following function f(x) = 3 log(2x) g(x) = 4f3(x
ID: 3725480 • Letter: W
Question
Write a program to evaluate the following function f(x) = 3 log(2x) g(x) = 4f3(x) Your program should employ two functions where the actual computations are being done. You could call these functions anything you like. Here is what needs to be done: 1. In one function calculate f(x) by asking the user to enter x value and pass it to the function where f(x) is being calculated. The result is returned to the main using the return statement. 2. In another function calculate g(x) where the value of f(x) previously calculated is passed to it. This function should not return the calculated g(x) instead it should use the pass-by-reference method to make it available in the main function. 3. Print both f(x) and g(f(x) from the main function with appropriate headings Print the x, f, and g values into a data file called mydata.txt using the ofstream. 4.Explanation / Answer
main.cpp
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
double caleculateFx(int x);
void caleculateGx(double &temp);
int main()
{
int input_x;
double resultOfFx;
double resultofGx;
fstream file;
cout<<"Enter the value of X : ";
cin>>input_x;
resultOfFx = caleculateFx(input_x);
resultofGx = resultOfFx;
cout<<" f(x) : "<<resultOfFx<<endl;
caleculateGx(resultofGx);
cout<<" g(x) : "<<resultofGx;
file.open ("mydata.txt", ios::out ); // creating file in write mode
// Writing on file
file <<"The value of x : "<<input_x<< endl;
file <<"The value of f(x) : "<<resultOfFx<< endl;
file <<"The value of g(x) : "<<resultofGx<< endl;
//closing the file
file.close();
return 0;
}
double caleculateFx(int x) { // caleculate f(x)
double result;
result = 3 * log(2 * pow(x,3));
return result;
}
void caleculateGx(double &temp) { // call by reference
temp = 4 * pow(temp , 3);
}
Output:
Enter the value of X : 3
f(x) : 11.967
g(x) : 6855.05
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.