Home > Backend Development > C++ > In C programming, working with 2D arrays at runtime

In C programming, working with 2D arrays at runtime

WBOY
Release: 2023-09-13 23:29:07
forward
1390 people have browsed it

In C programming, working with 2D arrays at runtime

Question

Write a C program that uses runtime compilation to calculate the sum and product of all elements in a two-dimensional array.

Solution

  • Run-time compilation or initialization is also known as dynamic allocation. Allocating memory at execution time (runtime) is called dynamic memory allocation.

  • The functions calloc() and malloc() support dynamic memory allocation.

  • The functions calloc() and malloc() support dynamic memory allocation. p>

In this program, we will calculate the sum of all elements and the product of all elements of a 2D array at runtime.

Logic used to calculate the sum of all elements in a two-dimensional array-

printf("Sum array is : </p><p>");
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("</p><p>");
}
Copy after login

Logic used to calculate the product of all elements in a two-dimensional array-

printf("Product array is : </p><p>");
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("</p><p>");
}
}
Copy after login

Example

Example demonstration

#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&#39;s A and B using for loop//
   printf("Enter elements into the array A: </p><p>");
   for(i=0;i<2;i++){
      for(j=0;j<3;j++){
         printf("A[%d][%d] :",i,j);
         scanf("%d",&A[i][j]);
      }
      printf("</p><p>");
   }
   for(i=0;i<2;i++){
      for(j=0;j<3;j++){
         printf("B[%d][%d] :",i,j);
         scanf("%d",&B[i][j]);
      }
      printf("</p><p>");
   }
   //Calculating sum and printing output//
   printf("Sum array is : </p><p>");
   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("</p><p>");
   }
   //Calculating product and printing output//
   printf("Product array is : </p><p>");
   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("</p><p>");
   }
}
Copy after login

Output

Enter elements into the array A:
A[0][0] :A[0][1] :A[0][2] :
A[1][0] :A[1][1] :A[1][2] :
B[0][0] :B[0][1] :B[0][2] :
B[1][0] :B[1][1] :B[1][2] :
Sum array is :
000
000
Product array is :
000
000
Copy after login

The above is the detailed content of In C programming, working with 2D arrays at runtime. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template