Hello. Please, i have this little question of finding the product of two matrices in which the user enters the number of rows and columns. i would appreciate it if a solution is given and even more if there are more than one solutions to the question. thanks.

Recommended Answers

All 5 Replies

I know you need your implementation in C but I hope this program in Python that I created is somewhat helpful.

# -------------------------------------------------------------------
# Function: init
# Purpose: Program's initialization function (i.e. Point of entry)
#  ------------------------------------------------------------------
def init():
    
    print("")
    print("|-----| Matrix Multiplier |-----|")
    print("")
    print("Programmed by: Carlos Felipe Perea")
    print("This program is distributed in accordance to the GNU GPL License version 3")
    print("")
    print("Purpose: Multiplies two matrices and prints the result on screen")
    print("To call this program again, use the 'init' function")
    print("")
    print("IMPORTANT: The number of columns in the first matrix must eqaul")
    print("the number of rows in the second matrix")
    print("")
    
    matrix_A = []
    matrix_B = []
    result_matrix = []
    
    # Ask the user for the dimensions of matrix A and matrix B
    try:
        matrix_A_row = int(input("Insert the number of rows for matrix A: "))
        matrix_A_col = int(input("Insert the number of columns for matrix A: "))
    
        matrix_B_row = int(input("Insert the number of rows for matrix B: "))
        matrix_B_col = int(input("Insert the number of columns for matrix B: "))
        print("")
    except:
        print("Error: The value must be a positive integer")
        return []
    
    # If the number of columns in matrix A is not equal to the number of rows in column B
    # then print an error message on screen and escape
    if matrix_A_col != matrix_B_row:
        
        print("Error: The number of columns in matrix A must be equal to the number of rows in matrix B")
        return []
    
    # Ask for the elements of each matrix
    i = 0
    while i < matrix_A_row:
        row = []
        ii = 0
        while ii < matrix_A_col:
            row.append(int(input("Matrix A - Row " + str(i + 1) + ", Column " + str(ii + 1) + ": ")))
            ii = ii + 1
            
        matrix_A.append(row)
        i = i + 1
    
        
    displayMatrix(matrix_A, "----- Matrix A -----")
    
    i = 0
    while i < matrix_B_row:
        row = []
        ii = 0
        while ii < matrix_B_col:
            row.append(int(input("Matrix B - Row " + str(i + 1) + ", Column " + str(ii + 1) + ": ")))
            ii = ii + 1
            
        matrix_B.append(row)
        i = i + 1
    
    displayMatrix(matrix_B, "----- Matrix B -----")
    
    # Create an empty matrix with the correct dimensions for the product matrix
    i = 0
    
    while i < matrix_A_row:
        
        row = []
        ii = 0
        
        while ii < matrix_B_col:
            
            row.append(0)
            ii = ii + 1
            
        result_matrix.append(row)
        #print(result_matrix[i])
        i = i + 1
        
    i = 0
    
    # Multiply each row of matrix A times each column of matrix B
    while i < len(result_matrix):
        
        ii = 0
        
        while ii < len(result_matrix[0]):
            
            result_matrix[i][ii] = multiplyRowCol(i, ii, matrix_A, matrix_B)
            ii = ii + 1
            
        i = i + 1
        
    print("")
    displayMatrix(result_matrix, "***** Result *****")
    
    return result_matrix
        
# -------------------------------------------------------------------------------------
# Function: multiplyRowCol
# Purpose: Multiplies matrix A's element at position 'i' times matrix B's elements at
# position 'i' and adds all of this products and returns the result
# -------------------------------------------------------------------------------------
def multiplyRowCol(rowA, colB, matrixA, matrixB):
    
    length = len(matrixA[rowA])
    result = 0
    i = 0
    
    while i < length:
        
        result = result + matrixA[rowA][i] * matrixB[i][colB]
        i = i + 1
        
    return result

# -------------------------------------------------
# Function: displayMatrix
# Purpose: Displays each row of the matrix on a 
# separate line of the shell
# -------------------------------------------------
def displayMatrix(matrix, title = ""):
    
    print("")
    
    if title != "":
        print(title)
        
    i = 0
    while i < len(matrix):
        
        print(matrix[i])
        i = i + 1
        
    print("")
    
# INITIALIZATION ------------------------------------------------
init()

Please i need d code for C.

commented: Read forum rules. We do not give code. -1

Please i need d code for C.

Stop whining and begging. The code he provided is practically C.

commented: Are you kidding? ...And this is practically a good rep. -1

Please i need d code for C.

The rules of this forum clearly explain that we do not give away code to posters that do not show proof of effort. Where's the code? Where is your effort in the matter?

commented: Thanks Aia, I'll remember that next time :) -5

Please i need d code for C.

If you were to just make an effort to translate the code that I gave to you C you would have what you need. Algorithms are practical and applicable to all programming languages given you know the language...which I'm guessing you don't.

commented: And I am guessing you haven't read the rules of this forum -1
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.