Lab # 11 Write a program to evaluate the following function f(x)-3log(2x) g(x) =
ID: 3725481 • Letter: L
Question
Lab # 11 Write a program to evaluate the following function f(x)-3log(2x) g(x) = 4,f'(x) Your program should employ two functions where the actual computations are being done. You could call these functions anthing 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 fx) 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. 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
// basic file operations
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
double f(double x){
return 2*log(2*pow(x,3));
}
double g(double x){
return 4*pow(f(x),3);
}
int main () {
ofstream myfile;
myfile.open ("myfile.txt");
for(int x =1; x<10; x++){
myfile << x<<" ,"<<f(x)<<" ,"<<g(x)<<" ";
}
myfile.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.