使用 for 循环编写一个 C 程序来计算矩阵的范数。在此 C 矩阵范数示例中,我们使用嵌套的 for 循环来迭代并找到平方。接下来,数学 sqrt 函数将找到矩阵的平方根。因此,矩阵的范数是其元素平方和的平方根。
#include <stdio.h>
#include <math.h>
int main()
{
int i, j, rows, columns, normal = 0;
printf("Enter Normal Matrix Rows and Columns = ");
scanf("%d %d", &rows, &columns);
int Norm_arr[rows][columns];
printf("Please Enter the Normal Matrix Items = \n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
scanf("%d", &Norm_arr[i][j]);
}
}
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
normal = normal + (Norm_arr[i][j]) * (Norm_arr[i][j]);
}
}
float actNormal = sqrt(normal);
printf("\nThe Square Of the Matrix = %d", normal);
printf("\nThe Normal Of the Matrix = %.2f\n", actNormal);
}

使用 while 循环计算矩阵范数的 C 程序。
#include <stdio.h>
#include <math.h>
int main()
{
int i, j, rows, columns, normal = 0;
printf("Enter Rows and Columns = ");
scanf("%d %d", &rows, &columns);
int Norm_arr[rows][columns];
printf("Please Enter the Items = \n");
i = 0;
while (i < rows)
{
j = 0;
while (j < columns)
{
scanf("%d", &Norm_arr[i][j]);
normal = normal + (Norm_arr[i][j]) * (Norm_arr[i][j]);
j++;
}
i++;
}
float actNormal = sqrt(normal);
printf("\nThe Square = %d", normal);
printf("\nThe Normal = %.2f\n", actNormal);
}
Enter Rows and Columns = 3 3
Please Enter the Items =
10 20 30
40 50 60
70 80 90
The Square = 28500
The Normal = 168.82
此 C 程序 使用 do while 循环计算给定矩阵的范数。
#include <stdio.h>
#include <math.h>
int main()
{
int i, j, rows, columns, normal = 0;
printf("Enter Rows and Columns = ");
scanf("%d %d", &rows, &columns);
int Norm_arr[rows][columns];
printf("Please Enter the Items = \n");
i = 0;
do
{
j = 0;
do
{
scanf("%d", &Norm_arr[i][j]);
normal = normal + (Norm_arr[i][j]) * (Norm_arr[i][j]);
} while (++j < columns);
} while (++i < rows);
float actNormal = sqrt(normal);
printf("\nThe Square = %d", normal);
printf("\nThe Normal = %.2f\n", actNormal);
}
Enter Rows and Columns = 4 4
Please Enter the Items =
10 20 30 40
50 60 70 80
90 10 25 35
45 55 65 75
The Square = 45350
The Normal = 212.96