Printing out variables in c changes values of variables

Posted by George Wilson on Stack Overflow See other posts from Stack Overflow or by George Wilson
Published on 2012-12-02T16:18:15Z Indexed on 2012/12/02 17:04 UTC
Read the original article Hit count: 178

Filed under:
|
|

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!!

© Stack Overflow or respective owner

Related posts about c

    Related posts about matrix