C/C++ Solve Matrix w/ C++: Row-Echelon Form

  • Thread starter Thread starter greeniguana00
  • Start date Start date
  • Tags Tags
    C++ Matrix
AI Thread Summary
The discussion centers around a C++ code snippet designed to solve matrices by transforming them into a row-echelon form. Key functionalities include outputting the matrix, switching rows, multiplying rows, adding rows, and adding multiples of rows. The code avoids global variables and aims for expandability. Users are prompted to input matrix dimensions and values, after which the program processes the matrix to achieve row reduction. However, there are concerns regarding the reliability of the code, with suggestions to use established libraries like the GNU Scientific Library (GSL) for more rigorous and tested solutions.
greeniguana00
Messages
52
Reaction score
0
I just thought I'd post this here. It will help you solve a matrix by getting it into row-echelon form (well, close to it anyway). I avoided global variables and tried to make it as expandable as possible.

Code:
#include <iostream>
using namespace std;

void output(int height, int width, long int * matrix)
{
	cout<<"__________________"<<endl;
	long int (*matrix2)[width];
	matrix2=(long int (*)[width]) matrix;
	int row_temp;
	int column_temp;
	for (row_temp=0;row_temp<height;row_temp++) {
		for (column_temp=0;column_temp<width;column_temp++) {
			cout<<matrix2[row_temp][column_temp]<<" ";
		}
		cout<<endl;
	}
}

void switch_rows(int row1, int row2, int width, long int * matrix)
{
	long int (*matrix2)[width];
	matrix2=(long int (*)[width]) matrix;
	int temp;
	int column_temp;
	for (column_temp=0;column_temp<width;column_temp++) {
		temp=matrix2[row1][column_temp];
		matrix2[row1][column_temp]=matrix2[row2][column_temp];
		matrix2[row2][column_temp]=temp;
	}
}

void multiply_row(int row, long int multiple, int width, long int * matrix)
{
	long int (*matrix2)[width];
	matrix2=(long int (*)[width]) matrix;
	int column_temp;
	for (column_temp=0;column_temp<width;column_temp++) {
		matrix2[row][column_temp]=matrix2[row][column_temp]*multiple;
	}
}

void add_rows(int row1, int row2, int width, long int * matrix)
{
	long int (*matrix2)[width];
	matrix2=(long int (*)[width]) matrix;
	int temp;
	int column_temp;
	for (column_temp=0;column_temp<width;column_temp++) {
		matrix2[row1][column_temp]=matrix2[row1][column_temp]+matrix2[row2][column_temp];
	}
}

void add_multiple_of_row(int row1, int row2, long int multiple, int width, long int * matrix)
{
	long int (*matrix2)[width];
	matrix2=(long int (*)[width]) matrix;
	int temp;
	int column_temp;
	for (column_temp=0;column_temp<width;column_temp++) {
		matrix2[row1][column_temp]=matrix2[row1][column_temp]+(matrix2[row2][column_temp]*multiple);
	}
}

int reduce_column(int column, int block_rows_until, int height, int width, long int * matrix)
{
	long int (*matrix2)[width];
	matrix2=(long int (*)[width]) matrix;
	int row_temp;
	int column_temp;
	int temp;
	int selected_row=-1;
	for (row_temp=(height-1);row_temp>=block_rows_until;row_temp--) {
		if (matrix2[row_temp][column]!=0) {
			selected_row=row_temp;
		}
	}
	if (selected_row==-1) {
		return block_rows_until;
	}
	switch_rows(block_rows_until, selected_row, width, matrix);
	for (row_temp=block_rows_until+1;row_temp<height;row_temp++) {
		if (matrix2[row_temp][column]!=0) {			
			output(height, width, matrix);
			temp=matrix2[row_temp][column];
			multiply_row(row_temp, matrix2[block_rows_until][column], width, matrix);
			output(height, width, matrix);
			add_multiple_of_row(row_temp, block_rows_until, -(temp), width, matrix);
		}
	}
	block_rows_until++;
	return block_rows_until;
	
}

int main()
{
	int width;
	int height;
	cout<<"Height? ";
	cin>>height;
	cout<<"Width? ";
	cin>>width;
	cout<<"Enter in the matrix row by row with entries separated by space:"<<endl;
	long int matrix[height][width];
	int row_temp;
	int column_temp;
	for (row_temp=0;row_temp<height;row_temp++) {
        	for (column_temp=0;column_temp<width;column_temp++) {
			cin>>matrix[row_temp][column_temp];
		}
	}
	long int * matrix_transfer;
	matrix_transfer=(long int *) matrix;
	int current_row=0;
	int current_column=0;
	int solved=0;
	while (solved!=1) {
		current_row=reduce_column(current_column, current_row, height, width, matrix_transfer);
		current_column++;
		if (current_row==(height-1)) {solved=1;}
		if (current_column==(width-1)) {solved=1;}
	}
	output(height, width, matrix_transfer);
	return 0;
}
 
Technology news on Phys.org
I'm not sure I would use such code posted randomly on the internet. As this code been rigorously tested?

I would rather use an established library, such as GSL.
https://www.gnu.org/software/gsl/
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Back
Top