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 8932067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:15:28+00:00 2026-06-15T09:15:28+00:00

I have an odd problem with some c-programme here. I was getting some wrong

  • 0

I have an odd problem with some c-programme here. I was getting some wrong values in a matrix I was finding the determinant of and so I started printing variables – yet found that by printing values out the actual values in the code changed.

I eventually narrowed it down to one specific printf statement – highlighted in the code below. If I comment out this line then I start getting incorrect values in my determinent calculations, yet by printing it out I get the value out I expect

Code below:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define NUMBER 15

double determinant_calculation(int size, double array[NUMBER][NUMBER]);

int main() {
    double array[NUMBER][NUMBER], determinant_value;
    int size;

    array[0][0]=1;
    array[0][1]=2;
    array[0][2]=3;
    array[1][0]=4;
    array[1][1]=5;
    array[1][2]=6;
    array[2][0]=7;
    array[2][1]=8;
    array[2][2]=10;

    size=3;

    determinant_value=determinant_calculation(size, array);
    printf("\n\n\n\n\n\nDeterminant value is %lf \n\n\n\n\n\n", determinant_value);
    return 0;
}

double determinant_calculation(int size, double array[NUMBER][NUMBER])
{
    double determinant_matrix[NUMBER][NUMBER], determinant_value;
    int x, y, count=0, sign=1, i, j;


    /*initialises the array*/
    for (i=0; i<(NUMBER); i++)
    {
        for(j=0; j<(NUMBER); j++)
        {
            determinant_matrix[i][j]=0;
        }
    }

    /*does the re-cursion method*/
    for (count=0; count<size; count++)
    {
        x=0;
        y=0;
        for (i=0; i<size; i++)
        {
            for(j=0; j<size; j++)
            {
                if (i!=0&&j!=count)
                {
                    determinant_matrix[x][y]=array[i][j];
                    if (y<(size-2)) {
                        y++;
                    } else {
                        y=0;
                        x++;
                    }
                }
            }
        }

        //commenting this for loop out changes the values of the code determinent prints -7 when commented out and -3 (expected) when included!
        for (i=0; i<size; i++) {
            for(j=0; j<size; j++){
                printf("%lf ", determinant_matrix[i][j]);
            }
            printf("\n");
        }

        if(size>2) {
            determinant_value+=sign*(array[0][count]*determinant_calculation(size-1 ,determinant_matrix));
        } else {
            determinant_value+=sign*(array[0][count]*determinant_matrix[0][0]);
        }
        sign=-1*sign;
    }
    return (determinant_value);
}

I know its not the prettiest (or best way) of doing what I’m doing with this code but it’s what I’ve been given – so can’t make huge changes. I don’t suppose anyone could explain why printing out the variables can actually change the values? or how to fix it because ideally i don’t want to!!

  • 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-06-15T09:15:29+00:00Added an answer on June 15, 2026 at 9:15 am

    Your variable determinant_value is not initialized as 0, so that causes problems.
    Here is the revised version with the test case.

    #include <stdio.h>
    
    #define NUMBER 3
    
    double determinant_calculation(int size, double array[NUMBER][NUMBER])
    {
        double determinant_matrix[NUMBER][NUMBER], determinant_value = 0;
        int x, y, count=0, sign=1, i, j;
    
        /*initialises the array*/
        for (i=0; i<(NUMBER); i++)
        {
            for(j=0; j<(NUMBER); j++)
            {
                determinant_matrix[i][j]=0;
            }
        }
    
        /*does the re-cursion method*/
        for (count=0; count<size; count++)
        {
            x=0;
            y=0;
            for (i=0; i<size; i++)
            {
                for(j=0; j<size; j++)
                {
                    if (i!=0&&j!=count)
                    {
                        determinant_matrix[x][y]=array[i][j];
                        if (y<(size-2)) {
                            y++;
                        } else {
                            y=0;
                            x++;
                        }
                    }
                }
            }
    
            if(size>2) {
                determinant_value+=sign*(array[0][count]*determinant_calculation(size-1,determinant_matrix));
            } else {
                determinant_value+=sign*(array[0][count]*determinant_matrix[0][0]);
            }
            sign=-1*sign;
        }
        return (determinant_value);
    }
    
    int main()
    {
        int i, j;
        double ans;
        double array[NUMBER][NUMBER] = {{1,2,3},{4,5,6},{7,8,10}};
    
        ans = determinant_calculation(3, array);
    
        printf("the matrix\n");
        for (i = 0; i < NUMBER; ++i) {
            for (j = 0; j < NUMBER; ++j) {
                    printf("%lf ", array[i][j]);
            }
            printf("\n");
        }
        printf("determinant : %lf\n", ans);
        return 0;
    }
    

    And the output would be:

    the matrix
    1.000000 2.000000 3.000000 
    4.000000 5.000000 6.000000 
    7.000000 8.000000 10.000000 
    determinant : -3.000000
    

    But I have no idea for your second question in the comments.

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

Sidebar

Related Questions

I'm not finding any question specifically with the odd problem I have. Basically I've
I have an odd problem. I have a unit test that keeps getting stuck
I have a very odd problem with the following code. For some reason I
I have an odd problem. I'm trying to use Javascript to fetch me some
I have a an odd problem with some texturing in opengl: This scene is
I have an odd problem with MVC Razor @ escaping. In some javascript in
Ok, so I have some problems with C++ iostreams that feels very odd, but
I have odd problem: After starting server I got this error: undefined local variable
I have an odd problem with Django. I have a set of objects that
I have an odd problem where Hibernate is running more queries than I've asked

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.