Different output if I debug?
I have a program that spits out nonsense when I run it, but it runs flawlessly if I run it step-by-step in a debugger with the same input.
any idea why this could be?
-------------
I am reluctant to paste long code since it might deter people who can help but, here it is..
(program finds path down graph with largest number sum)
(first line is # of graphs, 2nd is nxm size, then box of numbers is the graph)
input is:
1
2 2
1 1 6
9 1 1
1 1 1
output when debugging step-by-step : 11
output when running exe: 775639875
Code:
#include <stdio.h>
inline int max(int a,int b,int c)
{
if(a>b)
if(a>c)
return a;
else
return c;
if(b>a)
if(b>c)
return b;
else
return c;
if(c>a)
if(c>b)
return c;
else
return b;
return b;
}
void solve(int **grid,int rows,int cols)
{
for(int j=0;j<rows;++j)
{
grid[j][0] = -9999;
grid[j][cols+1] = -9999;
}
int **dp = new int*[rows];
for(int i = 0; i < rows; i++)
dp[i] = new int[cols+2];
for(int i=0;i<=cols+1;++i)
dp[0][i] = grid[0][i];
for(int k=1;k<rows;++k)
{
for(int n=1;n<=cols;++n)
{
int a,b,c,largest;
a = dp[k-1][n-1]+grid[k][n];
b = dp[k-1][n]+grid[k][n];
c = dp[k-1][n+1]+grid[k][n];
largest = max(a,b,c);
dp[k][n] = largest;
}
}
int solution = -9999;
for(int i=1;i<=cols;++i)
if(dp[rows-1][i] > solution)
solution = dp[rows-1][i];
printf("%d\n",solution);
}
int main()
{
int n;
scanf("%d",&n);
int **grid;
while(n--)
{
int rows,cols;
scanf("%d %d",&rows,&cols);
grid = new int*[rows];
for(int i = 0; i < rows; i++)
grid[i] = new int[cols+2];
for(int i=0;i < rows;++i)
for(int j=1;j <= cols;++j)
{
int in;
scanf("%d",&in);
grid[i][j]=in;
}
solve(grid,rows,cols);
}
return 0;
}
Thanks a ton!
btw: i am 10000% sure the code works correctly since I received correct answer for this code already.
i am using code::blocks compiler and debugger