Function changes variables not involved in the function (in c)

1

So I'm trying to make a program to row reduce a matrix (there are probably a thousand way better ways to do it but I'm trying to figure it out on my own). I have a function that is supposed to make two rows of different arrays equal. It doesn't even take the row variable as an input or have anything to do with it but for some reason after the function is called the row variable switches from whatever was put into the keyboard to 1072693248. I also just noticed a problem with the col variable where it is set to 0 the second time the for loop it is in happens. I have no clue what is causing these problems so any help would be really appreciated. I'm just going to post the entire code since I'm not sure which part is causing the problem (its probably super messy, my only coding experience is one super basic beginner course in c in university). (I put in some printfs to print row or col in different spots to try and find out where the problem was happening).

#include <stdlib.h>
void printArray(double a[][100], int rows, int columns);
void addRow(double a[][100], int columns, int rowToAdd, int rowAddedTo);
void addRowBetweenArrays(double arrayAddedTo[][100],double arrayAdded[][100], int columns, int rowToAdd, int rowAddedTo);
void multiplyRow(double a[][100], int columns, int rowMultiplied, double mupltiplyBy);
void switchRows(double a[][100], int columns, int rowOne, int rowTwo);
//void makeRowsEqual(double rowMadeEqual[][100], double madeEqualTo[][100], int rowInMadeEqualTo, int RowInMadeEqual, int columns);
void makeTwoArraysRowsEqual(double arrayMadeEqual[][100], double arrayMadeEqualTo[][100], int rowMadeEqual, int rowMadeEqualTo, int column);

int main()
{
    int row, col, i, j, checkReduced, rowIsReduced = 0, focus;
    double multiplyFactor;
    printf("Enter the size of your array (rows columns): ");
    scanf("%d %d", &row, &col);
    double array[row][100];
    double tempRow[1][100];
    for(j = 0; j < row; j++)
    {
        printf("Enter row %d: ", j + 1);
        for(i = 0; i < col; i++)
        {
            scanf("%lf", &array[j][i]);
        }
        printf("\n");
    }
    printf("\n\nUnreduced array:\n");
    printArray(array, row, col);
    //start the reducing
    for(focus = 0; focus < row; focus++)
    {
        rowIsReduced = 0;
        for(i = 0; i < col; i++)
        {
            if(i == focus)
            {
                i++;
            }
            if(array[i][focus] != 0)
            {
                rowIsReduced = 1;
            }
        }
        if(rowIsReduced == 1)
        {
            //now start the reducing process
            for(i = 0; i < row; i++)
            {
                printf("\n%d\n", row);
                if(i == focus)
                {
                    i++;
                }
                //skip the row we are focusing on
                //first check if we even need to manipulate the row
                if(array[i][focus] != 0)
                {
                    if(array[focus][focus] * array[i][focus] > 0)
                    {
                        printf("\n%d\n", row);
                        //then check if we need to add or subtract
                        //make temp row equal to focus row
                        printf("Column before function: %d", col);
                        makeTwoArraysRowsEqual(tempRow, array, 1, focus, col);
                        // to check order: makeTwoArraysRowsEqual(double arrayMadeEqual[][100], double arrayMadeEqualTo[][100], int rowMadeEqual, int rowMadeEqualTo, int column)
                        printf("\nAfter make rows equal: %d\n", row);
                        //now multiply both rows
                        multiplyFactor = array[i][focus];
                        multiplyRow(tempRow, col, 1, multiplyFactor);//getting stuck here
                        //multiplyRow(double a[][100], int columns, int rowMultiplied, double mupltiplyBy);
                        multiplyFactor = -1 * array[focus][focus];
                        multiplyRow(array, col, i, multiplyFactor);
                        //then add them
                        addRowBetweenArrays(array, tempRow, col, 1, i);
                        printf("Made it to end of the loop");
                    }
                    else //could also use: if(array[focus][focus] * array[i][focus] < 0)
                    {
                        //then check if we need to add or subtract
                        //make temp row equal to focus row
                        //makeRowsEqual(tempRow, array, focus, 1, col); need to switch this to new format
                        //now multiply both rows
                        multiplyRow(tempRow, col, 1, array[i][focus]);
                        multiplyRow(array, col, i, array[focus][focus]);
                        //then add them
                        addRowBetweenArrays(array, tempRow, col, 1, i);
                    }
                    //now the rows are added so there is a zero in the focus column
                    //will need to check if any rows are zero and move them to the bottom, have to figure that out
                    //will also need to divide all the rows by their first non zero term
                }
                printf("\n%d\n", row);//rows are acting weird, getting to be a really big number after the first run
            }
            printf("Made it out of i loop");
        }
    }
    printf("\n\Reduced array:\n");
    printArray(array, row, col);
    return 0;
}

void printArray(double a[][100], int rows, int columns)
{
    int i, j;
    for(j = 0; j < rows; j++)
    {
        for(i = 0; i < columns; i++)
        {
            printf("%6.2lf ", a[j][i]);
        }
        printf("\n");
    }
}

void addRow(double a[][100], int columns, int rowToAdd, int rowAddedTo)
{
    int i;
    for(i = 0; i < columns; i++)
    {
        a[rowAddedTo][i] += a[rowToAdd][i];
    }
    return 0;
}

void addRowBetweenArrays(double arrayAddedTo[][100],double arrayAdded[][100], int columns, int rowToAdd, int rowAddedTo)
{
    int i;
    for(i = 0; i < columns; i++)
    {
        arrayAddedTo[rowAddedTo][i] += arrayAdded[rowToAdd][i];
    }
}

void multiplyRow(double a[][100], int columns, int rowMultiplied, double mupltiplyBy)
{
    int i;
    for(i = 0; i < columns; i++)
    {
        a[rowMultiplied][i] *= mupltiplyBy;
    }
}

void switchRows(double a[][100], int columns, int rowOne, int rowTwo)
{
    int i, temp;
    for(i = 0; i < columns; i++)
    {
        temp = a[rowOne][i];
        a[rowOne][i] = a[rowTwo][i];
        a[rowTwo][i] = temp;
    }
}
/*
void makeRowsEqual(double rowMadeEqual[][100], double madeEqualTo[][100], int rowInMadeEqualTo, int rowInMadeEqual, int columns)
{
    int i;
    printf("In function: made = to: %d, made =: %d", rowInMadeEqualTo, rowInMadeEqual);
    for(i = 0; i < columns; i++) {
        rowMadeEqual[rowInMadeEqual][i] = madeEqualTo[rowInMadeEqualTo][i];
    }
    printf("\nMade equal:\n");
    printArray(rowMadeEqual, 1, columns);
        printf("\nMade equal to:\n");
    printArray(madeEqualTo, 2, columns);
}*/

void makeTwoArraysRowsEqual(double arrayMadeEqual[][100], double arrayMadeEqualTo[][100], int rowMadeEqual, int rowMadeEqualTo, int column)
{
    int i;
    for(i = 0; i < column; i++) {
        arrayMadeEqual[rowMadeEqual][i] = arrayMadeEqualTo[rowMadeEqualTo][i];
    }
    printf("Column in function: %d", column);
}

Here is a sample output:

Enter the size of your array (rows columns): 2 2
Enter row 1: 1
2

Enter row 2: 2
4



Unreduced array:
  1.00   2.00
  2.00   4.00

2

2
Column before function: 2Column in function: 2
After make rows equal: 1072693248
Made it to end of the loop
1072693248

1072693248

1072693248
Column before function: 0Column in function: 0
After make rows equal: 1072693248
Made it to end of the loop
1072693248

1072693248

1072693248
Column before function: 0Column in function: 0
After make rows equal: 1072693248
Made it to end of the loop
1072693248

1072693248

Process returned -1073741819 (0xC0000005)   execution time : 3.808 s
Press any key to continue.

P.S. Sorry if this question is formatted poorly or something, this is my first question and I've hardly used stack overflow as of right now.

c
arrays
function
asked on Stack Overflow Dec 19, 2019 by B0b1 • edited Dec 19, 2019 by Barmar

1 Answer

0

The problem is this line:

makeTwoArraysRowsEqual(tempRow, array, 1, focus, col);

The third argument is used as the row index in tempRow to copy to. tempRow is declared:

double tempRow[1][100];

Since it only has 1 row, the highest row index is 0, but you're copying to row 1, which is outside the array, resulting in undefined behavior.

You should use row index 0:

makeTwoArraysRowsEqual(tempRow, array, 0, focus, col);

You have similar problems in the calls to other functions that use tempRow. They all pass 1 when it should be 0.

It's not clear why tempRow even needs to be 2-dimensional, since it only has one row. Just make it a 1-dimensional array, and remove the row index from all the functions that use it.

answered on Stack Overflow Dec 19, 2019 by Barmar

User contributions licensed under CC BY-SA 3.0