Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

อาร์เรย์สองมิติในภาษา C คืออะไร?


อาร์เรย์คือกลุ่มของรายการที่เกี่ยวข้องกันซึ่งจัดเก็บด้วยชื่อสามัญ

ไวยากรณ์

ไวยากรณ์สำหรับการประกาศอาร์เรย์มีดังนี้ -

datatype array_name [size];

ประเภทของอาร์เรย์

Array แบ่งออกเป็น 3 ประเภทใหญ่ๆ ดังต่อไปนี้ −

  • อาร์เรย์หนึ่งมิติ
  • อาร์เรย์สองมิติ
  • อาร์เรย์หลายมิติ

การเริ่มต้น

อาร์เรย์สามารถเริ่มต้นได้สองวิธี ได้แก่ −

  • คอมไพล์การเริ่มต้นเวลา
  • การเริ่มต้นรันไทม์

สองอาร์เรย์หลายมิติ

ใช้ในสถานการณ์ที่ต้องเก็บตารางค่า (หรือ) ในแอปพลิเคชันเมทริกซ์

ไวยากรณ์

ไวยากรณ์ได้รับด้านล่าง −

datatype array_ name [rowsize] [column size];

ตัวอย่าง int a[5] [5];

a[0][0]
10
a[0][1]
20
a[0][2]
30
a[1][0]
40
a[1][1]
50
a[1]][2]
60
a[2][0]
a[2][1] a[2][2]


ต่อไปนี้เป็นโปรแกรม C สำหรับ การเริ่มต้นเวลาคอมไพล์

ตัวอย่าง

#include<stdio.h>
main ( ){
   int a[3][3] = {10,20,30,40,50,60,70,80,90};
   int i,j;
   printf ("elements of the array are");
   for ( i=0; i<3; i++){
      for (j=0;j<3; j++){
         printf("%d \t", a[i] [j]);
      }
      printf("\n");
   }
}

ผลลัพธ์

ผลลัพธ์ที่ได้ระบุไว้ด้านล่าง −

elements of the array are:
10 20 30
40 50 60
70 80 90

ต่อไปนี้เป็นโปรแกรม C สำหรับ การเริ่มต้นรันไทม์

ตัวอย่าง

#include<stdio.h>
main ( ){
   int a[3][3] ,i,j;
   printf ("enter elements of array");
   for ( i=0; i<3; i++){
      for (j=0;j<3; j++){
         scanf("%d", &a[i] [j]);
      }
   }
   printf("elements of the array are");
   for ( i=0; i<3; i++){
      for (j=0;j<3; j++){
         printf("%d\t", a[i] [j]);
      }
      printf("\n");
   }
}

ผลลัพธ์

ผลลัพธ์ที่ได้ระบุไว้ด้านล่าง −

Enter elements of array :
1 2 3 4 5 6 7 8 9
Elements of the array are
1 2 3
4 5 6
7 8 9

ด้านล่างนี้คือโปรแกรม 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] :2
A[0][1] :3
A[0][2] :1
A[1][0] :2
A[1][1] :4
A[1][2] :5
B[0][0] :1
B[0][1] :2
B[0][2] :3
B[1][0] :5
B[1][1] :6
B[1][2] :7
Sum array is :
3 5 4
7 10 12
Product array is :
2 6 3
10 24 35