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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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.