如何编写C语言程序执行标量矩阵乘法?或者,如何编写C语言程序对多维数组执行标量乘法,并附有示例。

C语言执行标量矩阵乘法的程序
此程序允许用户输入矩阵的行数和列数。接下来,这个C语言程序将使用For循环对该矩阵执行标量乘法。
/* C Program to Perform Scalar Matrix Multiplication */
#include<stdio.h>
int main()
{
int i, j, rows, columns, Multiplication[10][10], Number;
printf("\n Please Enter Number of rows and columns\n");
scanf("%d %d", &i, &j);
printf("\n Please Enter the Matrix Elements \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &Multiplication[rows][columns]);
}
}
printf("\n Please Enter the Multiplication Value : ");
scanf("%d", &Number);
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
Multiplication[rows][columns] = Number * Multiplication[rows][columns];
}
}
printf("\n The Result of a Scalar Matrix Multiplication is : \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
printf("%d \t ", Multiplication[rows][columns]);
}
printf("\n");
}
return 0;
}

在此C语言程序执行标量矩阵乘法的示例中,我们声明了大小为10*10的二维数组乘法。
下面的语句要求用户输入乘法矩阵的大小(行数和列数。例如,2行3列=乘法[2][3])。
printf("\n Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);
接下来,我们使用for循环迭代乘法[3][3]矩阵中的每个单元格。for循环内的条件(rows < i)和(columns < j)将确保C语言编译器不会超出矩阵限制。否则,矩阵将溢出。
for循环内的scanf语句将用户输入的值存储在每个单独的数组元素中,例如乘法[0][0],乘法[0][1],……。
for(rows = 0; rows < i; rows++).
{
for(columns = 0; columns < j; columns++)
{
scanf("%d", &a[rows][columns]);
}
}
接下来,我们要求用户输入乘法数,并将其保存在Number变量中。
在下一行程序中,我们还有另一个for循环来执行标量乘法。
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
Multiplication[rows][columns] = Number * Multiplication[rows][columns];
}
}
用户为执行标量矩阵乘法的程序输入的值为:乘法[3][3] = {{10, 20, 30}, { 40, 50, 60}, {70, 80, 90}}
行第一次迭代: for(rows = 0; rows < 3; 0++)
条件(0 < 3)为真。因此,它将进入第二个for循环。
列第一次迭代: for(columns = 0; 0 < 3; 0++)
条件 (columns < 3) 为真。因此,它将开始执行循环内的语句。
乘法[行][列] = 数字 * 乘法[行][列]
乘法[0][0] = 3 * 乘法[0][0] => 3 * 10 = 30
列第二次迭代:for(columns = 1; 1 < 3; 1++)
条件 (1 < 3) 为真。
乘法[0][1] = 3 * 乘法[0][1]
乘法[0][1] = 3 * 20 = 60
第二列迭代:for(columns = 2; 2 < 3; 2++)
条件 (2 < 3) 为真。
乘法[0][2] = 3 * 乘法[0][2]
乘法[0][2] = 3 * 30 = 90
j值将递增。递增后,第二个for循环(columns < 3)中的条件将失败。因此,它将退出循环。
接下来,行值递增(行将变为1),并开始第二次行迭代。
请遵循相同的步骤,其中rows = 1,然后rows = 2。
最后,我们使用另一个for循环来打印乘法矩阵。