http://math.sci.hiroshima-u.ac.jp/ ~ryo/lectures/informatics2010/ 1 2 C ATM etc. etc. (Personal Computer) 3 4
Input Output Device Central Processing Unit I/O CPU Memory 5 6 (CPU),,... etc. C, Java, Fortran... etc. CPU CPU 7 8
.bashrc Src u012345 Desktop / fs a.out prog.c Exec Mihon Src Informatics hello.c a.out home1 ryokoba bin Mihon home2 u012345 Desktop ~...../.. Exec Src Informatics./a.out a.out /home/user2/ryokoba/mihon/src../../bin 9 10 GUI Graphical User Interface GUI GUI GUI cd + ls & rm 11 12
ls : $ ls -l : ls pwd : cd : $ cd ~/Informatics/Mihon $ cd../.. $ cd ~ mkdir : $ mkdir New New cp : $ cp source_file target_file target_file source_file $ cp source_file target_directory target_directory source_file mv : $ mv source_file target_file source_file target_file $ mv source_file target_directory target_directory source_file rm : rm 13 14 Unixvi, emacs,... gedit MacXcode, mi,... Windows, TeraPad,... C #include <stdio.h> int main(void) {!... }... C printf( ) main( ) ( ) main 15 16
hello.c C hello.c a.out $ cc hello.c $./a.out a.out #include <stdio.h> int main(void) {! printf( Hello!!\n ); } printf( ) Tab Key 17 18 1 0 0 1 0 1 1 0 2 8 = 256 2 10 =1024 1K 19 C int, unsigned int, long, unsigned long, short, unsigned short, float, double char, unsigned char 2 2. 2.0 20
int a; a int a 2 int a int 2 a int a; double x; double x size_of_variables.c 21 22 C =, Ex. C C == 23 24
int a; int a = 0; initialize.c 25 substitution.c swap.c 26 swap1.c 2 swap2.c output_values.c 27 28
printf("input a: "); scanf("%d", &a); & double %f %lf input_values.c a, b 3 swap3.c! 29 30 int double int int int double 3/5 0.6 double double double type1.c, type2.c 1. values1.c 2. values2.c 31 32
(i) 33 (ii) 34 C 35 > < = 36
(true) (false) > == >!= comparison.c 37 38 and or not logical_operator.c 39 if 40
if even_odd.c 1. max_of_two 2. 3. common_multiple x = 2, y = 3 absolute_value 41 42 max_of_two common_multiple 43 44
common_multiple absolute_value 45 46 intercalary_year 100 400 = + 4 100 400 = 365.24219 40097 1 97.0/400=0.2425 47 48
if if_in_if.c if { } if (indent) max_of_three 49 50 (iii) for 51 52
for print_counter.c 53 for 54 for sum_up2 sum_up 55 print_double_counter.c 56
kuku indian_kuku chess_board 57 58 while while square.c 59 60
while print_counter.c while for_by_while sum_over judge_prime (1) for_by_while.c 61 62 (2) sum_over.c (2) sum_over.c 63 64
i 65 66 gcd(x, y) x, y (greatest common divisor) 0 gcd(x, y) = gcd(y, x%y) gcd(x, 0) = x gcd(20, 14) = gcd(14, 6) = gcd(6, 2) = gcd(2, 0) = 2 y Ex. gcd(x, y) = gcd(y, x%y) x = py + q p q x x y y (0 q<y) y, q x x, y q = x py x, y q y, q x, y = y, q gcd(x, y) = gcd(y, q) 67 68
y x x, y y gcd(20, 14) = gcd(14, 6) = gcd(6, 2) = gcd(2, 0) = 2 14 20 2 2. euclid 69 70 71 (array) int int 72
set_array.c set_array2 random_number.c 73 #include <stdio.h> int main(void) {! int x[5], i;! for (i = 0; i < 5; i++)! {!! x[i] = i * i;! }! printf("data of the array x\n");! for (i = 0; i < 5; i++)! {!! printf("x[%d] = %d\n", i, x[i]);! } } define #include <stdio.h> #define N 5 int main(void) {! int x[n], i;! for (i = 0; i < N; i++)! {!! x[i] = i * i;! }! printf("data of the array x\n");! for (i = 0; i < N; i++)! {!! printf("x[%d] = %d\n", i, x[i]);! } } 74 rand() srand(seed) seed 75 76
Mihon/Src/max_of_array.c max_of_array table_of_primes statistics m = 1 N x i V = 1 N (x i m) 2 σ = V N N i=1 i=1 x #include <math.h> 77 78 #include <stdio.h> #include <glsc.h> int main(void) {! double x_window = 100.0, y_window = 100.0;! g_init("graph", x_window, y_window);! g_device(g_disp);! g_sleep(2.0);! g_term(); } ex0.c 79 80
(0, 0) x_std g_def_scale(scale_number, x_left, x_right, y_bottom, y_top,! x_left_std, y_top_std, x_width_std, y_width_std); x_left x_right y_top y_top_std y_width_std y_std x_left_std x_width_std (x_window, y_window) y_bottom 81 82 g_init("graph", x_window, y_window); g_device(g_disp); g_def_scale(0, x_left, x_right, y_bottom, y_top, x_left_std, y_top_std, x_width_std, y_width_std); g_cls(); g_sel_scale(0); g_box(0.0, 1.0, 0.0, 1.0, G_YES, G_NO); g_move(0.5, 0.0); g_plot(1.0, 1.0); g_plot(0.0, 0.5); g_plot(0.5, 0.0); color g_sleep(g_stop); g_term(); g_line_color(color); g_area_color(color); 83 84
g_line_width(width); width g_line_type(type); type g_text(x_std, y_std, Hello ); 85 86 y (x i,y i ) x (x i,y i ) y i y y = f(x) y = f(x) x i x x i = x 0 + i x (i =0, 1,,I) y i = f(x i ) (x i,y i ) (x i,y i ) f(x i ) 87 88