OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...P s ,;> uå&g5 Rb P...
Transcript of OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...P s ,;> uå&g5 Rb P...
// 配列とポインタの関係(後半)
int a[4] = {1, 2, 3, 4}; int* x = &a[0]; int* y = &a[2];
cout << x[0] << “ : ” << x[1] << endl; cout << y[0] << “ : ” << y[1] << endl;
int* z = a; cout << z[0] << “ : ” << z[1] << endl;
return 0; }
%./point1-a 1 : 2 3 : 4 1 : 2
出⼒結果
// 配列とポインタの関係(後半)
int a[4] = {1, 2, 3, 4}; int* x = &a[0]; int* y = &a[2];
cout << x[0] << “ : ” << x[1] << endl; cout << y[0] << “ : ” << y[1] << endl;
int* z = a; cout << z[0] << “ : ” << z[1] << endl;
return 0; }
%./point1-a 1 : 2 3 : 4 1 : 2
出⼒結果
a[0] a[1] a[2] a[3] x y
1 2 3 4
10 14 18 22 30 34………
10 18
x[0] : a[0]
y[0] : a[2]
x[0 + 1] : a[0]の隣の値 = a[1] y[0 + 1] : a[2] 隣の値 = a[3]
// 配列とポインタの関係(後半)
int a[4] = {1, 2, 3, 4}; int* x = &a[0]; int* y = &a[2];
cout << x[0] << “ : ” << x[1] << endl; cout << y[0] << “ : ” << y[1] << endl;
int* z = a; cout << z[0] << “ : ” << z[1] << endl;
return 0; }
%./point1-a 1 : 2 3 : 4 1 : 2
出⼒結果
a[0] a[1] a[2] a[3] x y
1 2 3 4
10 14 18 22 30 34………
10 18z10
// 配列とポインタの関係(後半)
int a[4] = {1, 2, 3, 4}; int* x = &a[0]; int* y = &a[2];
cout << x[0] << “ : ” << x[1] << endl; cout << y[0] << “ : ” << y[1] << endl;
int* z = a; cout << z[0] << “ : ” << z[1] << endl;
return 0; }
%./point1-a 1 : 2 3 : 4 1 : 2
出⼒結果
a[0] a[1] a[2] a[3] x y
1 2 3 4
10 14 18 22 30 34………
10 18
x[0] : a[0]
y[0] : a[2]
x[0 + 1] : a[0]の隣の値 = a[1] y[0 + 1] : a[2] 隣の値 = a[3]
// 配列引数を使った例 #include<iostream> using namespace std;
void incr(int x[], int sz){ for(int i = 0; i < sz; i++){
x[i]++; }
} int main(){
const int n = 5; int a[n] = {1, 2, 3, 4, 5};
incr(a, n);
for(int i = 0; i < n; i++){ cout << a[i] << “ ”;
} cout << endl; return 0;
} %./ar1 2 3 4 5 6
出⼒結果
// 配列引数を使った例 #include<iostream> using namespace std;
void incr(int x[], int sz){ for(int i = 0; i < sz; i++){
x[i]++; }
} int main(){
const int n = 5; int a[n] = {1, 2, 3, 4, 5};
incr(a, n);
for(int i = 0; i < n; i++){ cout << a[i] << “ ”;
} cout << endl; return 0;
} %./ar1 2 3 4 5 6
出⼒結果
// 配列引数を使った例の別の書き方 #include<iostream> using namespace std;
void incr(int* x, int sz){ for(int i = 0; i < sz; i++){
x[i]++; }
} int main(){
const int n = 5; int a[n] = {1, 2, 3, 4, 5};
incr(&a[0], n);
for(int i = 0; i < n; i++){ cout << a[i] << “ ”;
} cout << endl; return 0;
}
int* x int x[]
incr(a, n) incr(&a[0],n)
// print 関数は配列の中身を変更しない #include<iostream> using namespace std;
void incr(int x[], int sz){ for(int i = 0; i < sz; i++){
x[i]++; }
} void print(const int x[], int sz){
for(int i = 0; i < sz; i++){ cout << x[i] << “ ”;
} cout << endl;
}
int main(){ const int n = 5; int a[n] = {1, 2, 3, 4, 5};
incr(a, n); print(a,n);
return 0; }
%./ar2 2 3 4 5 6
出⼒結果
// 関数で配列に入力する #include<iostream> using namespace std;
int input(int x[], int max){ int num = 0; while(num < max && cin >> x[num]){
num++; }
return num; }
void print(const int x[], int num){ for(int i = 0; i < num; i++){
cout << x[i] << “ ”; } cout << endl;
}
int main(){ const int size = 10; int a[size] = {};
int n = input(a,size); print(a,n);
return 0; }
%./ar3 1 2 3 4 5 6 7 8 9 (Ctrl+d) 1 2 3 4 5 6 7 8 9
出⼒結果
// 関数で配列に入力する #include<iostream> using namespace std;
int input(int x[], int max){ int num = 0; while(num < max && cin >> x[num]){
num++; }
return num; }
void print(const int x[], int num){ for(int i = 0; i < num; i++){
cout << x[i] << “ ”; } cout << endl;
}
int main(){ const int size = 10; int a[size] = {};
int n = input(a,size); print(a,n);
return 0; }
%./ar3 1 2 3 4 5 6 7 8 9 (Ctrl+d) 1 2 3 4 5 6 7 8 9
出⼒結果
// 関数で配列に入力する #include<iostream> using namespace std;
int input(int x[], int max){ int num = 0; while(num < max && cin >> x[num]){
num++; }
return num; }
void print(const int x[], int num){ for(int i = 0; i < num; i++){
cout << x[i] << “ ”; } cout << endl;
}
int main(){ const int size = 10; int a[size] = {};
int n = input(a,size); print(a,n);
return 0; }
%./ar3 1 2 3 4 5 6 7 8 9 (Ctrl+d) 1 2 3 4 5 6 7 8 9
出⼒結果
// 関数で配列に入力する #include<iostream> using namespace std;
int input(int x[], int max){ int num = 0; while(num < max && cin >> x[num]){
num++; }
return num; }
void print(const int x[], int num){ for(int i = 0; i < num; i++){
cout << x[i] << “ ”; } cout << endl;
}
int main(){ const int size = 10; int a[size] = {};
int n = input(a,size); print(a,n);
return 0; }
%./ar3 1 2 3 4 5 6 7 8 9 (Ctrl+d) 1 2 3 4 5 6 7 8 9
出⼒結果
// 配列の途中の要素のアドレス #include<iostream> using namespace std;
int main(){
int a[4] = {1, 2, 3, 4}; int* x = &a[2];
for(int i = 0; i < 2; i++){ cout << x[i] << endl;
}
return 0; }
%./ar5 3 4
出⼒結果
a[0] a[1] a[2] a[3] x y
1 2 3 4
10 14 18 22 30 34………
x[0] : a[2] x[1] : a[3]
./lesson9-1 初期配列 : 3.3 4.4 5.5 6.6 7.7 8.8 自乗結果 : 10.89 19.36 30.25 43.56 59.29 77.44
#include<iostream> using namespace std;
int main(){ const int n = 6;
double ar[n] = {3.3,4.4,5.5,6.6,7.7,8.8};
cout << “初期配列 : ” ; print( );
square( ); cout << “自乗結果 : ” ; print( );
return 0; }
// 2次元配列の宣言と代入 #include<iostream> using namespace std;
int main(){
char c[3][5];
c[1][4] = ‘L’;
return 0; }
L
// 2次元配列の初期化 #include<iostream> using namespace std;
int main(){ char c[3][5] = {{‘a’,’b’,‘c’,‘d’,‘e’}, {‘f’,‘g’,‘h’,‘i’,‘j’}, {‘k’,’l’,‘m’,‘n’,‘o’}}; char d[3][5] = {‘a’,’b’,‘c’,‘d’,‘e’,‘f’,‘g’,‘h’,‘i’,‘j’,‘k’,’l’,’m’,‘n’,‘o’}; return 0; }
// 2次元配列の初期化 #include<iostream> using namespace std;
int main(){ char c[3][5] = {}; char d[3][5] = {0};
return 0; }
// 2次元配列の初期化 #include<iostream> using namespace std;
int main(){
char c[][] = {};
char d[3][] = {};
char e[][5] = {};
return 0; }
c[0][0]10// 2次元配列のメモリマップ #include<iostream> using namespace std;
int main(){
char c[2][3] = {};
return 0; }
c[0][1]
c[0][2]
c[1][0]
c[1][1]
c[1][2]
11
12
13
14
15
………
c[0][0] A10// 2次元配列のメモリマップ #include<iostream> using namespace std;
int main(){
char c[][3] = {{‘A’, ‘B’, ‘C’},{‘D’, ‘E’, ‘F’}};
return 0; }
c[0][1] B
c[0][2] C
c[1][0] D
c[1][1] E
c[1][2] F
11
12
13
14
15
………
空き
c[0][0] A10// 2次元配列のメモリマップ #include<iostream> using namespace std;
int main(){
char c[][] = {{‘A’, ‘B’, ‘C’},{‘D’, ‘E’, ‘F’}};
return 0; }
c[0][1] B
c[0][2] C
c[1][0] D
c[1][1] E
c[1][2] F
11
12………
あと何個?
あと何個?
c[0][0] A10// 2次元配列のメモリマップ #include<iostream> using namespace std;
int main(){
char c[2][] = {{‘A’, ‘B’, ‘C’},{‘D’, ‘E’, ‘F’}};
return 0; }
c[0][1] B
c[0][2] C
c[1][0] D
c[1][1] E
c[1][2] F
11
12………
あと何個?
あと何個?
#include<iostream> using namespace std;
int main(){
const int n = 3, m = 4; double a[n][m] = { { 2.3, 8.4, 4.3, 1.2},
{ 8.2, 1.3, 8.1, 7.5}, { 3.3, 3.1, 9.8, 5.3} };
for( int i=0; i < n; i++){ for(int j =0; j < m; j++){
cout << a[i][j] << “ ”; } cout << endl;
}
return 0; }
a[0][0],a[0][1],a[0][2],a[0][3] a[1][0],a[1][1],a[1][2],a[1][3] a[2][0],a[2][1],a[2][2],a[2][3]
%./arrout 2.3 8.4 4.3 1.2 8.2 1.3 8.1 7.5 3.3 3.1 9.8 5.3
出⼒結果
#include<iostream> using namespace std;
int main(){
const int n = 3, m = 4; double a[n][m] = {};
for( int i=0; i < n; i++){ for(int j =0; j < m; j++){
cin >> a[i][j]; }
} // 出力の記述は省略
return 0; }
a[0][0],a[0][1],a[0][2],a[0][3] a[1][0],a[1][1],a[1][2],a[1][3] a[2][0],a[2][1],a[2][2],a[2][3]
%./arrin 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
出⼒結果
�a bc d
�
2行
�a bc d
�1行
1列 2列
A =
�a bc d
�AT =
�a cb d
�
#include<iostream> using namespace std;
int main(){
const int n = 3, m = 4; int data[n][m] = {};
for( int i=0; i < n; i++){ for(int j =0; j < m; j++){
cin >> data[i][j]; }
}
cout << endl;
}
for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){
cout << data[i][j] << “ ”; } cout << endl;
} cout << endl;
for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){
cout << data[j][i] << “ ”; } cout << endl;
} cout << endl;
return 0; }
DATA =
�1 23 4
�
DATAT =
�1 32 4
�
data[0][0],data[0][1] data[1][0],data[1][1]
data[0][0],data[1][0] data[0][1],data[1][1]
// print 関数は配列の中身を変更しない #include<iostream> using namespace std;
void incr(int x[], int sz){ for(int i = 0; i < sz; i++){
x[i]++; }
} void print(const int x[], int sz){
for(int i = 0; i < sz; i++){ cout << x[i] << “ ”;
} cout << endl;
}
int main(){ const int n = 5; int a[n] = {1, 2, 3, 4, 5};
incr(a, n); print(a, n);
return 0; }
%./ar2 2 3 4 5 6
出⼒結果
a[0] a[1] a[2] a[3]
1 2 3 4
10 14 18 22 30 34………
./lesson9-1 初期配列 : 3.3 4.4 5.5 6.6 7.7 8.8 自乗結果 : 10.89 19.36 30.25 43.56 59.29 77.44
#include<iostream> using namespace std;
int main(){ const int n = 6;
double ar[n] = {3.3,4.4,5.5,6.6,7.7,8.8};
cout << “初期配列 : ” ; print( );
square( ); cout << “自乗結果 : ” ; print( );
return 0; }
// 2 次元配列を出力する関数 #include<iostream> using namespace std;
void print(const int x[][3], int sz){ for(int i = 0; i < sz; i++){
for(int j = 0; j < 3; j++){ cout << x[i][j] << “ ”;
} cout << endl;
} cout << endl;
}
int main(){ const int n = 2, m = 3; int a[n][m] = {{1, 2, 3}, {4, 5, 6}};
print(a,n);
return 0; }
c[0][0] A10
c[0][1] B
c[0][2] C
c[1][0] D
c[1][1] E
c[1][2] F
11
12
13
14
15
………
空き
// 2次元配列のメモリマップ #include<iostream> using namespace std;
void print(char a[][3],int sz){ for(int i = 0; i < sz; i++){
for(int j = 0; j < 3; j++){ cout << a[i][j] << “ ”;
} cout << endl;
} cout << endl;
}
int main(){
char c[2][3] = {{‘A’, ‘B’, ‘C’},{‘D’, ‘E’, ‘F’}};
print(c,2);
return 0; }
c[0][0] A10
c[0][1] B
c[0][2] C
c[1][0] D
c[1][1] E
c[1][2] F
11
12
13
14
15
………
空き
// 2次元配列のメモリマップ #include<iostream> using namespace std;
void print(char a[][],int sz){ for(int i = 0; i < sz; i++){
for(int j = 0; j < 3; j++){ cout << a[i][j] << “ ”;
} cout << endl;
} cout << endl;
}
int main(){
char c[2][3] = {{‘A’, ‘B’, ‘C’},{‘D’, ‘E’, ‘F’}};
print(c,2);
return 0; }
// 各行の先頭アドレスを渡す #include<iostream> using namespace std;
void print_capital(const char s[]){ for(int i =0; s[i] != ‘\0’; i++){
char c = s[i]; if(c >= ‘a’ && c >= ‘z’){
c = ‘A’ + (c - ‘a’); } cout << c;
} cout << endl;
}
int main(){ const int row = 3, col = 5; const char str[row][col] = {“a1cd”, “2eg”, “hi”};
for(int i =0; i < row; i++){ print_capital(str[i]);
} return 0;
}
// 各行の先頭アドレスを渡す #include<iostream> using namespace std;
void print_capital(const char s[]){ for(int i =0; s[i] != ‘\0’; i++){
char c = s[i]; if(c >= ‘a’ && c >= ‘z’){
c = ‘A’ + (c - ‘a’); } cout << c;
} cout << endl;
}
int main(){ const int row = 3, col = 5; const char str[row][col] = {“a1cd”, “2eg”, “hi”};
for(int i =0; i < row; i++){ print_capital(str[i]);
} return 0;
}
./lesson9-2 -- 個人平均 -- 1人目 : 20 点 2人目 : 30 点 3人目 : 40 点 4人目 : 50 点 5人目 : 60 点 -- 科目平均 -- 1科目目 : 30 点 2科目目 : 40 点 3科目目 : 50 点
int main(){
const int nums = 5; // 保存されている人数 const int clas = 3; // 科目数
int scores[nums][clas] = {{10,20,30},{20,30,40},{30,40,50},{40,50,60},{50,60,70}};
// 5人の国語,算数,英語の得点が格納されている.
ave_person(scores, nums, clas); ave_class(scores, nums, clas);
return 0; }
#include<iostream> using namespace std;
// main 文は省略
#include<iostream> using namespace std;
// main 文は省略