Here is the code that got wrong answer :
/*SUMTRIAN*/
#include<stdio.h>
#include<math.h>
int calculate_largest(int triangle[100][100],int n);
int main()
{
int n,i,j,max_sum,t;
int triangle[100][100];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i = 0;i<n;i++)
{
for(j = 0;j<=i;j++)
{
scanf("%d",&triangle[i][j]);
}
}
max_sum = calculate_largest(triangle,n);
printf("%d\n",max_sum);
}
return 0;
}
int calculate_largest(int triangle[100][100],int n)
{
int temp[100][100];
int i,j;
for(i = n-2;i>=0;i--)
{
for(j = 0; j<=i;j++)
{
temp[i][j] = fmax(triangle[i+1][j],triangle[i+1][j+1]) + triangle[i][j];
triangle[i][j] = temp[i][j];
}
}
return temp[0][0];
}
I modified the above code and avoided using fmax and got Correct Answer. Here is the modified code:
/*SUMTRIAN*/
#include<stdio.h>
int calculate_largest(int triangle[100][100],int n);
int main()
{
int n,i,j,max_sum,t;
int triangle[100][100];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i = 0;i<n;i++)
{
for(j = 0;j<=i;j++)
{
scanf("%d",&triangle[i][j]);
}
}
max_sum = calculate_largest(triangle,n);
printf("%d\n",max_sum);
}
return 0;
}
int calculate_largest(int triangle[100][100],int n)
{
int i,j,max_of_two;
for(i = n-2;i>=0;i--)
{
for(j = 0; j<=i;j++)
{
if(triangle[i+1][j]>triangle[i+1][j+1])
{
max_of_two = triangle[i+1][j];
}
else
{
max_of_two = triangle[i+1][j+1];
}
triangle[i][j] = max_of_two + triangle[i][j];
}
}
return triangle[0][0];
}
Do functions like fmax() of math.h library not work in online judge environments? How to make them work?