>  기사  >  백엔드 개발  >  C 프로그래밍에서 런타임 시 2D 배열 작업

C 프로그래밍에서 런타임 시 2D 배열 작업

WBOY
WBOY앞으로
2023-09-13 23:29:071333검색

C 프로그래밍에서 런타임 시 2D 배열 작업

Question

런타임 컴파일을 사용하여 2차원 배열에 있는 모든 요소의 합과 곱을 계산하는 C 프로그램을 작성하세요.

Solution

  • 런타임 컴파일 또는 초기화를 동적 할당이라고도 합니다. 실행 시간(런타임)에 메모리를 할당하는 것을 동적 메모리 할당이라고 합니다.

  • calloc() 및 malloc() 함수는 동적 메모리 할당을 지원합니다.

  • calloc() 및 malloc() 함수는 동적 메모리 할당을 지원합니다. p>

이 프로그램에서는 2D 배열의 모든 요소의 합과 런타임 시 모든 요소의 곱을 계산합니다.

2차원 배열의 모든 요소의 합을 계산하는 논리 -

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>");
}

2차원 배열의 모든 요소의 곱을 계산하는 논리 −

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>");
}
}

예제 데모

#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>");
   }
}

출력

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

위 내용은 C 프로그래밍에서 런타임 시 2D 배열 작업의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제