I am trying to now multiply matrices stored in 2d arrays. When the program runs i get segmentation fault, using DDD i found the point where it stops after 3 loops.

int i,j,k;
	float** matrixc = new float*[rows];
		
		if (matrixc != NULL) 
		{
			for (int x = 0; x < rowsa; x++)
			{
				matrixc[x] = new float[columnsb];
  			}
		}

	for (i = 1; i <= rowsa; i++)
	{
		for (j = 1; j <= columnsb; j++)
		{
			float sum = 0;
			for (k = 1; k <= rowsb; k++)
			{
	*/seg fault  occurs here*/  sum = sum + matrixa[i][k] * matrixb[k][j];
			}
			matrixc[i][j] = sum;
			cout << " " <<  sum[i][j];
		}
		cout << endl;
	}

This is my code, there isnt anything wrong with it to my knowledge, and i cant determine why i get a seg fault

How big are the matrices?
What values do rowsa and columnsb have?

The segmentation fault probably has something to do with you getting out of array-bounds, but without the complete code, I can't tell.

ahh i see, well the thing is the size is dynamic, so there size can vary. I think i have the wrong variables in there.

#include <iostream>
#include <fstream>
#include "matrix-2.h"
using namespace std;

int main ()
{
	char filename[10];

	int rows;
	int columns;
	

	int finalcolumn;
	int finalrows;
	
	float** x = NULL;				//creating pointer
	x = readmatrix(filename, rows, columns);
			
	if (x == NULL)					
	{
		cout << "There was an error" << endl;
	}

	float** matrixa = new float*[rows];		//declaring matrixa
			
	for (int i = 0; i < rows; i++)			//copy data into matrixa
	{
		for (int j = 0; j < columns; j++)
		{
			matrixa[i][j] = x[i][j];
		}
	}

	printmatrix(x, columns, rows);			//print matrix a

	int rowsa = rows;				
	int columnsa = columns; 

	
				
	delete [] x;
						
	float** y = NULL;				//creaint pointer
	y = readmatrix(filename, rows, columns);
			
	if (y == NULL)					//if error occurs
	{
		cout << "There was an error" << endl;
	}

	float** matrixb = new float*[rows];		//declaring matrixb
			
	for (int a = 0; a < rows; a++)				
	{
		for (int b = 0; b < columns; b++)	//copy data into matrixb
		{
			matrixb[a][b] = y[a][b];
		}
	}

	printmatrix(y, columns, rows);			//print matrixb

	int rowsb = rows;				
	int columnsb = columns;		
					
	delete [] y;						
	float** z = NULL;
	z = mult(matrixa, rowsa, columnsa, matrixb, rowsb, columnsb, finalcolumn, finalrows);
	printmatrix(z, columns, rows);
	return 0;
}
#include <iostream>
#include <fstream>
#include "matrix-2.h"
using namespace std;

float** readmatrix(char filename[], int& rows, int& columns)
{
	cout << "Enter filename to open: " << endl;
	cin >> filename;

	ifstream ins;
	ins.open(filename, ios::in | ios::out);

	if(ins.good())
	{	
		ins >> rows >> columns;			//read in rows and columns

		float** matrix = new float*[rows];	//create matrix
		
		if (matrix != NULL) 
		{
			for (int i = 0; i < rows; i++)		
			{
				matrix[i] = new float[columns];
  			}
		}


		for (int i = 0; i < rows; i++)			//read in data
		{
			for (int j = 0; j < columns; j++)
			{
			ins >> matrix[i][j];
			}		
		}
		return matrix;				//return the matrix
		delete [] matrix;			//delete the memory
	}
	else
	{	
		return NULL;
	}
	
	ins.close();
	
	return 0;
}


void printmatrix(float* matrix[], int columns, int rows)
{
	
	for(int i = 0; i < rows; i++)			//print function
	{
		for(int j = 0; j < columns; j++)
		{
			cout << " " <<  matrix[i][j];
		}
		cout << endl;
	}
}

float** mult(float* matrixa[], int rowsa, int columnsa, float* matrixb[], int rowsb, int columnsb, int& finalcolumn, int& rows)
{
	float** matrixc = new float*[rows];			//matrixc for multiplication
		
		if (matrixc != NULL) 
		{
			for (int x = 0; x < rowsa; x++)
			{
				matrixc[x] = new float[columnsb];
  			}
		}

	for (int i = 1; i <= rowsa; i++)			//multiply matrix
	{
		for (int j = 1; j <= columnsb; j++)		
		{
			
			for (int k = 1; k <= rowsb; k++)
			{
			matrixc[i][j] = matrixc[i][j] + matrixa[i][k] * matrixb[k][j];		//seg fault here, dont know why
			}									
			cout << " " <<  matrixc[i][j];						
		}
		cout << endl;
	}
	return matrixc;


	return 0;
}

Thats my code, i also have another problem, once i read in 1 file, then i go to read in another file i get seg fault again. I have no ideas why the seg fault occurs, the code seems to be fine, compiles without any warnings or errors.

[edit]. Aha there's the rest of the code. Give me a minute

You loop from array-element 1-x, where you should loop from element 0-(x-1). The result is a segmentation-error because you go out of array bounds.

change: for (i = 1; i <= rowsa; i++) to: for (i = 0; i < rowsa; i++) Do the same with the other 2 loops in that function.

Hey thanks for that, i didnt realise, but i still get a seg fault. I dont see why it is doing it, the code on there looks legit, it may be due to another seg fault which occurs when reading in.

For example if i read in matrix_a and matrix_b. If matrix_b has larger dimensions than matrix_a i get a seg fault. However if i read them in with the larger matrix first, there is no seg fault. Any ideas?

matrixc[i][j] = matrixc[i][j] + matrixa[i][k] * matrixb[k][j];

You use the index(k) for the colums from matrix a but also for rows of matrix b. This will cause segmentation faults.

What are you trying to multiply, can you give an example of a matrix A and B and your desired output C?

matrix a:
1 2 3
4 5 6
7 8 9

matrix b;
1 2
3 4
5 6

matric c sould equal
22 28
49 64
76 100

The problem i have is if i read in matrix a, and then b, when i go to read in b i get a seg fault. If i read in b then a, they read in, and then i get a seg fault in the multiplication.

Also k needs to be used in the specific way, thats how it works mathematically, and all examples of multiplying matrices i have work in that way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.