在运行时初始化的二维数组上的C程序

问题

通过使用运行时编译,计算数组中所有元素的总和与乘积。

解决方案

二维数组用于必须在矩阵应用程序中存储(或)值表的情况

语法如下-

datatype array_ name [rowsize] [column size];

例如,int a [5] [5];

数组中的元素数=行大小*列大小= 5 * 5 = 25

示例

以下是C程序,通过使用运行时编译来计算数组中所有元素的总和与乘积-

#include<stdio.h>
void main(){
   //Declaring the array - run time//
   int A[2][3],B[2][3],i,j,sum[i][j],product[i][j];
   //Reading elements into the array's A and B using for loop//
   printf("Enter elements into the array A: \n");
   for(i=0;i<2;i++){
      for(j=0;j<3;j++){
         printf("A[%d][%d] :",i,j);
         scanf("%d",&A[i][j]);
      }
      printf("\n");
   }
   for(i=0;i<2;i++){
      for(j=0;j<3;j++){
         printf("B[%d][%d] :",i,j);
         scanf("%d",&B[i][j]);
      }
      printf("\n");
   }
   //Calculating sum and printing output//
   printf("Sum array is : \n");
   for(i=0;i<2;i++){
      for(j=0;j<3;j++){
         sum[i][j]=A[i][j]+B[i][j];
         printf("%d\t",sum[i][j]);
      }
      printf("\n");
   }
   //Calculating product and printing output//
   printf("Product array is : \n");
   for(i=0;i<2;i++){
      for(j=0;j<3;j++){
         product[i][j]=A[i][j]*B[i][j];
         printf("%d\t",product[i][j]);
      }
      printf("\n");
   }
}
输出结果

执行以上程序后,将产生以下结果-

Enter elements into the array A:
A[0][0] :12
A[0][1] :23
A[0][2] :03
A[1][0] :25
A[1][1] :34
A[1][2] :01
B[0][0] :03
B[0][1] :46
B[0][2] :23
B[1][0] :01
B[1][1] :24
B[1][2] :32
Sum array is:
15    69    26
26    58    33
Product array is:
36    1058   69
25    816    32