1 / 8

情報演習 ~データ構造:配列~

情報演習 ~データ構造:配列~. 2011 年 1 月 11 日 笠井俊信. 演習問題 1. 問題:  直角三角形の辺の長さの組み合わせを全て示せ. 3 辺は全て整数値.最大辺の上限を入力.. A. C. A 2 =B 2 +C 2. B. 最大値の上限は? 10 A B C  5  4  3 10  8  6. 演習問題 1 ( C プログラム). #include <stdio.h> void main(void) { int a, b, c, n; scanf(”%d”, &n); for(a=1;a<=n;a++)

igor-hester
Télécharger la présentation

情報演習 ~データ構造:配列~

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 情報演習~データ構造:配列~ 2011年1月11日 笠井俊信

  2. 演習問題1 問題:  直角三角形の辺の長さの組み合わせを全て示せ. 3辺は全て整数値.最大辺の上限を入力. A C A2=B2+C2 B 最大値の上限は? 10 ABC  5  4  3 10  8  6

  3. 演習問題1(Cプログラム) #include <stdio.h> void main(void) { int a, b, c, n; scanf(”%d”, &n); for(a=1;a<=n;a++) for(b=1;b<=a;b++) for(c=1;c<=b;c++) if(a*a==b*b+c*c) printf(“%d %d %d”,a,b,c); } • 最大値を入力 ー> n • 変数を宣言 a, b, c: 整数型 • aを1からnまで繰り返す • bを1からaまで繰り返す • cを1からbまで繰り返す • a2=b2+c2ならば • a, b, c の値を出力

  4. 新しいデータ型 • 今までのデータ型 • int a; ・・・ 整数型の変数1つを宣言 • 今日の新しいデータ型 • int a[5]; ・・・整数型の変数5個の配列を宣言 • int a[] = {1,2,3,4,5}; 変数の宣言と初期化 • 配列の参照の仕方() • 1番目のデータ → a[0] • 最後のデータ → a[4]

  5. 演習問題1(復習) 問題: 2つの数を入力し,その大きい方を出力する. アルゴリズムを示せ(日本語で) ※2つの数値を配列に格納せよ • 2つの数値を入力(A,B)する • (条件分岐):大きい方を求める • もしAがBよりも大きいならば,Aを出力. • そうでないなら,Bを出力.

  6. Cプログラムの例1 #include <stdio.h> void main(void) { int a[2]; scanf(“%d”, &a[0]); scanf(“%d”, &a[1]); if(a[0]>a[1]){ printf(“%d\n”, a[0]); } else{ printf(“%d\n”, a[1]); } } • 2つの数値を入力(A,B)する • (条件分岐):大きい方を求める • もしAがBよりも大きいならば,Aを出力. • そうでないなら,Bを出力.

  7. 演習問題2(復習) 問題:  整数データを10個入力していき,合計と平均 (整数値)を求めて表示する手順を示せ. ※10個のデータは配列に格納せよ • カウンタと変数SUMの値を0に設定 • (繰り返し)カウンタの値が10未満である限り • 数値を入力する(配列のカウンタ番目に) • 入力された数値をSUMに加算 • カウンタの値を1だけ増やす • 合計値(SUM)を出力 • 平均値(SUM/10)を出力

  8. 演習問題2(Cプログラム) #include <stdio.h> void main(void) { int dat[10], sum, i; sum=0; for (i=0;i<10;i++){ scanf("%d", &dat[i]); sum = sum + dat[i]; } printf("合計 = %d\n", sum); printf("平均 = %d\n", sum/i); } • カウンタと変数SUMの値を0に設定 • (繰り返し)カウンタの値が10未満である限り • 数値を入力する • 入力された数値をSUMに加算 • カウンタの値を1だけ増やす • 合計値(SUM)を出力 • 平均値(SUM/10)を出力

More Related