Download include

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Arrays
Nithi Thanon
Computer Science
Prince of Songkla University
2
Bubble Sort
การโปรแกรมภาษา C
นิธิ ทะนนท์
3
#include <stdio.h>
#define MAX 5
void main()
{
int number[MAX] = {1, 5, 4, 5, 2};
int temp, bottom, i;
bool exchanged = true;
bottom = MAX - 2;
while ( exchanged )
{
exchanged = false;
for (i = 0; i <= bottom; i++)
{
if (number[i] > number[i+1])
{
temp = number[i];
//exchange
number[i] = number[i+1];
number[i+1] = temp;
exchanged = true; //exchange is made
}
}
bottom--;
}
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
4
#include <stdio.h>
#define MAX 5
void BubbleSort(int number[], int max) {
int temp, i;
bool exchanged = true;
int bottom = max - 2;
while (exchanged) {
exchanged = false;
for (i = 0; i <= bottom; i++) {
if (number[i] > number[i+1]) {
temp = number[i];
//exchange
number[i] = number[i+1];
number[i+1] = temp;
exchanged = true;
//exchange is made
}
}
bottom--;
}
}
void main() {
int arr[MAX] = {1, 5, 4, 5, 2};
BubbleSort(arr, 5);
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
5
Binary Search
Search 44
low
#1 0
#2 5
#3 5
high
8
8
5
mid
4
6
5
high
8
8
5
5
mid
4
6
5
Search 45
#1
#2
#3
#4
low
0
5
5
6
การโปรแกรมภาษา C
นิธิ ทะนนท์
6
#include <stdio.h>
#define MAX 10
void main() {
int number[MAX] = {2, 5, 12, 17, 23, 38, 44, 77, 84, 90};
int low = 0;
int high = MAX - 1;
int mid = (low + high) / 2;
int searchValue = 45;
while ( (low <= high) && (number[mid] != searchValue) ) {
if (number[mid] < searchValue) {
low = mid + 1;
}
else { //number[mid] > searchValue
high = mid - 1;
}
mid
= (low + high) / 2; //integer division will truncate
}
if ( low > high) {
printf("NOT_FOUND");
}
else {
printf("Index: %d, Data: %d", mid, number[mid]);
}
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
7
#include <stdio.h>
#define MAX 10
int BinarySearch(int number[], int max, int searchValue) {
int low = 0;
int high = max - 1;
int mid
= (low + high) / 2;
while ( (low <= high) && (number[mid] != searchValue) ) {
if (number[mid] < searchValue) {
low = mid + 1;
}
else { //number[mid] > searchValue
high = mid - 1;
}
mid
= (low + high) / 2; //integer division will truncate
}
if ( low > high) {
return -1;
}
else {
return mid;
}
}
void main()
{
int arr[MAX] = {2, 5, 12, 17, 23, 38, 44, 77, 84, 90};
printf("%d", BinarySearch(arr, 10, 78));
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
8
Arrays of Structures
struct bin {
char name[30]; /* name of the part */
int quantity;
/* how many are in the bin */
int cost;
/* The cost of a single part (in cents) */
};
struct bin printer_cable_bin;
strcpy(printer_cable_bin.name, "Printer Cables");
printer_cable_bin.quantity = 12;
printer_cable_bin.cost = 1000;
int total_cost = printer_cable_bin.cost * printer_cable_bin.quantity;
การโปรแกรมภาษา C
นิธิ ทะนนท์
9
struct time {
int hour;
int minute;
int second;
};
/* 0-24 */
/* 0-59 */
/* 0-59 */
const int MAX_LAPS = 4;
struct time lap[MAX_LAPS];
lap[0].hour = 10;
lap[0].minute = 20;
lap[0].second = 30;
struct time start_stop[2] = {
{10, 10, 10},
{12, 12, 12}
};
การโปรแกรมภาษา C
นิธิ ทะนนท์
10
Array and Function
// Pass by value
void swap(int a, int b) {
int t;
t = a;
a = b;
b = t;
}
void main() {
int x = 1;
int y = 2;
swap(x, y); // x:1, y:2
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
11
// Pass by reference
void swap(int &a, int &b) {
int t;
t = a;
a = b;
b = t;
}
void main() {
int x = 1;
int y = 2;
swap(x, y); // x:2, y:1
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
12
// Pass by Reference
void func(int a[]) {
a[0] = 4;
a[1] = 5;
a[2] = 6;
}
void main() {
int arr[3] = {1, 2, 3};
func(arr); // {4, 5, 6}
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
13
#include <stdio.h>
#define MAX 5
void printarr(int a[MAX]) {
for(int i=0; i<5; i++) {
printf("value in array %d\n", a[i]);
}
}
void main() {
int a[MAX];
for(int i=0; i<MAX; i++) {
a[i]=i;
}
printarr(a);
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
14
#include <stdio.h>
#define ROW 3
#define COL 2
void printarr(int a[ROW][COL]) {
for (int i=0; i<ROW; i++) {
for (int j=0; j<COL; j++) {
printf("array[%d][%d]: %d ", i, j, a[i][j]);
}
printf("\n");
}
}
void main() {
int a[ROW][COL];
for(int i=0; i<ROW; i++) {
for(int j=0; j<COL; j++) {
a[i][j] = i*10+j;
}
}
printarr(a);
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
15
Text File

Write file
FILE *fp;
// fp = fopen("array_data.txt", "r+t");
// fp = fopen("array_data.txt", "a+t");
fp = fopen("array_data.txt", "w+t");
if (fp ==NULL) {
printf("File not found !");
}
else {
fprintf(fp, "%d \n", 100);
fclose(fp);
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
16

Read File
FILE *fp;
fp = fopen("array_data.txt", "r+t");
if (fp ==NULL) {
printf("File not found !");
}
else {
int data = 0;
fscanf(fp, "%d", &data);
fclose(fp);
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
17
#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
srand(unsigned(time(NULL)));
// File
FILE *fp;
fp = fopen("array_data.txt", "w+t");
if (fp ==NULL) {
printf("File not found !");
}
else {
for (int i=1; i<=MAX_SIZE; i++) {
fprintf(fp, "%d \n", Random(1, 100));
}
fclose(fp);
}
return (0);
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
18
#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() {
// File
FILE *fp;
fp = fopen("array_data.txt", "r+t");
if (fp ==NULL) {
printf("File not found !");
}
else {
int data = 0;
for (int i=0; i<MAX_SIZE; i++) {
fscanf(fp, "%d", &data);
printf("%d\n", data);
}
fclose(fp);
}
return (0);
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
19
#include <stdio.h>
#define MAX 2000
int main() {
int numbers[MAX];
int count=0;
FILE *fp;
fp = fopen("BubbleSort.txt", "r+t");
if (fp == NULL) {
printf("File not found!");
}
else {
while (!feof(fp)) {
fscanf(fp, "%d", &numbers[count]);
count++;
}
fclose(fp);
}
return (0);
}
การโปรแกรมภาษา C
นิธิ ทะนนท์
Related documents