Write the MULTI_MATRIX function which multiplies the matrix MAT1 by an integer X: MAT1 = X * MAT1

Choose the necessary parameters and write a small program that tests the MULTI_MATRIX function.


Difficulty level
Video recording
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
#define N 100

int READ_NB_ROWS(int Rmax)
{
	int R;
	do
	{
		printf("Number of rows    (max.%d) : ", Rmax);
		scanf("%d", &R);
	} while (R<0 || R>Rmax);
	return R;
}

int READ_NB_COLUMNS(int Cmax)
{
	int C;
	do
	{
		printf("Number of columns (max.%d) : ", Cmax);
		scanf("%d", &C);
	} while (C<0 || C>Cmax);
	return C;
}

void READ_MATRIX(int MAT[][N], int R, int C)
{
	int I, J;
	for (I = 0; I<R; I++)
		for (J = 0; J<C; J++)
		{
			printf("Element[%d][%d] : ", I, J);
			scanf("%d", &MAT[I][J]);
		}
}

void PRINT_MATRIX(int MAT[][N], int R, int C)
{
	int I, J;
	for (I = 0; I < R; I++)
	{
		for (J = 0; J < C; J++)
			printf("%7d", MAT[I][J]);
		printf("\n");
	}
}

void MULTI_MATRIX(int X, int MAT[][N], int R, int C)
{
	int I, J;
	for (I = 0; I<R; I++)
		for (J = 0; J<C; J++)
			MAT[I][J] *= X;
}

void main()
{
	int Mat[N][N], R, C;
	int X;

	R = READ_NB_ROWS(N);
	C = READ_NB_COLUMNS(N);
	READ_MATRIX(Mat, R, C);
	PRINT_MATRIX(Mat, R, C);

	printf("Enter X (integer) : ");
	scanf("%d", &X);

	MULTI_MATRIX( X, Mat, R, C);

	PRINT_MATRIX(Mat, R, C);
	getch();
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Recursively reverse a stack