### Function getting ignored.

Alright, so the idea is that this program takes values from an input file and returns their residual numbers on the grayscale (this is using an array). Problem is no matter where I call my function to take the data from the array and convert it, nothing happens. I'm allowed to copy the algorithm word for word, so I did. This didn't help. Here is the code that matters:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104`` ``````int max(int n, int w); int min(int n, int w); int predictingValue(int n, int w, int nw); int main() { int rows, columns, y, a = 0, b = 0; int projectArray[400][400]; fstream fin; ofstream fout; fin.open("input2.txt"); fout.open("output2.txt"); if(!fin) { cerr << "File did not open." << endl; exit(1); } fin >> columns >> rows; for(int i=0; i < columns; i++) { for(int j=0; j < rows; j++) { fin >> projectArray[i][j]; //projectArray[i][j] = predictingValue(projectArray[i][j+1], projectArray[i+1][j], projectArray[i][j]); //this is the call, it does nothing! this is one of the places it is being tried. } } //if I try the call here, it also does nothing. /* for(int i=0; i < columns; i++) { for(int j=0; j < rows; j++) { projectArray[i][j] = predictingValue(projectArray[i][j+1], projectArray[i+1][j], projectArray[i][j]); // thought maybe a loop would do it, and it does nothing. } } */ for(int i=0; i < columns; i++) { for(int j=0; j < rows; j++) { fout << projectArray[i][j] << " "; } fout << endl; } fin.close(); fout.close(); char response; cin >> response; return 0; } int max(int n, int w) { if(n > w) { return n; } else { return w; } } int min(int n, int w) { if(n < w) { return n; } else { return w; } } int predictingValue(int n, int w, int nw) { int y; if(nw>=max(n,w)) { y=max(n,w); } else if(nw<=min(n,w)) { y=min(n,w); } else { y = w + n - nw; } return y; }``````

Can anyone tell what's going wrong with my call? Is it how I set the function equal to the array?
Last edited on
You are using future data.
`projectArray[i][j] = predictingValue(projectArray[i][j+1], projectArray[i+1][j], projectArray[i][j]);` there is garbage in `projectArray[i][j+1], projectArray[i+1][j]`

Get a debugger, run step by step, watch variables...
what do you mean future data? It was read from the file already.
Last edited on
Hes right. When you read from fin you increment the file pointer at each iteration of the loop into the new position. Because fin has not read into position j+1 and i+1 yet, those memory locations contain garbage data, but you are referencing them anyway.
Then, shouldn't placing the call after that loop work? By being outside the loop, all the data would have been read in right?
Topic archived. No new replies allowed.