0

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.

My Full Code

#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;} } 

1 Answer 1

1

I see, you pass p to your function Min_Max. Where p is a pointer to an entry point of an array. That array is created as a local variable in another function evaluate. That doesn't work, because as soon as evaluate has finished, all its local variables, such as the fx array, get destroyed and the pointer you return then points to "nothing".

In that case you can use heap memory (use new operator) to allocate the fx. But don't forget to free it afterward.

Also, look here

Sign up to request clarification or add additional context in comments.

5 Comments

Forgive me but I don't fully understand your answer and English is not my first language but let me try to understand it. Your saying that the pointer p can only access the first value of the array? Heap memory is new to me and i have seen the example you gave but i don't understand how to use it with my code. Lastly, what do you mean by "don't forget to free it afterward."
Do you know about variable scope? That is the exact problem here. When you create a local variable it only exists inside these brackets {}. When a program goes out of the brackets the variable does not exist anymore. So, you create a variable inside the brackets, pass a pointer to it to the outside and as soon as you exit the function, this pointer points to nothing. And about heap memory you just go ahead and google new operator, I'm sure you'll find everything else related as soon as you start :)
More explicitly: You create a variable fx inside the function evaluate. fx creates as soon as the function starts working and dies as soon as the function stops working. So, while the function is working, you take the memory address of fx (which is just a number) and return it from the function. In main function, you assign to variable p that memory address. Since evaluate has stopped working, all its local variables including fx do not exist at this point of time. p has the memory address at which the fx was created but now there is "nothing" at this address.
Variable scope as in Local and Global? Would changing float *p to global help? Because I change it to global and it is the same as before. Thank you for explaining it clearly that helps but what I don't understand is why my method works on a small scale but not like this?Thanks for the help that you give hope people would do the same to you in the future.
If you put the line float fx[n]; right before the line int main() then it will work. Or if you just change float fx[n]; to static float fx[n];. But I would not mess with changing the scope it's a bad practice. I would accept an array pointer as a parameter and dereference it inside the function. You will find a lot of information exactly about this if you type C++ return array from function in google.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.