วันอังคารที่ 7 กรกฎาคม พ.ศ. 2552

แบบฝึกหัด

1. ให้นักศึกษากำหนดค่าของ Array 1 มิติ และ Array 2 มิติ
#include"stdio.h"
main()
{
int a[1];
int b[2][3];
}

2. ให้นักศึกษาหาค่าของ A[2], A[6] จากค่า A={2,8,16,24,9,7,3,8}
#include"stdio.h"
main()
{
int a[]={2,8,16,24,9,7,3,8};
printf("%d\n",a[2]);
printf("%d\n",a[6]);
}
จะได้ ค่าของ A[2] = 16
และ ค่าของ A[6] = 3

3. จากค่าของ int a[2][3] = {{6,5,4},{3,2,1}};ให้นักศึกษา หาค่าของ a[1][0] และ a[0][2]
#include"stdio.h"
main()
{
int a[2][3] = {{6,5,4},{3,2,1}};
printf("%d\n",a[1][0]);
printf("%d\n",a[0][2]);
}
จะได้ ค่าของ A [1][0] = 3
และ ค่าของ A [0][2] = 4

4. ให้นักศึกษากำหนด Structure ที่มีค่าของข้อมูลจากน้อย6 Records
#include"stdio.h"
main()
{
struct number
{
int i;
int a;
}num;
struct date
{
int day;
int month;
int year;
};
struct product
{
float price;
char type[40];
int many;
struct date buy;
float total;
}pn;
{
printf("\t\tbuyer Data\n",num.i+1);
printf("Date of buy : ");
scanf("%d",&pn.buy.day);
printf("month of buy : ");
scanf("%d",&pn.buy.month);
printf("Year of buy : ");
scanf("%d",&pn.buy.year);
printf("Buy date : %d-%d-%d\n",pn.buy.day,pn.buy.month,pn.buy.year);
printf("How many type you will buy : ");
scanf("%d",&num.a);
for(num.i=0;num.i{
printf("Enter type name : ");
scanf("%s",&pn.type);
printf("price = ");
scanf("%f",&pn.price);
printf("How many you buy : ");
scanf("%d",&pn.many);
printf(" \n");
pn.total = pn.price*pn.many;
printf("\t\t Data buyer \n");
printf("Buy date : %d-%d-%d\n",pn.buy.day,pn.buy.month,pn.buy.year);
printf("Name : %s\n",pn.type);
printf("price : %.0f\n",pn.price);
printf("Pieces : %d\n",pn.many);
printf("total : %.2f\n\n",pn.total);
}
}
}

5. ให้นักศึกษาบอกความแตกต่างของการกำหนดตัวชนิด Array กับตัวแปร Pointer ในสภาพของการกำหนดที่อยู่ของข้อมูล
ตัวแปร Pointer คือตัวแปรที่เก็บตำแหน่งของหน่วยความจำซึ่งตำแหน่งของหน่วยความจำนี้จะเป็นที่อยู่ของสิ่งอื่น ๆ ในหน่วยความจำ
Array คือกลุ่มของข้อมูล ที่มีชนิดของข้อมูลเป็นชนิดเดียวกัน การอ้างถึงกลุ่มของชุดข้อมูลนี้จะอ้างด้วยชื่อของตัวแปรเดียวกัน
ความแตกต่างระหว่างตัวแปร Array และ Pointer คือตัวแปรตารางอาเรย์จะเก็บเฉพาะค่าต่างๆ ที่เป็นชนิดกันเดียวกับตัวแปรอาเรย์แต่ ตัวแปรพอยเตอร์จะเก็บเฉพาะค่าตำแหน่ง Address ตัวแปรเท่านั้น

ไม่มีความคิดเห็น:

แสดงความคิดเห็น