First of thanks for giving me a hand with this. I am no expert at C++ but i have done some work in C. My code problem is that it would not display the returned array value correctly.
In general what my program trying to do is to evaluate a function F(x) , display it in a table format and find its min and max. I have find ways of doing all that but when I want to display the returned value of array F(x) it somehow got distorted.The first value is always correct for example like
cout << *(value+0) <<endl; but the next one the value is not the same as the supposed f(x).Sorry in advance if my code is not up to the proper standard but i been wrapping my head over this for awhile now.
#include <iostream> #include <fstream> #include <cmath> #include <iomanip> #include <string> #include <stdlib.h> using namespace std; float *evaluate (); void display (); void Min_Max(float *); int main() { float *p; evaluate(); display(); cin.get(); p = evaluate(); Min_Max(p); return 0; } float *evaluate() { ofstream Out_File("result.txt"); int n=30; float x [n]; float fx[n]; float interval = ((4-(-2))/0.2); x[0]= -2.0; for(n=0;n <= interval;n++) { fx[n] = 4*exp((-x[n])/2)*sin((2*x[n]- 0.3)*3.14159/180); x[n+1] = x[n] + 0.2; if (Out_File.is_open()) { Out_File <<setprecision(5)<<setw(8)<<showpoint<<fixed<< x[n]; Out_File << "\t\t"<<setprecision(5)<<setw(8)<<showpoint<<fixed<<fx[n]<<endl; } else cout << "Unable to open file"; } Out_File.close(); return fx; } void display() { ifstream inFile; inFile.open("result.txt"); string line; cout << " x\t\t\t f(x)"<<endl; cout << "_______________________________________"<<endl; while( getline (inFile,line)) { cout<<line<<endl; } inFile.close(); } void Min_Max(float *value) { int a=0; for(a=0;a<=30;a++){ cout << *(value+a) <<endl; *value =0;} }