Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7596057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:49:36+00:00 2026-05-30T21:49:36+00:00

I have a 2d array (matrix) where I am trying to calculate the biggest

  • 0

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));     
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:49:38+00:00Added an answer on May 30, 2026 at 9:49 pm

    temp is not initialized in calcProduct, and neither is maxProduct. They will contain random, garbage values the first time through the loop, which is corrupting your maxProduct result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem when trying to convert a sequnce of cell array's elements
I have a matrix in the type of a Numpy array. How would I
I have an array of numbers that potentially have up to 8 decimal places
I have matrix with 3 dimension (n*m*k). I am trying to fined the maximum
I'm trying to create a matrix data structure in C. I have a struct
I have been trying to solve this problem the whole day: How do I
I am trying to deal with a very large dataset. I have k =
let's say I have a matrix (array) like this example, but much larger: 0
I have a matrix represented in PHP as an array: array( array('a','b'), // a
I have a simple OpenGL program and trying to draw an instanced array that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.