Document 1487202

Download Report

Transcript Document 1487202

Arrays
Nithi Thanon
Computer Science
Prince of Songkla University
2
อาร์เรย์ (Array)

ตัวแปรสำหรับเก็บชุดข ้อมูลชนิดเดียวกัน
int data[10];


ระบุตำแหน่งข ้อมูลจำกดัชนีข ้อมูล (Index)
กำรอ ้ำงอิงดัชนีข ้อมูลเริม
่ จำก 0 ถึง n-1
data[0] = 20;
printf("Data 9 %d", data[9]);
การโปรแกรมภาษา C
นิธิ ทะนนท์
3
การประกาศตัวแปรอาร์เรย์
// ประกำศตัวแปร
int nArray[5];
// กำหนดค่ำเริม
่ ต ้น
int nArray[5] = {34, 27, 45, 82, 22};
// กำหนดค่ำเริม
่ ต ้น โดยไม่ระบุขนำด
int nArray[] = {34, 27, 45, 82, 22};
int nArray[] = {34, 27, 45};
// ประกำศตัวแปร โดยใชค่้ ำคงที่
#define MAX_SIZE 5
int nArray[MAX_SIZE];
#define MAX_SIZE 5
int nArray[MAX_SIZE] = {34, 27, 45, 82, 22};
การโปรแกรมภาษา C
นิธิ ทะนนท์
4
การใช้งานตัวแปรอาร์เรย์
int nArray[5];
nArray[0]
nArray[1]
nArray[2]
nArray[3]
nArray[4]
=
=
=
=
=
34;
27;
45;
82;
22;
nArray[2] = 10;
nArray[3] = x + y * z;
nArray[1] = nArray[3] - nArray[2];
nArray[MAX_SIZE-2] = 20;
nArray[x+y-z] = 10;
การโปรแกรมภาษา C
นิธิ ทะนนท์
5
#include <stdio.h>
double data[5];
double total;
double average;
/* data to average and total */
/* the total of the data items */
/* average of the items */
int main() {
data[0] = 34.0;
data[1] = 27.0;
data[2] = 45.0;
data[3] = 82.0;
data[4] = 22.0;
total = data[0] + data[1] + data[2] + data[3] + data[4];
average = total / 5.0;
printf("Total %f Average %f\n", total, average);
return (0);
}
// Total 210.000000 Average 42.000000
การโปรแกรมภาษา C
นิธิ ทะนนท์
6
#include <stdio.h>
double data[5];
double total;
double average;
/* data to average and total */
/* the total of the data items */
/* average of the items */
int main() {
data[0] = 34.0;
data[1] = 27.0;
data[2] = 45.0;
data[3] = 82.0;
data[4] = 22.0;
for (int i=0; i<5; i++) {
total = total + data[i];
}
average = total / 5.0;
printf("Total %f Average %f\n", total, average);
return (0);
}
// Total 210.000000 Average 42.000000
การโปรแกรมภาษา C
นิธิ ทะนนท์
7
#include <stdio.h>
#define MAX_SIZE 5
double data[MAX_SIZE];
double total;
double average;
/* data to average and total */
/* the total of the data items */
/* average of the items */
int main() {
data[0] = 34.0;
data[1] = 27.0;
data[2] = 45.0;
data[3] = 82.0;
data[4] = 22.0;
for (int i=0; i<MAX_SIZE; i++) {
total = total + data[i];
}
average = total / MAX_SIZE;
printf("Total %f Average %f\n", total, average);
return (0);
}
// Total 210.000000 Average 42.000000
การโปรแกรมภาษา C
นิธิ ทะนนท์
8
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE 1000
int Random(int min, int max) {
return (int)( ((double)rand() / (RAND_MAX + 1)) * (max - min) + min );
}
int main() {
// seed the pseudorandom-number
srand(unsigned(time(NULL)));
int nArray[MAX_SIZE];
for (int i=0; i<MAX_SIZE; i++) {
nArray[i] = Random(1, 100);
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
9
int min = nArray[0];
int max = nArray[0];
int sum = 0;
double avg = 0.0;
for (int i=1; i<MAX_SIZE; i++) {
// sum
sum = sum + nArray[i];
// min
if (nArray[i] < min) {
min = nArray[i];
}
// max
if (nArray[i] > max) {
max = nArray[i];
}
}
avg = sum / MAX_SIZE;
printf("Min: %d\n", min);
printf("Max: %d\n", max);
printf("Sum: %d\n", sum);
printf("Avg: %f\n", avg);
return (0);
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
10
Strings
char name[4];
name[0] = 'S';
name[1] = 'a';
name[2] = 'm';
name[3] = '\0';
// NUL end-of-string
char name[4] = "Xyz";
name[1] = 'Y';
name[2] = 'Z';
การโปรแกรมภาษา C
นิธิ ทะนนท์
11
char name[4] = "Xyz";
name = "Sam";
// Illegal
strcpy(name, "Abc");
// Legal
char name[] = "Xyz";
strcpy(name, "Abc");
// 4 bytes
// Legal
char string[50];
strcpy(string,"Sam");
// 0 - 48, 49 NUL
char str[2] = "X";
char str = 'X';
// 2 bytes
// 1 byte
การโปรแกรมภาษา C
นิธิ ทะนนท์
12
#include <string.h>
#include <stdio.h>
int main()
{
char first[100];
char last[100];
char full_name[200];
/* first name */
/* last name */
/* full version of first and last name */
strcpy(first, "Steve");
strcpy(last, "Oualline");
strcpy(full_name, first);
/* Initialize first name */
/* Initialize last name */
/* full = "Steve" */
strcat(full_name, " ");
/* full = "Steve " */
strcat(full_name, last);
/* full = "Steve Oualline" */
printf("The full name is %s\n", full_name);
int
int
int
int
len = strlen(full_name);
cmp1 = strcmp("abc", "def");
cmp2 = strcmp("def", "abc");
cmp3 = strcmp("abc", "abc");
/*
/*
/*
/*
14 */
-1 */
1 */
0 */
return (0);
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
13
#include <string.h>
#include <stdio.h>
int main() {
char first[100];
printf("First name: ");
scanf("%s", first);
printf("First name: %s", first);
printf("Last name: ");
gets(first);
printf("First name: %s", first);
return (0);
}
First name: John Smith
First name: John
Last name: John Smith
First name: John Smith
การโปรแกรมภาษา C
นิธิ ทะนนท์
14
Multidimensional Arrays
// ประกำศตัวแปร
int matrix[2][4];
matrix[1][2] = 10;
// กำหนดค่ำเริม
่ ต ้น
int matrix[2][4] = {
{1, 2, 3, 4},
{10, 20, 30, 40}
};
// ใชค่้ ำคงที่
#define ROW 3
#define COL 2
int array[ROW][COL];
การโปรแกรมภาษา C
นิธิ ทะนนท์
15
#include <stdio.h>
int main()
{
int array[3][2];
array[0][0] = 0 *
array[0][1] = 0 *
array[1][0] = 1 *
array[1][1] = 1 *
array[2][0] = 2 *
array[2][1] = 2 *
10
10
10
10
10
10
+
+
+
+
+
+
0;
1;
0;
1;
0;
1;
for (int i=0; i<3; i++) {
for (int j=0; j<2; j++) {
printf("array[%d][%d]: %d ", i, j, array[i][j]);
}
printf("\n");
}
return (0);
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
16
#include <stdio.h>
#include <string.h>
int main() {
char student[5][80] = {
"ANT",
"BAT",
"CAT",
"DOG",
"RAT"
};
for(int i=0; i<5; i++) {
printf("student[%d]=%s", i, student[i]);
}
strcpy(student[0],
strcpy(student[1],
strcpy(student[2],
strcpy(student[3],
strcpy(student[4],
return (0);
"ANT");
"BAT");
"CAT");
"DOG");
"RAT");
}
การโปรแกรมภาษา C
นิธิ ทะนนท์