segmentation fault : matrix multiplication ??

This is a discussion on segmentation fault : matrix multiplication ?? within the C Programming forums, part of the General Programming Boards category; Hi ! I tried to compile this program on red hat 9.0 but I keep getting segementation fault when I ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    91

    segmentation fault : matrix multiplication ??

    Hi !

    I tried to compile this program on red hat 9.0 but I keep getting segementation fault when I run the output. The program multiplies two matricies and is very simple one :


    Code:
    #include<stdio.h>
    
    #include<stdlib.h>
    
    
    
    int main()
    
    {
    
    	int **A, **B, **C, row1, row2, col1, col2, sum, i, j, k;
    
    	printf("Enter the rows and columns of matrices:\n");
    
    	printf("Matrix one row and columns\n");
    
    	scanf("%d %d", &row1, &col1);
    
    	printf("Matrix two row and columns\n");
    
    	scanf("%d %d", &row2, &col2);
    
    	if(col1 != row2)
    
    	{
    
    		printf("Matrices cannot be multiplied\n");
    
    		exit(1);
    
    	}
    
    	A = malloc(row1*sizeof(int*));
    
    	B = malloc(row2*sizeof(int*));
    
    	C = malloc(row1*sizeof(int*));
    
    	for(i=0; i<row1; i++)
    
    		A[i] = malloc(col1*sizeof(int));
    
    	for(i=0; i<row2; i++)
    
    		B[i] = malloc(col2*sizeof(int));
    
    	for(i=0; i<row1; i++)
    
    		C[i] = malloc(col2*sizeof(int));
    
    	printf("Enter elements for first matrix row by row\n");
    
    	for(i=0; i<row1; i++)
    
    		for(j=0; j<col1; j++)
    
    			scanf("%d", &A[i][j]);
    
        printf("Enter elements for second matrix row by row\n");
    
    	for(i=0; i<row2; i++)
    
    		for(j=0; j<col2; j++)
    
    
    
    
    /*---------MULTIPLICATION------------------------- */
    
    
    
    
    
    	for(i=0; i<row1; i++)
    
    	{
    
    		for(j=0; j<col2; j++)
    
    		{
    
    			sum = 0;
    
    			for(k=0; k<col1; k++)
    
    				sum += A[i*k][j]*B[i][j*k];
    
    			C[i][j] = sum;
    
    		}
    
    	}
    
    
    
    	for(i=0; i<row1; i++)
    
    	{
    
    		for(j=0; j<col2; j++)
    
    			printf("%d ", C[i][j]);
    
    		printf("\n");
    
    	}
    	return 0;
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    printf("Enter elements for second matrix row by row\n");
    
    	for(i=0; i<row2; i++)
    
    		for(j=0; j<col2; j++)
    
    
    
    
    /*---------MULTIPLICATION------------------------- */
    
    
    
    
    
    	for(i=0; i<row1; i++)
    Shouldn't you be actually scanning something in there?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    inserted the line " scanf("%d", &B[i][j]); "

    Still there is a segmentation fault ??????

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Repost your updated code. And how about squeezing a lot of that whitespace out so it takes up less space?

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    Here is the updated code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
    	int **A, **B, **C, row1, row2, col1, col2, sum;
    	printf("Enter the rows and columns of matrices:\n");
    	printf("Matrix one row and columns\n");
    	scanf("%d %d", &row1, &col1);
    	printf("Matrix two row and columns\n");
    	scanf("%d %d", &row2, &col2);
    	if(col1 != row2)
    	{
    		printf("Matrices cannot be multiplied\n");
    		exit(1);
    	}
    	A = malloc(row1* sizeof(int*));
    	B = malloc(row2* sizeof(int*));
    	C = malloc(row1* sizeof(int*));
    	for(int i=0; i<row1; i++)
    		A[i] = malloc(col1* sizeof(int));
    	for(i=0; i<row2; i++)
    		B[i] = malloc(col2* sizeof(int));
    	for(i=0; i<row1; i++)
    		C[i] = malloc(col2* sizeof(int));
    	printf("Enter elements for first matrix row by row\n");
    	for(i=0; i<row1; i++)
    		for(int j=0; j<col1; j++)
    			scanf("%d", &A[i][j]);
        printf("Enter elements for second matrix row by row\n");
    	for(i=0; i<row2; i++)
    		for(int j=0; j<col2; j++)
    			scanf("%d", &B[i][j]);
    //-------------------------------- MULTIPLICATION ------------------------------------------------------
    
    
    	for(i=0; i<row1; i++)
    	{
    		for(int j=0; j<col2; j++)
    		{
    			sum = 0;
    			for(int k=0; k<col1; k++)
    				sum += A[i*k][j]*B[i][j*k];
    			C[i][j] = sum;
    		}
    	}
    
    	for(i=0; i<row1; i++)
    	{
    		for(int j=0; j<col2; j++)
    			printf("%d ", C[i][j]);
    		printf("\n");
    	}
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    well, it seems to work fine for me, and i am using Dev-c++ compiler. i have made some changes as well

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
    	int **A, **B, **C, row1, row2, col1, col2, sum;
    	int i,j,k;
    	printf("Enter the rows and columns of matrices:\n");
    	printf("Matrix one row and columns\n");
    	scanf("%d %d", &row1, &col1);
    	printf("Matrix two row and columns\n");
    	scanf("%d %d", &row2, &col2);
    	if(col1 != row2)
    	{
    		printf("Matrices cannot be multiplied\n");
    		exit(1);
    	}
    	A = malloc(row1* sizeof(int*));
    	B = malloc(row2* sizeof(int*));
    	C = malloc(row1* sizeof(int*));
    	for(i=0; i<row1; i++)
    		A[i] = malloc(col1* sizeof(int));
    	for(i=0; i<row2; i++)
    		B[i] = malloc(col2* sizeof(int));
    	for(i=0; i<row1; i++)
    		C[i] = malloc(col2* sizeof(int));
    	printf("Enter elements for first matrix row by row\n");
    	for(i=0; i<row1; i++)
    		for( j=0; j<col1; j++)
    			scanf("%d", &A[i][j]);
        printf("Enter elements for second matrix row by row\n");
    	for(i=0; i<row2; i++)
    		for(j=0; j<col2; j++)
    			scanf("%d", &B[i][j]);
    //-------------------------------- MULTIPLICATION ------------------------------------------------------
    
    
    	for(i=0; i<row1; i++)
    	{
    		for( j=0; j<col2; j++)
    		{
    			sum = 0;
    			for( k=0; k<col1; k++)
    				sum += A[i*k][j]*B[i][j*k];
    			C[i][j] = sum;
    		}
    	}
    
    	for(i=0; i<row1; i++)
    	{
    		for( j=0; j<col2; j++)
    			printf("%d ", C[i][j]);
    		printf("\n");
    	}
    	
    	fflush(stdin);
    	getchar();
    	return 0;
    }
    
    /*my ouput
    Enter the rows and columns of matrices:
    Matrix one row and columns
    2
    2
    Matrix two row and columns
    2
    2
    Enter elements for first matrix row by row
    2
    2
    2
    2
    Enter elements for second matrix row by row
    2
    2
    2
    2
    8 8
    8 8
    */
    have declared all the varibales which are used in the loop at the beginning of the main,

    what compiler and OS are using ??

    s.s.harish

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    > sum += A[i*k][j]*B[i][j*k];
    Isn't i*k going to result in some very large values - much larger than the values you've allocated for?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    	for(i=0; i<row1; i++)
    	{
    		for( j=0; j<col2; j++)
    		{
    			sum = 0;
    			for( k=0; k<col1; k++)
    				sum += A[i*k][j]*B[i][j*k];
    			C[i][j] = sum;
    		}
    	}
    http://cboard.cprogramming.com/showthread.php?p=409919
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    Now that's spooky Dave
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    sorry for my dumbness but I still didn't understand what is going on ?

    This is following an algorithm in book and it seems to work fine on dev C++ but gives me segementation fault in red hat gcc compiler

  11. #11
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    my sincere apologies for my dumbness and the stupidity of the text , I researched the algorithm and found that it works only for square matricies, hence both matricies should have equal number of rows and colums .

    The program works fine it u enter a 2 X 2 matrix, but surprisingly when i enter a 3 X 3 matrix it gives a crazy result with the last row or element messed up and a 4 X 4 matrix gives a segmentation fault ?????

    I ran these on gcc compiler in red hat 9.0.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    > sorry for my dumbness but I still didn't understand what is going on ?
    Which bit?
    The "why it crashes on one machine and not another"?
    Because programs are not obliged to crash at the first moment you make a mistake. Life would be a lot simpler if it did, because then you'd get rid of all those bugs.

    Well think about how big your array indices get.
    Code:
    			for( k=0; k<col1; k++) {
    				int p = i * k, q = j * k;
    				printf( "New indices = %d %d\n", p, q );
    				sum += A[p][j]*B[i][q];
    			}
    Now do THOSE look sensible to you?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a matrix - Segmentation fault
    By endomlic in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 01:24 AM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-14-2004, 12:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21