Park-Kachen
28 มิถุนายน 2552
DTS02-24-06-2552
ARRAYอะเรย์ เป็นโครงสร้างข้อมูลประกอบด้วยสมาชิกที่มีจำนวนคงที่ ในการจัดเก็บสมาชิกแต่ละตัวจะมีขนาดเท่ากันและเรียงต่อเนื่องในหน่วยความจำหลัก การกำหนดอะเรย์จะต้องกำหนดชื่อตัวแปรบอกมิติของอะเรย์นั้น จะพิจารณาตาม ประเภทของอะเรย์ในมิติต่างๆ ดังนี้
-อะเรย์ 1 มิติ
-อะเรย์ หลายมิติ
1. อะเรย์ 1 มิติ มีรูปแบบ คือ data-type array-name[expression] เช่น int num[3] หมายถึง การจองพื้นที่ในหน่วยความจำหลักสำหรับ num ให้เป็นตัวแปรชุดชนิด integer ขนาด 3 สมาชิก โดยเรียก สมาชิกตัวที่ 1 เป็น num[0] เรียงไปเรื่อยๆ จนถึงสมาชิกตัวที่ 3
1. อะเรย์ 1 มิติ มีรูปแบบ คือ data-type array-name[expression] เช่น int num[3] หมายถึง การจองพื้นที่ในหน่วยความจำหลักสำหรับ num ให้เป็นตัวแปรชุดชนิด integer ขนาด 3 สมาชิก โดยเรียก สมาชิกตัวที่ 1 เป็น num[0] เรียงไปเรื่อยๆ จนถึงสมาชิกตัวที่ 3
2. อะเรย์ 2 มิติ มีรูปแบบ คือ data-type array-name[n][m]โดยที่ n = ตัวเลขที่แสดงตำแหน่งของแถวm = ตวเลขที่แสดงตำแหน่งของคอลัมน์เช่น char b[3][3]; หมายถึง การจองพื้นที่ ในหน่วยความจำหลัก จำนวน 9ที่ สำหรับตัวแปร b โดยที่จำนวนสมาชิกของอะเรย์หาได้จาก จำนวนแถว x จำนวนคอลัมน์
ยกตัวอย่าง
col1 col2 col3
row1 b[0][0] b[0][1] b[0][2]
row2 b[1][0] b[1][1] b[1][2]
row3 b[2][0] b[2][1] b[2][2]
row1 b[0][0] b[0][1] b[0][2]
row2 b[1][0] b[1][1] b[1][2]
row3 b[2][0] b[2][1] b[2][2]
RECORD OR STRUCTURE
record เป็นการกำหนดข้อมูลให้เป็นรูปแบบของ structurestructure เป็นโครงสร้างที่สมาชิกมีประเภทของข้อมูลต่างกัน โดยอาจเป็นจำนวนเต็ม ทศนิยม อะเรย์ อักขระ พอยเตอร์ และ structure ด้วยกัน
record เป็นการกำหนดข้อมูลให้เป็นรูปแบบของ structurestructure เป็นโครงสร้างที่สมาชิกมีประเภทของข้อมูลต่างกัน โดยอาจเป็นจำนวนเต็ม ทศนิยม อะเรย์ อักขระ พอยเตอร์ และ structure ด้วยกัน
ตัวอย่าง อะเรย์ชนิดโครงสร้าง
struct struc-name{
type name1;
type name1;
type name1;
............
type name-n;
struct-array variable;
}
อะเรย์ชนิดโครงสร้างรูปแบบการอ้างถึง คือ struct-array-name[subscript].member-name
ตัวอย่างเช่น
#include "stdio.h"
#include "conio.h"
void cal(infor)
struct fee{
char name[20];
char faculty[30];
char majority[50];
int math;
int thai;
int english;
int programming;
int multimedia;
} infor;
{
int y;
clrscr();
printf("name : %s\t",infor.name);
printf("faculty : %s\n",infor.faculty);
printf("majority : %s\n",infor.majority);
y = infor.math + infor.thai + infor.english + infor.programming + infor.multimedia;
printf("%d baht\n",y*250);
}
main()
{
struct fee{
char name[20];
char faculty[30];
char majority[50];
int math;
int thai;
int english;
int programming;
int multimedia; };
struct fee student;
printf("Enter name :");
gets(student.name);
printf("Enter faculty :");
gets(student.faculty);
printf("Enter majority :");
gets(student.majority);
printf("Enter math credit :");
scanf("%d",&student.math);
printf("Enter thai credit :");
scanf("%d",&student.thai);
printf("Enter english credit :");
scanf("%d",&student.english);
printf("Enter programming credit :");
scanf("%d",&student.programming);
printf("Enter multimedia credit :");
scanf("%d",&student.multimedia);
cal(student);
}
#include "conio.h"
void cal(infor)
struct fee{
char name[20];
char faculty[30];
char majority[50];
int math;
int thai;
int english;
int programming;
int multimedia;
} infor;
{
int y;
clrscr();
printf("name : %s\t",infor.name);
printf("faculty : %s\n",infor.faculty);
printf("majority : %s\n",infor.majority);
y = infor.math + infor.thai + infor.english + infor.programming + infor.multimedia;
printf("%d baht\n",y*250);
}
main()
{
struct fee{
char name[20];
char faculty[30];
char majority[50];
int math;
int thai;
int english;
int programming;
int multimedia; };
struct fee student;
printf("Enter name :");
gets(student.name);
printf("Enter faculty :");
gets(student.faculty);
printf("Enter majority :");
gets(student.majority);
printf("Enter math credit :");
scanf("%d",&student.math);
printf("Enter thai credit :");
scanf("%d",&student.thai);
printf("Enter english credit :");
scanf("%d",&student.english);
printf("Enter programming credit :");
scanf("%d",&student.programming);
printf("Enter multimedia credit :");
scanf("%d",&student.multimedia);
cal(student);
}
27 มิถุนายน 2552
ประวัติ
นายขวัญเมือง คเชนทร
Mr.Khwanmuang Kachentorn
หลักสูตร การบริหารธุรกิจ (คอมพิวเตอร์ธุรกิจ) คณะ วิทยการจัดการมหาวิทยาลัยราชภัฏสวนดุสิต
E-mail : u50172792027@gmail.com
Mr.Khwanmuang Kachentorn
หลักสูตร การบริหารธุรกิจ (คอมพิวเตอร์ธุรกิจ) คณะ วิทยการจัดการมหาวิทยาลัยราชภัฏสวนดุสิต
E-mail : u50172792027@gmail.com