r/cprogramming • • Jun 09 '23

Writing an algorithm to reduce matrices to their echelon form and I am stuck on this bit

void Reduce(int row, int col, double mat[][10])

{

for(int i=0; i<row; i++){

for(int j=0; j<col; j++){

mat[i][j]=(mat[i][j])/(mat[i][0]);

}

}

}

Good day. I have been working on this algorithm as a way to distract myself from studying but I am stuck on this function. The purpose of this function is the divide a whole row with the value of the first column in each row. The math seems sound to me but only the first value of each row is divided giving me 1s in the first column but without changing the values in subsequent columns.

I have attached the rest of the code below but this part up here is the important part. All other parts have been test and is working flawlessly

#include <stdio.h>

void test_print(double a[][10], int x, int y);

void INIT_Mat(double a[][10]);

void Get_row_col(int *a, int *b);

void Matrix_populate(int row, int col, double a[][10]);

void Division_numbers(int row, int col, double mat[][10], double nums[10]);

void test_print_1d(int row, double mat[10]);

void Reduce(int row, int col, double mat[][10]);

int main()

{

double Matrix[10][10];

double div_nums[10];

int row, col;

INIT_Mat(Matrix);

Get_row_col(&row, &col);

Matrix_populate(row, col, Matrix);

Reduce(row, col, Matrix);

test_print(Matrix, row, col);

return 0;

}

void Reduce(int row, int col, double mat[][10])

{

for(int i=0; i<row; i++){

for(int j=0; j<col; j++){

mat[i][j]=(mat[i][j])/(mat[i][0]);

}

}

}

void test_print_1d(int row, double mat[10])

{

for(int i=0; i<row; i++){

printf("%lf", mat[i]);

}

}

void Division_numbers(int row, int col, double mat[][10], double nums[10])

{

for(int i=0; i<row; i++){

nums[i]=mat[i][0];

}

}

void Matrix_populate(int row, int col, double a[][10])

{

for(int i=0; i<row; i++){

for(int j=0; j<col; j++){

printf("Enter value %d_%d: ", i+1, j+1);

scanf("%lf", &a[i][j]);

}

printf("\n");

}

}

void Get_row_col(int *a, int *b)

{

printf("Enter row count: ");

scanf("%d", &*a);

printf("Enter column count: ");

scanf("%d", &*b);

}

void test_print(double a[][10], int x, int y) //Test output

{

printf("Rows: %d\nColumns: %d\n", x, y);

for(int i=0; i<x; i++){

for(int j=0; j<y; j++){

printf("%.2lf ", a[i][j]);

}

printf("\n");

}

}

void INIT_Mat(double a[][10]) //This is used to initialize all values to 1

{

for(int i=0; i<10; i++){

for(int j=0; j<10; j++){

a[i][j]=1;

}

}

}

///////

0 Upvotes

8 comments sorted by

4

u/Bitwise_Gamgee Jun 09 '23

In the Reduce() function, you're dividing every element in the row by the first element of that row. But, the first element of each row is updated to 1. When other elements in the same row are processed, they get divided by 1 (which is the updated value of the first element), leaving them unchanged. Consider storing the first element of the row in a temporary variable.

void Reduce(int row, int col, double mat[][10])
{
	for(int i=0; i<row; i++){
		double divisor = mat[i][0];
		for(int j=0; j<col; j++){
			mat[i][j]=(mat[i][j])/divisor;
		}
	}
}

2

u/SmoothBeanMan Jun 09 '23

Of yes of course thank you so much. I did store it in a separate array at first but it seemed wasteful. Thanks

2

u/SmoothBeanMan Jun 09 '23

And I realized this makes the function more easy to implement in a loop structure later on for the other reductions

1

u/Plane_Dust2555 Jun 09 '23

What if any elements of the first "column" is zero?

1

u/Bitwise_Gamgee Jun 09 '23

You'd implement a Gaussian elimination function and refactor the matrix via operations such as swapping, scaling, subtracting or even just pivoting.

Matrix operations are themselves an interesting subset of mathematical computer science.

2

u/SmoothBeanMan Jun 09 '23

Oh no the code was fomatted when posting but it seems to have lost it's shape now

2

u/weregod Jun 09 '23

Use 4 spaces as indentation. And add one extra level of indentation at each line, including empty lines and lines without indentation.

2

u/IamImposter Jun 10 '23

You already have the answer so I'll just give general purpose suggestion:

  • learn to use a debugger like gdb, the one in vscode or visual studio. It makes it very easy to step through the code and see the result of every statement

  • if for some reason you do not want to use debugger (although you should, it's an important skill to learn), add print statements in your code and print the value of the relevant variables at key points. That way you can pin point where the problem started and then fixing it is not as big a deal