I have a 2d array (matrix) where I am trying to calculate the biggest product of adjacent numbers. It’s very similar to Project Euler Problem 11 except the user enters how many adjacent numbers they want in the calculation. I have it all right I think. The problem is if i use integer’s to calculate the product of say 5 integers of 99 (e.g. 99*99*99*99*99) it will not display right. The maximum number of adjacent numbers which can be checked is 99. I have tried changing to long doubles (as you can see in the code) but it prints ridiculous numbers, e.g. with 3 adjacent numbers I entered 99 in all matrix positions and should get back 970299 (which I do when the maxProduct is an int), but instead I get:
-497917511184158537131936752181264370659584929560826523880745083032965215342755650440802286656251727430041200624372430370294634536699364412350122489510814753628581807006780156992324264734484592980976635224618682514265787653963930812412392499329499188301075222828863209569131692032
I feel like it’s something obvious but I just can’t see it.
#include <stdio.h>
#include <stdlib.h>
long double calcProduct(int n, int m, int ** matrix)
{
int i, x, y; //Loop counters
long double maxProduct; //Used to hold the maximum product of numbers found so far
long double temp; //Used to hold the current product of numbers
//Searching left to right
for(y = 0; y < n; y++)
{
for(x = 0; x <= n - m; x++)
{
for(i = 0; i < m; i++)
{
temp *= matrix[x + i][y];
}
if(temp > maxProduct)
{
maxProduct = temp;
}
temp = 1;
}
}
//Searching top down
for(x = 0; x < n; x++)
{
for(y = 0; y <= n - m; y++)
{
for(i = 0; i < m; i++)
{
temp *= matrix[x][y + i];
}
if(temp > maxProduct)
{
maxProduct = temp;
}
temp = 1;
}
}
temp = 1;
//Searching diagonal down right
for(x = 0; x < n - m; x++)
{
for(y = 0; y <= n - m; y++)
{
for(i = 0; i < m; i++)
{
temp *= matrix[x + i][y + i];
}
if(temp > maxProduct)
{
maxProduct = temp;
}
temp = 1;
}
}
temp = 1;
//Searching diagonal up right
for(x = 0; x < n - m; x++)
{
for(y = n - 1; y >= m - 1; y--)
{
for(i = 0; i < m; i++)
{
temp *= matrix[x + i][y - i];
}
if(temp > maxProduct)
{
maxProduct = temp;
}
temp = 1;
}
}
return maxProduct;
}
main()
{
int ** matrix; //2D array to hold the matrix items
int n, m; //Used to hold the size of the matrix (n) and the number of adjacent numbers to include in the calculation (m)
int i, j; //Loop counters
//Taking input of n (for size of grid) and m (number of adjacent numbers to include in calculation)
scanf("%d %d", &n, &m);
//Assign the array 'matrix' with the size of int multiplied by the number of items to hold (n)
matrix = (int **)malloc(sizeof(int*)*n);
//If the matrix is null then exit the program
if (matrix == NULL)
{
exit (0);
}
for(i = 0; i < n; i++)
{
matrix[i] = (int *)malloc(sizeof(int)*n);
if(matrix[i] == NULL) exit (0);
}
//Getting the numbers which are held in the matrix
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d", &matrix[i][j]);
}
}
//Prints the highest product by calling the method calcProduct, giving it n, m and the array matrix
printf("%.0Lf", calcProduct(n, m, matrix));
}
tempis not initialized incalcProduct, and neither ismaxProduct. They will contain random, garbage values the first time through the loop, which is corrupting your maxProduct result.