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

The elements of matrix A will be multiplied by X.

X×(abcdefghijkl)=(X×aX×bX×cX×dX×eX×fX×gX×hX×iX×jX×kX×l)


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];
	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++)
			A[I][J] *= X;

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

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
GCD between 2 numbers using recursion