SlideShare une entreprise Scribd logo
1  sur  34
Télécharger pour lire hors ligne
บทท 6 อารเรย


                  อ.วราวฒ แขงขน
มหาวทยาลยราชภฏสวนดสต ศนยพษณโลก
Content

  ●   Array
  ●   Array with String
  ●   Library function string <string.h>




K.Warawut              บทท 6 อารเรยและโครงสราง   2
1. อารเรย (Array)

  ●
      คอ ตวแปรทสามารถเกบขอมลไดเป"นช%ด โดย
      ประกาศสรางตวแปรข'(นมาเพยงตวเดยว แต*ขอมล
      ท(งหมดน(นจะตองเป"นขอมลชน-ดเดยวกน เช*น
      จ.านวนเตม 5 ตว ทศน-ยม 7 ตว หรออกขระ 15 ตว
      เป"นตน
  ●
      ตวแปรอารเรยสามารถเรยกไดอกอย*างหน'งว*า
      ตวแปรชด
            num[0] num[1] num[2] num[3] num[4]

K.Warawut    10     20         30           40
                      บทท 6 อารเรยและโครงสราง    50   3
อารเรย 1 มต
            (One Dimension Array)
  ●
      คอ ตวแปรอารเรยทเกบขอมลเพยงแค*                  num[0]
                                                10
      ช(นเดยวหรอแถวเดยว
                                                     num[1]
 num[0] num[1] num[2] num[3] num[4]
                                                20
                                                     num[2]
      10    20   30       40           50       30
                                                     num[3]
                                                40
                                                     num[4]
                                                50
K.Warawut             บทท 6 อารเรยและโครงสราง             4
อางถงขอมลในอารเรย

  ●
      การอางถ'งขอมลในอารเรยตวใดตวหน'ง เราจะตอง
      ระบ%อ-นเดกซ (index) หรอตวเลขแสดงต.าแหน*งของ
      ขอมลภายในเครองหมาย [ ] ต*อทายชอตวแปร
      อารเรย

            num[0] num[1] num[2] num[3] num[4]

             10     20         30           40   50

K.Warawut             บทท 6 อารเรยและโครงสราง         5
การประกาศตวแปรอารเรย 1 มต

  ●
      รปแบบ
        type name[n];
  ●
      ตวอย"าง
        int num[4];
        char name[25];




K.Warawut               บทท 6 อารเรยและโครงสราง   6
ก#าหนดขอมลใหกบตวแปร
                 อารเรย 1 มต
 สามารถท.าได 2 แบบ ดงน(
  1.การก.าหนดขอมลท%กต.าแหน*งในตวแปรอารเรย
    พรอมกนทเดยว ซ'งขอมลท(งหมดน(นตองเขยน
    ภายในเครองหมาย { } และคนระหว*างขอมลแต*ละ
    ตวดวยเครองหมาย ,
    ตวอย"าง
      int num[5] = {10, 20, 30, 40, 50};
      int n[ ] = {60, 70, 80, 90, 100};
      char major[8] = {'C', 'o', 'm', 'p', 'u', 't', 'e', 'r'};
K.Warawut                บทท 6 อารเรยและโครงสราง                  7
ก#าหนดขอมลใหกบตวแปร
                อารเรย 1 มต (ต"อ)
 สามารถท.าได 2 แบบ ดงน(
  2.การก.าหนดขอมลทละค*า หมายถ'ง ก.าหนดค*าเพอ
    เกบลงในต.าแหน*งใดต.าแหน*งหน'ง โดยระบ% index
    ของต.าแหน*งทตองการ
    ตวอย"าง
      num[0] = 10;
      major[1] = 'o';
      major[7] = 'r';
K.Warawut          บทท 6 อารเรยและโครงสราง        8
#include <stdio.h>
#include <stdlib.h>

int main( ) {
   int num[5] = {10, 20, 30, 40, 50};
   int i;

    for (i=0; i<5; i++)
       printf(“%d “, num[i]);

    system(“PAUSE”);
    return 0;
}
                                        9
อารเรย 2 มต
             (Two Dimension Array)
  ●
      คอ ตวแปรอารเรยทเกบขอมลทมลกษณะเป"น
      ตารางทมม-ต-ของกวางและความยาว

      num[0][0] num[0][1] num[0][2] num[0][3] num[0][4]

            10     20           30                40   50

            60    70            80                90   100
      num[1][0] num[1][1] num[1][2] num[1][3] num[1][4]
K.Warawut               บทท 6 อารเรยและโครงสราง              10
การประกาศตวแปรอารเรย 2 มต

  ●
      รปแบบ
        type name[m][n];
  ●
      ตวอย"าง
        int num[2][5];




K.Warawut                บทท 6 อารเรยและโครงสราง   11
ก#าหนดขอมลใหกบตวแปร
                 อารเรย 2 มต
 สามารถท.าได 2 แบบ ดงน(
  1.การก.าหนดขอมลท%กต.าแหน*งในตวแปรอารเรย
    พรอมกนทเดยว ซ'งขอมลท(งหมดน(นตองเขยน
    ภายในเครองหมาย { } และคนระหว*างขอมลแต*ละ
    ตวดวยเครองหมาย ,
    ตวอย"าง
      int num[2][5] = { {1, 2, 3, 4, 5},
                        {6, 7, 8, 9, 10} };
K.Warawut         บทท 6 อารเรยและโครงสราง      12
ก#าหนดขอมลใหกบตวแปร
                อารเรย 2 มต (ต"อ)
 สามารถท.าได 2 แบบ ดงน(
  2.การก.าหนดขอมลทละค*า หมายถ'ง ก.าหนดค*าเพอ
    เกบลงในต.าแหน*งใดต.าแหน*งหน'ง โดยระบ% index
    ของต.าแหน*งทตองการ
    ตวอย"าง
      num[0][0] = 10;
      num[1][0] = 60;


K.Warawut          บทท 6 อารเรยและโครงสราง        13
#include ...;

int main( ) {
    int num[2][5] = { {10, 20, 30, 40, 50},
                        {60, 70, 80, 90, 100} };
    int m, n;

    for (m=0; m<2; m++) {
       for (n=0; n<5; n++)
          printf(“num[%d][%d] = %d “, m, n, num[m][n]);
       printf(“n”);
    }

    system(“PAUSE”);
    return 0;
}                                                         14
อารเรย 3 มต
            (Three Dimension Array)
  ●
      คอ ตวแปรอารเรยทเกบขอมลทมลกษณะเป"นตาราง
      ทมม-ต-ความกวาง ความยาว และความล'ก




K.Warawut           บทท 6 อารเรยและโครงสราง    15
การประกาศตวแปรอารเรย 3 มต

  ●
      รปแบบ
        type name[m][n][d];
  ●
      ตวอย"าง
        int num[2][5][2];




K.Warawut              บทท 6 อารเรยและโครงสราง   16
ก#าหนดขอมลใหกบตวแปร
                 อารเรย 3 มต
 สามารถท.าได 2 แบบ ดงน(
  1.การก.าหนดขอมลท%กต.าแหน*งในตวแปรอารเรย
    พรอมกนทเดยว ซ'งขอมลท(งหมดน(นตองเขยน
    ภายในเครองหมาย { } และคนระหว*างขอมลแต*ละ
    ตวดวยเครองหมาย ,
    ตวอย"าง
      int num[2][2][5] = {{{1, 2, 3, 4, 5}, {6, 7, 8, 9,
    10}}, {{11, 12, 13, 14, 15}, {16, 17, 18, 19,
    20}}};
K.Warawut             บทท 6 อารเรยและโครงสราง              17
ก#าหนดขอมลใหกบตวแปร
                อารเรย 3 มต (ต"อ)
 สามารถท.าได 2 แบบ ดงน(
  2.การก.าหนดขอมลทละค*า หมายถ'ง ก.าหนดค*าเพอ
    เกบลงในต.าแหน*งใดต.าแหน*งหน'ง โดยระบ% index
    ของต.าแหน*งทตองการ
    ตวอย"าง
      num[0][0][0] = 1;
      num[0][1][0] = 11;


K.Warawut          บทท 6 อารเรยและโครงสราง        18
#include ...;

int main( ) {
    int num[2][2][5] = {{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}},
{{11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}}};
    int m, n, d;
    for (d=0; d<2; d++) {
         printf(“Deep %dn”, d+1);
         for (m=0; m<2; m++) {
            for (n=0; n<5; n++)
                printf(“%d “, num[d][m][n]);
            printf(“n”);
         }
    }
    ....
}                                                              19
Array with String

  ●
      ก.าหนดชน-ดขอมล (Data type) เป"น char
  ●
      ชอตวแปรจะมเครองหมาย [ ] และมการระบ%จ.านวน
      ตวอกษร เช*น name[25]
  ●
      การรบค*า สามารถใชฟ5งกชน gets( ) หรอ scanf( )
  ●
      สามารถเขาถ'งขอมลแต*ละ element ได โดย
      ต.าแหน*งแรกคอ 0


K.Warawut            บทท 6 อารเรยและโครงสราง         20
int main( ) {
    char name[ ] = “warawut”;
    int i;

    for (i=0; i<strlen(name); i++)
       printf(“%c”, name[i]);
}
#define MAX_CHAR 50

int main( ) {
    char name[MAX_CHAR];
    int i;

    printf(“Enter name: “); gets(name);   puts(name);
    for (i=0; i<strlen(name); i++)
       printf(“%c”, name[i]);                           21

}
Library function string <string.h>

  ●   strcpy( )
      หนาท: คดลอกขอมลจาก string หน'งไปยงอก
      string หน'ง
      รปแบบ:
         strcpy(string1, string2)




K.Warawut           บทท 6 อารเรยและโครงสราง   22
#include <stdio.h>         #include <stdio.h>
 #include <stdlib.h>        #include <stdlib.h>

 int main( ) {              int main( ) {
    char s[20];                char s[20];
                  Error
      s = “SAWASDEE”;            strcpy(s, “SAWASDEE”);
      printf(“%sn”, s);         printf(“%sn”, s);

      printf(“nn”);            printf(“nn”);
      system(“PAUSE”);           system(“PAUSE”);
      return 0;                  return 0;
 }                          }
K.Warawut            บทท 6 อารเรยและโครงสราง         23
Library function string <string.h>

  ●   strcat( )
      หนาท: ใชเชอมต*อขอความ 2 ขอความเขาดวยกน
      รปแบบ:
         strcat(string1, string2)




K.Warawut          บทท 6 อารเรยและโครงสราง     24
int main( ) {
     char name[50];
     char lastname[50];
     char fullname[100];

      strcpy(name, “Warawut”);
      strcpy(lastname, “Khangkhan”);

      strcpy(fullname, name);

      strcat(fullname, “ “);
      strcat(fullname, lastname);

      printf(“The full name is %sn”, fullname);
 }
K.Warawut                บทท 6 อารเรยและโครงสราง   25
Library function string <string.h>

  ●   strlen( )
      หนาท: ใชหาความยาวของสตร-ง
      รปแบบ:
         length = strlen(string)




K.Warawut           บทท 6 อารเรยและโครงสราง   26
int main( ) {
    char name[50];
    char lastname[50];
    char fullname[100];

   strcpy(name, “Warawut”);
   strcpy(lastname, “Khangkhan”);

   strcpy(fullname, name);

   strcat(fullname, “ “);
   strcat(fullname, lastname);

    printf(“The full name is %sn”, fullname);
    printf(“String length full name is %dn”,
strlen(fullname));                               27

}
Library function string <string.h>

  ●   strcmp( )
      หนาท: เปรยบเทยบขอความ (string1 กบ string2)
      ถามค*าเท*ากนจะคนค*า 0 ออกมา
      รปแบบ:
         strcmp(string1, string2)




K.Warawut            บทท 6 อารเรยและโครงสราง       28
int main( ) {
     char name1[50];
     char name2[50];

      strcpy(name1, “Warawut”);
      strcpy(name2, “warawut”);

   printf(“Comparing %s and %s is %d n”, name1,
 name2, strcmp(name1, name2));
 }




K.Warawut              บทท 6 อารเรยและโครงสราง     29
Library function string <string.h>

  ●   strlwr( )
      หนาท: ใชเปลยนขอความใหเป"นตวพ-มพเลก
      รปแบบ:
         strlwr(string)
  ●   strupr( )
      หนาท: ใชเปลยนขอความใหเป"นตวพ-มพใหญ*
      รปแบบ:
         strupr(string)

K.Warawut          บทท 6 อารเรยและโครงสราง   30
int main( ) {
     char name[50];

      strcpy(name, “warawut”);

      printf(“Original name is %sn”, name);
      printf(“String upper name is %sn”, strupr(name));
      printf(“Original name is %sn”, name);
      printf(“String lower name is %sn”, strlwr(name));
      printf(“Original name is %sn”, name);
 }




K.Warawut               บทท 6 อารเรยและโครงสราง            31
Library function string <string.h>

  ●   strrev( )
      หนาท: ใชกลบตวอกษรในขอความจากซายไปขวา
      รปแบบ:
         strrev(string)
  ●




K.Warawut         บทท 6 อารเรยและโครงสราง    32
int main( ) {
     char name[50];

      strcpy(name, “warawut”);

      printf(“Original name is %sn”, name);
      printf(“String reverse name is %sn”, strrev(name));
      printf(“Original name is %sn”, name);
 }




K.Warawut               บทท 6 อารเรยและโครงสราง              33
int main( ) {
     char name[50];
     int i, j;

      strcpy(name, “warawut”);

      printf(“Original name is %snn”, name);

      for (i=0; i<strlen(name); i++) {
         for (j=i; j<strlen(name); j++)
             printf(“%c”, name[j]);
         printf(“n”);
      }
 }

K.Warawut                 บทท 6 อารเรยและโครงสราง   34

Contenu connexe

Tendances

บทที่ 5 ตัวแปรชุดและตัวแปรอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระบทที่ 5 ตัวแปรชุดและตัวแปรอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระMook Sasivimon
 
บทที่5 ข้อมูลชนิดอาร์เรย์และสตริง
บทที่5 ข้อมูลชนิดอาร์เรย์และสตริงบทที่5 ข้อมูลชนิดอาร์เรย์และสตริง
บทที่5 ข้อมูลชนิดอาร์เรย์และสตริงNaphamas
 
Java Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionJava Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionIMC Institute
 
อาเรย์ (Array)
อาเรย์ (Array)อาเรย์ (Array)
อาเรย์ (Array)tumetr
 
นาย วสันต์-ล่าเริง-58170125 01
นาย วสันต์-ล่าเริง-58170125 01นาย วสันต์-ล่าเริง-58170125 01
นาย วสันต์-ล่าเริง-58170125 01Wasan Larreng
 
ตัวแปรชุดและตัวแปรกลุ่มอักขระ
ตัวแปรชุดและตัวแปรกลุ่มอักขระตัวแปรชุดและตัวแปรกลุ่มอักขระ
ตัวแปรชุดและตัวแปรกลุ่มอักขระwebsite22556
 
(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริง
(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริง(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริง
(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริงKittinan Noimanee
 

Tendances (11)

บทที่ 5 ตัวแปรชุดและตัวแปรอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระบทที่ 5 ตัวแปรชุดและตัวแปรอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระ
 
บทที่5 ข้อมูลชนิดอาร์เรย์และสตริง
บทที่5 ข้อมูลชนิดอาร์เรย์และสตริงบทที่5 ข้อมูลชนิดอาร์เรย์และสตริง
บทที่5 ข้อมูลชนิดอาร์เรย์และสตริง
 
Java Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionJava Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and Collection
 
อาเรย์ (Array)
อาเรย์ (Array)อาเรย์ (Array)
อาเรย์ (Array)
 
08 arrays
08 arrays08 arrays
08 arrays
 
นาย วสันต์-ล่าเริง-58170125 01
นาย วสันต์-ล่าเริง-58170125 01นาย วสันต์-ล่าเริง-58170125 01
นาย วสันต์-ล่าเริง-58170125 01
 
Array 2
Array 2Array 2
Array 2
 
ตัวแปรชุดและตัวแปรกลุ่มอักขระ
ตัวแปรชุดและตัวแปรกลุ่มอักขระตัวแปรชุดและตัวแปรกลุ่มอักขระ
ตัวแปรชุดและตัวแปรกลุ่มอักขระ
 
Test
TestTest
Test
 
(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริง
(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริง(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริง
(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริง
 
ตัวแปลชุดและตัวแปลอักขระ
ตัวแปลชุดและตัวแปลอักขระตัวแปลชุดและตัวแปลอักขระ
ตัวแปลชุดและตัวแปลอักขระ
 

En vedette

บทที่2 โครงสร้างข้อมูลอาร์เรย์
บทที่2 โครงสร้างข้อมูลอาร์เรย์บทที่2 โครงสร้างข้อมูลอาร์เรย์
บทที่2 โครงสร้างข้อมูลอาร์เรย์tapabnum
 
โครงสร้างแบบอาร์เรย์
โครงสร้างแบบอาร์เรย์โครงสร้างแบบอาร์เรย์
โครงสร้างแบบอาร์เรย์waradakhantee
 
สแตกและคิว (Stack & queue)
สแตกและคิว (Stack & queue)สแตกและคิว (Stack & queue)
สแตกและคิว (Stack & queue)tumetr
 
การใช้ตัวแปรแบบคงที่ใน VB.NET 2005 Express Editor
การใช้ตัวแปรแบบคงที่ใน VB.NET 2005 Express Editorการใช้ตัวแปรแบบคงที่ใน VB.NET 2005 Express Editor
การใช้ตัวแปรแบบคงที่ใน VB.NET 2005 Express EditorWarawut
 
Chapter6 ตัวแปร โอเปอเรเตอร์
Chapter6 ตัวแปร โอเปอเรเตอร์Chapter6 ตัวแปร โอเปอเรเตอร์
Chapter6 ตัวแปร โอเปอเรเตอร์Siriyakorn Bk
 
แสตก
แสตกแสตก
แสตกNu_waew
 
การจัดเรียงข้อมูล (sorting)
การจัดเรียงข้อมูล (sorting)การจัดเรียงข้อมูล (sorting)
การจัดเรียงข้อมูล (sorting)tumetr
 
โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)
โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)
โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)tumetr
 
การวิเคราะห์อัลกอริทึม
การวิเคราะห์อัลกอริทึมการวิเคราะห์อัลกอริทึม
การวิเคราะห์อัลกอริทึมguestc3a629f6
 
5.ข้อมูลชนิดอาร์เรย์
5.ข้อมูลชนิดอาร์เรย์5.ข้อมูลชนิดอาร์เรย์
5.ข้อมูลชนิดอาร์เรย์mansuang1978
 
Lesson1 programing concept
Lesson1 programing conceptLesson1 programing concept
Lesson1 programing conceptskiats
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examplesmua99
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversionSyed Mustafa
 

En vedette (16)

บทที่2 โครงสร้างข้อมูลอาร์เรย์
บทที่2 โครงสร้างข้อมูลอาร์เรย์บทที่2 โครงสร้างข้อมูลอาร์เรย์
บทที่2 โครงสร้างข้อมูลอาร์เรย์
 
โครงสร้างแบบอาร์เรย์
โครงสร้างแบบอาร์เรย์โครงสร้างแบบอาร์เรย์
โครงสร้างแบบอาร์เรย์
 
Array1
Array1Array1
Array1
 
Computer
ComputerComputer
Computer
 
สแตกและคิว (Stack & queue)
สแตกและคิว (Stack & queue)สแตกและคิว (Stack & queue)
สแตกและคิว (Stack & queue)
 
การใช้ตัวแปรแบบคงที่ใน VB.NET 2005 Express Editor
การใช้ตัวแปรแบบคงที่ใน VB.NET 2005 Express Editorการใช้ตัวแปรแบบคงที่ใน VB.NET 2005 Express Editor
การใช้ตัวแปรแบบคงที่ใน VB.NET 2005 Express Editor
 
vb.net
vb.netvb.net
vb.net
 
Chapter6 ตัวแปร โอเปอเรเตอร์
Chapter6 ตัวแปร โอเปอเรเตอร์Chapter6 ตัวแปร โอเปอเรเตอร์
Chapter6 ตัวแปร โอเปอเรเตอร์
 
แสตก
แสตกแสตก
แสตก
 
การจัดเรียงข้อมูล (sorting)
การจัดเรียงข้อมูล (sorting)การจัดเรียงข้อมูล (sorting)
การจัดเรียงข้อมูล (sorting)
 
โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)
โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)
โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)
 
การวิเคราะห์อัลกอริทึม
การวิเคราะห์อัลกอริทึมการวิเคราะห์อัลกอริทึม
การวิเคราะห์อัลกอริทึม
 
5.ข้อมูลชนิดอาร์เรย์
5.ข้อมูลชนิดอาร์เรย์5.ข้อมูลชนิดอาร์เรย์
5.ข้อมูลชนิดอาร์เรย์
 
Lesson1 programing concept
Lesson1 programing conceptLesson1 programing concept
Lesson1 programing concept
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversion
 

Similaire à Array

ตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระAreeya Onnom
 
ตัวแปรชุดและตัวแปรอักขระ PPT
ตัวแปรชุดและตัวแปรอักขระ PPTตัวแปรชุดและตัวแปรอักขระ PPT
ตัวแปรชุดและตัวแปรอักขระ PPTAreeya Onnom
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นThanachart Numnonda
 
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขร
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขรบทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขร
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขรMook Sasivimon
 
ตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระ ตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระ Areeya Onnom
 
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]Mook Sasivimon
 
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระบทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระMook Sasivimon
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมThanachart Numnonda
 
การเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา Cการเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา CWarawut
 
การใช้งาน Matlab
การใช้งาน Matlabการใช้งาน Matlab
การใช้งาน MatlabOoy's Patchaya
 
บทที่ 10 ตัวแปรสตริ
บทที่ 10 ตัวแปรสตริบทที่ 10 ตัวแปรสตริ
บทที่ 10 ตัวแปรสตริTheeravaj Tum
 

Similaire à Array (20)

ตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระ
 
ตัวแปรชุดและตัวแปรอักขระ PPT
ตัวแปรชุดและตัวแปรอักขระ PPTตัวแปรชุดและตัวแปรอักขระ PPT
ตัวแปรชุดและตัวแปรอักขระ PPT
 
C lang
C langC lang
C lang
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่น
 
งาน
งานงาน
งาน
 
Tec4
Tec4Tec4
Tec4
 
งานทำ Blog บทที่ 8
งานทำ Blog บทที่ 8งานทำ Blog บทที่ 8
งานทำ Blog บทที่ 8
 
99
9999
99
 
งาน
งานงาน
งาน
 
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขร
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขรบทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขร
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขร
 
งานทำ Blog บทที่ 8
งานทำ Blog บทที่ 8งานทำ Blog บทที่ 8
งานทำ Blog บทที่ 8
 
งานทำ Blog บทที่ 8
งานทำ Blog บทที่ 8งานทำ Blog บทที่ 8
งานทำ Blog บทที่ 8
 
ตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระ ตัวแปรชุดและตัวแปรอักขระ
ตัวแปรชุดและตัวแปรอักขระ
 
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]
 
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระบทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุม
 
การเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา Cการเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา C
 
Week1 f end
Week1 f endWeek1 f end
Week1 f end
 
การใช้งาน Matlab
การใช้งาน Matlabการใช้งาน Matlab
การใช้งาน Matlab
 
บทที่ 10 ตัวแปรสตริ
บทที่ 10 ตัวแปรสตริบทที่ 10 ตัวแปรสตริ
บทที่ 10 ตัวแปรสตริ
 

Plus de Warawut

Database design
Database designDatabase design
Database designWarawut
 
Business Computer Project 4
Business Computer Project 4Business Computer Project 4
Business Computer Project 4Warawut
 
Object-Oriented Programming 10
Object-Oriented Programming 10Object-Oriented Programming 10
Object-Oriented Programming 10Warawut
 
Object-Oriented Programming 9
Object-Oriented Programming 9Object-Oriented Programming 9
Object-Oriented Programming 9Warawut
 
Object-Oriented Programming 8
Object-Oriented Programming 8Object-Oriented Programming 8
Object-Oriented Programming 8Warawut
 
Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7Warawut
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6Warawut
 
Management Information System 6
Management Information System 6Management Information System 6
Management Information System 6Warawut
 
Management Information System 5
Management Information System 5Management Information System 5
Management Information System 5Warawut
 
Management Information System 4
Management Information System 4Management Information System 4
Management Information System 4Warawut
 
Object-Oriented Programming 5
Object-Oriented Programming 5Object-Oriented Programming 5
Object-Oriented Programming 5Warawut
 
Business Computer Project 3
Business Computer Project 3Business Computer Project 3
Business Computer Project 3Warawut
 
Management Information System 3
Management Information System 3Management Information System 3
Management Information System 3Warawut
 
Business Computer Project 2
Business Computer Project 2Business Computer Project 2
Business Computer Project 2Warawut
 
Chapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemChapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemWarawut
 
Object-Oriented Programming 4
Object-Oriented Programming 4Object-Oriented Programming 4
Object-Oriented Programming 4Warawut
 
Business Computer Project 1
Business Computer Project 1Business Computer Project 1
Business Computer Project 1Warawut
 
Chapter 1 Organization & MIS
Chapter 1 Organization & MISChapter 1 Organization & MIS
Chapter 1 Organization & MISWarawut
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3Warawut
 
Object-Oriented Programming 2
Object-Oriented Programming 2Object-Oriented Programming 2
Object-Oriented Programming 2Warawut
 

Plus de Warawut (20)

Database design
Database designDatabase design
Database design
 
Business Computer Project 4
Business Computer Project 4Business Computer Project 4
Business Computer Project 4
 
Object-Oriented Programming 10
Object-Oriented Programming 10Object-Oriented Programming 10
Object-Oriented Programming 10
 
Object-Oriented Programming 9
Object-Oriented Programming 9Object-Oriented Programming 9
Object-Oriented Programming 9
 
Object-Oriented Programming 8
Object-Oriented Programming 8Object-Oriented Programming 8
Object-Oriented Programming 8
 
Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6
 
Management Information System 6
Management Information System 6Management Information System 6
Management Information System 6
 
Management Information System 5
Management Information System 5Management Information System 5
Management Information System 5
 
Management Information System 4
Management Information System 4Management Information System 4
Management Information System 4
 
Object-Oriented Programming 5
Object-Oriented Programming 5Object-Oriented Programming 5
Object-Oriented Programming 5
 
Business Computer Project 3
Business Computer Project 3Business Computer Project 3
Business Computer Project 3
 
Management Information System 3
Management Information System 3Management Information System 3
Management Information System 3
 
Business Computer Project 2
Business Computer Project 2Business Computer Project 2
Business Computer Project 2
 
Chapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemChapter 2 Strategy & Information System
Chapter 2 Strategy & Information System
 
Object-Oriented Programming 4
Object-Oriented Programming 4Object-Oriented Programming 4
Object-Oriented Programming 4
 
Business Computer Project 1
Business Computer Project 1Business Computer Project 1
Business Computer Project 1
 
Chapter 1 Organization & MIS
Chapter 1 Organization & MISChapter 1 Organization & MIS
Chapter 1 Organization & MIS
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3
 
Object-Oriented Programming 2
Object-Oriented Programming 2Object-Oriented Programming 2
Object-Oriented Programming 2
 

Array

  • 1. บทท 6 อารเรย อ.วราวฒ แขงขน มหาวทยาลยราชภฏสวนดสต ศนยพษณโลก
  • 2. Content ● Array ● Array with String ● Library function string <string.h> K.Warawut บทท 6 อารเรยและโครงสราง 2
  • 3. 1. อารเรย (Array) ● คอ ตวแปรทสามารถเกบขอมลไดเป"นช%ด โดย ประกาศสรางตวแปรข'(นมาเพยงตวเดยว แต*ขอมล ท(งหมดน(นจะตองเป"นขอมลชน-ดเดยวกน เช*น จ.านวนเตม 5 ตว ทศน-ยม 7 ตว หรออกขระ 15 ตว เป"นตน ● ตวแปรอารเรยสามารถเรยกไดอกอย*างหน'งว*า ตวแปรชด num[0] num[1] num[2] num[3] num[4] K.Warawut 10 20 30 40 บทท 6 อารเรยและโครงสราง 50 3
  • 4. อารเรย 1 มต (One Dimension Array) ● คอ ตวแปรอารเรยทเกบขอมลเพยงแค* num[0] 10 ช(นเดยวหรอแถวเดยว num[1] num[0] num[1] num[2] num[3] num[4] 20 num[2] 10 20 30 40 50 30 num[3] 40 num[4] 50 K.Warawut บทท 6 อารเรยและโครงสราง 4
  • 5. อางถงขอมลในอารเรย ● การอางถ'งขอมลในอารเรยตวใดตวหน'ง เราจะตอง ระบ%อ-นเดกซ (index) หรอตวเลขแสดงต.าแหน*งของ ขอมลภายในเครองหมาย [ ] ต*อทายชอตวแปร อารเรย num[0] num[1] num[2] num[3] num[4] 10 20 30 40 50 K.Warawut บทท 6 อารเรยและโครงสราง 5
  • 6. การประกาศตวแปรอารเรย 1 มต ● รปแบบ type name[n]; ● ตวอย"าง int num[4]; char name[25]; K.Warawut บทท 6 อารเรยและโครงสราง 6
  • 7. ก#าหนดขอมลใหกบตวแปร อารเรย 1 มต สามารถท.าได 2 แบบ ดงน( 1.การก.าหนดขอมลท%กต.าแหน*งในตวแปรอารเรย พรอมกนทเดยว ซ'งขอมลท(งหมดน(นตองเขยน ภายในเครองหมาย { } และคนระหว*างขอมลแต*ละ ตวดวยเครองหมาย , ตวอย"าง int num[5] = {10, 20, 30, 40, 50}; int n[ ] = {60, 70, 80, 90, 100}; char major[8] = {'C', 'o', 'm', 'p', 'u', 't', 'e', 'r'}; K.Warawut บทท 6 อารเรยและโครงสราง 7
  • 8. ก#าหนดขอมลใหกบตวแปร อารเรย 1 มต (ต"อ) สามารถท.าได 2 แบบ ดงน( 2.การก.าหนดขอมลทละค*า หมายถ'ง ก.าหนดค*าเพอ เกบลงในต.าแหน*งใดต.าแหน*งหน'ง โดยระบ% index ของต.าแหน*งทตองการ ตวอย"าง num[0] = 10; major[1] = 'o'; major[7] = 'r'; K.Warawut บทท 6 อารเรยและโครงสราง 8
  • 9. #include <stdio.h> #include <stdlib.h> int main( ) { int num[5] = {10, 20, 30, 40, 50}; int i; for (i=0; i<5; i++) printf(“%d “, num[i]); system(“PAUSE”); return 0; } 9
  • 10. อารเรย 2 มต (Two Dimension Array) ● คอ ตวแปรอารเรยทเกบขอมลทมลกษณะเป"น ตารางทมม-ต-ของกวางและความยาว num[0][0] num[0][1] num[0][2] num[0][3] num[0][4] 10 20 30 40 50 60 70 80 90 100 num[1][0] num[1][1] num[1][2] num[1][3] num[1][4] K.Warawut บทท 6 อารเรยและโครงสราง 10
  • 11. การประกาศตวแปรอารเรย 2 มต ● รปแบบ type name[m][n]; ● ตวอย"าง int num[2][5]; K.Warawut บทท 6 อารเรยและโครงสราง 11
  • 12. ก#าหนดขอมลใหกบตวแปร อารเรย 2 มต สามารถท.าได 2 แบบ ดงน( 1.การก.าหนดขอมลท%กต.าแหน*งในตวแปรอารเรย พรอมกนทเดยว ซ'งขอมลท(งหมดน(นตองเขยน ภายในเครองหมาย { } และคนระหว*างขอมลแต*ละ ตวดวยเครองหมาย , ตวอย"าง int num[2][5] = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10} }; K.Warawut บทท 6 อารเรยและโครงสราง 12
  • 13. ก#าหนดขอมลใหกบตวแปร อารเรย 2 มต (ต"อ) สามารถท.าได 2 แบบ ดงน( 2.การก.าหนดขอมลทละค*า หมายถ'ง ก.าหนดค*าเพอ เกบลงในต.าแหน*งใดต.าแหน*งหน'ง โดยระบ% index ของต.าแหน*งทตองการ ตวอย"าง num[0][0] = 10; num[1][0] = 60; K.Warawut บทท 6 อารเรยและโครงสราง 13
  • 14. #include ...; int main( ) { int num[2][5] = { {10, 20, 30, 40, 50}, {60, 70, 80, 90, 100} }; int m, n; for (m=0; m<2; m++) { for (n=0; n<5; n++) printf(“num[%d][%d] = %d “, m, n, num[m][n]); printf(“n”); } system(“PAUSE”); return 0; } 14
  • 15. อารเรย 3 มต (Three Dimension Array) ● คอ ตวแปรอารเรยทเกบขอมลทมลกษณะเป"นตาราง ทมม-ต-ความกวาง ความยาว และความล'ก K.Warawut บทท 6 อารเรยและโครงสราง 15
  • 16. การประกาศตวแปรอารเรย 3 มต ● รปแบบ type name[m][n][d]; ● ตวอย"าง int num[2][5][2]; K.Warawut บทท 6 อารเรยและโครงสราง 16
  • 17. ก#าหนดขอมลใหกบตวแปร อารเรย 3 มต สามารถท.าได 2 แบบ ดงน( 1.การก.าหนดขอมลท%กต.าแหน*งในตวแปรอารเรย พรอมกนทเดยว ซ'งขอมลท(งหมดน(นตองเขยน ภายในเครองหมาย { } และคนระหว*างขอมลแต*ละ ตวดวยเครองหมาย , ตวอย"าง int num[2][2][5] = {{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}}, {{11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}}}; K.Warawut บทท 6 อารเรยและโครงสราง 17
  • 18. ก#าหนดขอมลใหกบตวแปร อารเรย 3 มต (ต"อ) สามารถท.าได 2 แบบ ดงน( 2.การก.าหนดขอมลทละค*า หมายถ'ง ก.าหนดค*าเพอ เกบลงในต.าแหน*งใดต.าแหน*งหน'ง โดยระบ% index ของต.าแหน*งทตองการ ตวอย"าง num[0][0][0] = 1; num[0][1][0] = 11; K.Warawut บทท 6 อารเรยและโครงสราง 18
  • 19. #include ...; int main( ) { int num[2][2][5] = {{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}}, {{11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}}}; int m, n, d; for (d=0; d<2; d++) { printf(“Deep %dn”, d+1); for (m=0; m<2; m++) { for (n=0; n<5; n++) printf(“%d “, num[d][m][n]); printf(“n”); } } .... } 19
  • 20. Array with String ● ก.าหนดชน-ดขอมล (Data type) เป"น char ● ชอตวแปรจะมเครองหมาย [ ] และมการระบ%จ.านวน ตวอกษร เช*น name[25] ● การรบค*า สามารถใชฟ5งกชน gets( ) หรอ scanf( ) ● สามารถเขาถ'งขอมลแต*ละ element ได โดย ต.าแหน*งแรกคอ 0 K.Warawut บทท 6 อารเรยและโครงสราง 20
  • 21. int main( ) { char name[ ] = “warawut”; int i; for (i=0; i<strlen(name); i++) printf(“%c”, name[i]); } #define MAX_CHAR 50 int main( ) { char name[MAX_CHAR]; int i; printf(“Enter name: “); gets(name); puts(name); for (i=0; i<strlen(name); i++) printf(“%c”, name[i]); 21 }
  • 22. Library function string <string.h> ● strcpy( ) หนาท: คดลอกขอมลจาก string หน'งไปยงอก string หน'ง รปแบบ: strcpy(string1, string2) K.Warawut บทท 6 อารเรยและโครงสราง 22
  • 23. #include <stdio.h> #include <stdio.h> #include <stdlib.h> #include <stdlib.h> int main( ) { int main( ) { char s[20]; char s[20]; Error s = “SAWASDEE”; strcpy(s, “SAWASDEE”); printf(“%sn”, s); printf(“%sn”, s); printf(“nn”); printf(“nn”); system(“PAUSE”); system(“PAUSE”); return 0; return 0; } } K.Warawut บทท 6 อารเรยและโครงสราง 23
  • 24. Library function string <string.h> ● strcat( ) หนาท: ใชเชอมต*อขอความ 2 ขอความเขาดวยกน รปแบบ: strcat(string1, string2) K.Warawut บทท 6 อารเรยและโครงสราง 24
  • 25. int main( ) { char name[50]; char lastname[50]; char fullname[100]; strcpy(name, “Warawut”); strcpy(lastname, “Khangkhan”); strcpy(fullname, name); strcat(fullname, “ “); strcat(fullname, lastname); printf(“The full name is %sn”, fullname); } K.Warawut บทท 6 อารเรยและโครงสราง 25
  • 26. Library function string <string.h> ● strlen( ) หนาท: ใชหาความยาวของสตร-ง รปแบบ: length = strlen(string) K.Warawut บทท 6 อารเรยและโครงสราง 26
  • 27. int main( ) { char name[50]; char lastname[50]; char fullname[100]; strcpy(name, “Warawut”); strcpy(lastname, “Khangkhan”); strcpy(fullname, name); strcat(fullname, “ “); strcat(fullname, lastname); printf(“The full name is %sn”, fullname); printf(“String length full name is %dn”, strlen(fullname)); 27 }
  • 28. Library function string <string.h> ● strcmp( ) หนาท: เปรยบเทยบขอความ (string1 กบ string2) ถามค*าเท*ากนจะคนค*า 0 ออกมา รปแบบ: strcmp(string1, string2) K.Warawut บทท 6 อารเรยและโครงสราง 28
  • 29. int main( ) { char name1[50]; char name2[50]; strcpy(name1, “Warawut”); strcpy(name2, “warawut”); printf(“Comparing %s and %s is %d n”, name1, name2, strcmp(name1, name2)); } K.Warawut บทท 6 อารเรยและโครงสราง 29
  • 30. Library function string <string.h> ● strlwr( ) หนาท: ใชเปลยนขอความใหเป"นตวพ-มพเลก รปแบบ: strlwr(string) ● strupr( ) หนาท: ใชเปลยนขอความใหเป"นตวพ-มพใหญ* รปแบบ: strupr(string) K.Warawut บทท 6 อารเรยและโครงสราง 30
  • 31. int main( ) { char name[50]; strcpy(name, “warawut”); printf(“Original name is %sn”, name); printf(“String upper name is %sn”, strupr(name)); printf(“Original name is %sn”, name); printf(“String lower name is %sn”, strlwr(name)); printf(“Original name is %sn”, name); } K.Warawut บทท 6 อารเรยและโครงสราง 31
  • 32. Library function string <string.h> ● strrev( ) หนาท: ใชกลบตวอกษรในขอความจากซายไปขวา รปแบบ: strrev(string) ● K.Warawut บทท 6 อารเรยและโครงสราง 32
  • 33. int main( ) { char name[50]; strcpy(name, “warawut”); printf(“Original name is %sn”, name); printf(“String reverse name is %sn”, strrev(name)); printf(“Original name is %sn”, name); } K.Warawut บทท 6 อารเรยและโครงสราง 33
  • 34. int main( ) { char name[50]; int i, j; strcpy(name, “warawut”); printf(“Original name is %snn”, name); for (i=0; i<strlen(name); i++) { for (j=i; j<strlen(name); j++) printf(“%c”, name[j]); printf(“n”); } } K.Warawut บทท 6 อารเรยและโครงสราง 34