English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Other

C Language Reference Manual

The C program passes the matrix to the function for multiplication

Comprehensive Collection of C Programming Examples

In this example, you will learn to multiply two matrices and use user-defined functions to display them.

To understand this example, you should understand the followingC Programming LanguageTopic:

The program requires the user to enter the size of the matrix (rows and columns).

Then, it prompts the user to enter the elements of these matrices, and finally adds and displays the result.

To perform this task, three functions need to be executed:

  1. To get matrix elements from the user - enterData()

  2. Multiply two matrices -  multiplyMatrices()

  3. Display the result matrix after multiplication - display()

Example: Multiply matrices by passing them to a function

#include <stdio.h>
void enterData(int firstMatrix[10], int secondMatrix[10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
void multiplyMatrices(int firstMatrix[10], int secondMatrix[10], int multResult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
void display(int mult[][10], int rowFirst, int columnSecond);
int main()
{
	int firstMatrix[10]10], secondMatrix[10]10], mult[10]10], rowFirst, columnFirst, rowSecond, columnSecond, i, j, k;
	printf("Enter the rows and columns of the first matrix: ");
	scanf("%d %d", &rowFirst, &columnFirst);
	printf("Enter the rows and columns of the second matrix: ");
	scanf("%d %d", &rowSecond, &columnSecond);
	//If the columns of the first matrix do not equal the rows of the second matrix, the user is prompted to re-enter the size of the matrix.
	while (columnFirst != rowSecond)
	{
		printf("Error! The columns of the first matrix do not equal the second row.\n");
		printf("Enter the rows and columns of the first matrix: ");
		scanf("%d%d", &rowFirst, &columnFirst);
		printf("Enter the rows and columns of the second matrix: ");
		scanf("%d%d", &rowSecond, &columnSecond);
	}
	//Get matrix data function
        enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond, columnSecond);
        //Function used to multiply two matrices.
        multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond, columnSecond);
        //Function to display the resulting matrix after multiplication
        display(mult, rowFirst, columnSecond);
	return 0;
}
void enterData(int firstMatrix[10], int secondMatrix[10], int rowFirst, int columnFirst, int rowSecond, int columnSecond)
{
	int i, j;
	printf("\nInput matrix elements 1:\n");
	for(i = 0; i < rowFirst; ++i)
	{
		for(j = 0; j < columnFirst; ++j)
		{
			printf("Input element a%d%d: ", i + 1, j + 1);
			scanf("%d", &firstMatrix[i][j]);
		}
	}
	printf("\nInput matrix elements 2:\n");
	for(i = 0; i < rowSecond; ++i)
	{
		for(j = 0; j < columnSecond; ++j)
		{
			printf("Input element b%d%d: ", i + 1, j + 1);
			scanf("%d", &secondMatrix[i][j]);
		}
	}
}
void multiplyMatrices(int firstMatrix[10], int secondMatrix[10], int mult[10], int rowFirst, int columnFirst, int rowSecond, int columnSecond)
{
	int i, j, k;
	//Initialize elements of matrix mult to 0.
	for(i = 0; i < rowFirst; ++i)
	{
		for(j = 0; j < columnSecond; ++j)
		{
			mult[i][j] = 0;
		}
	}
	//Multiply matrix firstMatrix and secondMatrix and store the result in array mult.
	for(i = 0; i < rowFirst; ++i)
	{
		for(j = 0; j < columnSecond; ++j)
		{
			for(k = 0; k < columnFirst; ++k)
			{
				mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
			}
		}
	}
}
void display(int mult[][10], int rowFirst, int columnSecond)
{
	int i, j;
	printf("\nOutput matrix:\n");
	for(i = 0; i < rowFirst; ++i)
	{
		for(j = 0; j < columnSecond; ++j)
		{
			printf("%d	", mult[i][j]);
			if(j == columnSecond - 1)}}
				printf("\n\n");
		}
	}
}

Output Result

Enter the number of rows and columns for the first matrix: 3
2
Enter the number of rows and columns for the second matrix: 3
2
Error! The columns of the first matrix are not equal to the rows of the second matrix.
Enter the number of rows and columns for the first matrix: 2
3
Enter the number of rows and columns for the second matrix: 3
2
Enter matrix elements 1:
Enter element a11: 3
Enter element a12: -2
Enter element a13: 5
Enter element a21: 3
Enter element a22: 0
Enter element a23: 4
Enter matrix elements 2:
Enter element b11: 2
Enter element b12: 3
Enter element b21: -9
Enter element b22: 0
Enter element b31: 0
Enter element b32: 4
Output Matrix:
24  29
6  25

Comprehensive Collection of C Programming Examples