Home > Blockchain >  Multiplying 2 matrices using pointers in C
Multiplying 2 matrices using pointers in C

Time:11-16

I wrote this code that inputs 2 matrices and prints the product after multiplying the 2 matrices using pointers. But when I enter the first element I am getting segmentation fault. I have tried everything with pointers but I think this is some loop error as it says segmentation fault. Please help

#include<stdio.h>
#include<stdlib.h>
#define N 10

void readMatrix(float *arrp, int rows, int cols)
{
  for (int i=0; i<rows; i  )
  {
    for (int j=0; j<cols; j  )
    {
        printf("Enter element [%d][%d] : ",i 1,j 1);
        scanf("%f",&*(arrp   i)   j);
    }
  }
  return;
}

void printMatrix(float *arrp, int rows, int cols)
{
  for (int i=0; i<rows; i  )
  {
    for (int j=0; j<cols; j  ) printf("%.2f\t",*((arrp   i)   j));
    printf("\n");
  }
  return;
}

int main()
{
    float *ap, *bp, *cp;
    int arows, brows, acols, bcols;

    printf("Give no. of rows of matrix A (<=%d) = ",N);
    scanf("%d",&arows);
    printf("Give no. of columns of matrix A (<=%d) = ",N);
    scanf("%d",&acols);
    if (arows>N || acols>N || arows<1 || acols<1)
    {
        printf("Illegal rows/cols = %d/%d. Should be <=%d.\n", arows, acols, N);
        exit(0);
    }
    readMatrix(ap, arows, acols); printf("\n");
    printf("Matrix A :\n"); printMatrix(ap, arows, acols); printf("\n");

    printf("Give no. of rows of matrix B (<=%d) = ", N);
    scanf("%d",&brows);
    printf("Give no. of columns of matrix B (<=%d) = ", N);
    scanf("%d",&bcols);
    if (brows>N || bcols>N || brows<1 || bcols<1)
    {
        printf("Illegal rows/cols = %d/%d. Should be <=%d.\n", brows, bcols, N);
        exit(0);
    }

    if (acols==brows)
    {
        readMatrix(bp, brows, bcols); printf("\n");
        printf("Matrix B :\n"); printMatrix(bp, brows, bcols); printf("\n");

        for (int i=0; i<arows; i  )
        {
            for (int j=0; j<bcols; j  )
            {
              float sum=0.0;
              for (int k=0; k<acols; k  ) sum  = *((ap   i)   k) * *((bp   k)   j);
              *((cp   i)   j) = sum;
            }
        }

        printf("After Multiplication, Matrix C :\n"); printMatrix(cp, arows, bcols);
    }
    else printf("\nIncompatible matrices\nColumns of matrix A should be equal to rows of matrix B\n");
    exit(0);
}

CodePudding user response:

Your program never assigns any values to ap, bp, or cp. You need to reserve memory for the data in some way and set the pointers to point to it.

Additionally, *((ap i) k) is equivalent to *(ap (i k)). It merely adds i and k. The program needs to compute where the element in row i and column k is. If ap is a pointer to float, you need to multiply i by the number of columns. If ap is a pointer to an array, you need to insert another operator in there—go back to your source material for the class and see what is different about this expression than what the book or the instructor taught.

  • Related