Write a program that multiplies an A matrix by a real X.

The result of the multiplication will be memorized in a second matrix B which will then be displayed.

\(X \times \begin{pmatrix} a & b & c & d \\e & f & g & h \\ i & j & k & l \end{pmatrix}= \begin{pmatrix} X \times a & X \times b & X \times c & X \times d \\X \times e & X \times f & X \times g & X \times h \\ X \times i & X \times j & X \times k & X \times l \end{pmatrix}\)


Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>
#define SIZE 50
void main()
{
	float A[SIZE][SIZE];
	float B[SIZE][SIZE];
	int R, C;        /* dimensions */
	int I, J;       
	float X;         
			
	do {
		printf("Number of rows   (max.%d) : ", SIZE);
		scanf("%d", &R);
	} while (R <= 0 || R > SIZE);
	do {
		printf("Number of columns (max.%d) : ", SIZE);
		scanf("%d", &C);
	} while (C <= 0 || C > SIZE);

	for (I = 0; I<R; I++)
		for (J = 0; J<C; J++)
		{
			printf("A[%d][%d] : ", I, J);
			scanf("%f", &A[I][J]);
		}

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


	printf("Matrix :\n");
	for (I = 0; I<R; I++)
	{
		for (J = 0; J<C; J++)
			printf("%6.2f", A[I][J]);
		printf("\n");
	}

	/* Mupliply A by X and assign to B */
	for (I = 0; I<R; I++)
		for (J = 0; J<C; J++)
			B[I][J] = X*A[I][J];

	
	printf("Resultant matrix :\n");
	for (I = 0; I<R; I++)
	{
		for (J = 0; J<C; J++)
			printf("%6.2f", B[I][J]);
		printf("\n");
	}
	getch();
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Number pattern 6