1 / 16

小林 学

湘南工科大学. 2011 年 11 月 15 日. 情報理論2 第6回. 小林 学. 〒251-8511  神奈川県藤沢市辻堂西海岸 1-1-25. Tel. 0466-30-0232( 直通 ). Fax. 0466-34-5932. kobayasi@info.shonan-it.ac.jp. Page 2. [ 前回の課題 1] 数を数える(カウント). [ 課題 1] 次の配列の 2 以上の数 を数えるプログラムを作成しなさい. [1]. [4]. Data. [0]. [3]. [2]. 実行結果.

gili
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年11月15日 情報理論2 第6回 小林 学 〒251-8511 神奈川県藤沢市辻堂西海岸1-1-25 Tel. 0466-30-0232(直通) Fax. 0466-34-5932 kobayasi@info.shonan-it.ac.jp

  2. Page 2 [前回の課題1] 数を数える(カウント) [課題1]次の配列の2以上の数を数えるプログラムを作成しなさい [1] [4] Data [0] [3] [2] 実行結果 用意する変数: count(1の数を入れる) 初期値: count = 0

  3. Page 3 [前回の課題1] 数を数える(カウント) i Data[i] count 初期値: 0 繰り返し: 0 20→1 1 1 1 2 21→2 3 42→3 4 0 2 if(Data[i]>=2) count++;

  4. Page 4 [前回の課題1の解答] #include<stdio.h> void main(void){ int i, Data[5]={2, 1, 2, 4, 0}; //データの初期化 int count; count = 0; //カウントの初期化 for(i=0;i<5;i++){ if(Data[i]>=2) count++; //Data[i]が1ならば //countを1増やす } printf("Data中の2以上の数:%d\n",count); }

  5. Page 5 アルゴリズム:最大値・最小値 最大の位置 [1] [4] Data [0] [3] [2] void main(void){ int Data[5]={5, 3, 6, 8, 2};//配列Dataの初期化 実行結果 用意する変数: max(最大値の候補), position(最大の位置の候補) 初期値: max = Data[0] position = 0

  6. アルゴリズム:最大値・最小値 Page 6 i Data[i] max position 初期値: 5 0 繰り返し: (for文) 1 3 5 0 2 6 5→60→2 3 8 6→82→3 4 2 8 3 if(Data[i]>max){ max = Data[i]; position = i; } [課題2]配列Data[5]={5, 3, 6, 8, 2}の最大値を求めるプログラムを作成しなさい

  7. Page 7 [前回の課題2の解答] #include<stdio.h> void main(void){ int i, Data[5]={5, 3, 6, 8, 2}; //データの初期化 int max, position; max = Data[0]; //初期化 position = 0; //初期化 for(i=1;i<5;i++){ if(Data[i]>max){ max = Data[i]; position = i; } } printf("最大の位置:%d\n", position); printf("最大値:%d\n", max); }

  8. Page 8 [前回の課題3]キーボードから5個の整数を入力し,最小値(min)と最小の位置(position)を求めるプログラムを作成しなさい. ヒント: printf("5つの整数を入力:\n"); for(i=0;i<5;i++) scanf("%d",&Data[i]);

  9. Page 9 [前回の課題3の解答] #include<stdio.h> void main(void){ int i, Data[5],min, position; printf("5つの整数を入力:\n"); for(i=0;i<5;i++) scanf(“%d”,&Data[i]); //入力 min = Data[0]; //初期化 position = 0; //初期化 for(i=1;i<5;i++){ if(Data[i] < min){ min = Data[i]; position = i; } } printf(“最小の位置:%d\n", position); printf(“最小値:%d\n", min); }

  10. データ構造2:キュー Page 10 画面出力 [1] [4] Data [0] [3] [2] DataNo 1 Input(9) 2 Input(22) 3 Output 2 13 Output 1 9

  11. Page 11 [課題1] キュー構造について,以下の問に答えよ int DataNo=0; :データの数を表す int 型変数 int Data[5]; :データを格納する int 型配列 • 上の定義に対して,以下の命令を実行したときの配列Data の内容と DataNo の変化,画面出力を示しなさい • Input(2) → Output → Input(4) → Input(7) → Output • (1)と同様に,以下の結果を示しなさい • Input(4) → Input(12) → Input(22) → Output → Output → Output

  12. Page 12 [課題2] 配列 Data の内容をシフトさせる以下のプログラムを完成させなさい #include<stdio.h> void main(void){ int i, Data[5]={5,7,2,9,3}; printf("Data="); for(i=0;i<5;i++) printf(" %d",Data[i]); //ここに配列をシフトさせるプログラムを書く printf("\nData="); for(i=0;i<5;i++) printf(" %d",Data[i]); } [実行結果]

  13. Page 13 [課題3] 次ページのスタックのプログラムを修正して,以下のキューを実現するプログラムを作成しなさい [実行例]

  14. Page 14 [参考2:スタックのプログラム] #include<stdio.h> void main(void){ int select, i, DataNo=0, Data[5], input; while(1){ printf("\nInputならば0,Outputならば1を入力:"); scanf("%d",&select); if(select==0){ printf("入力データ:"); scanf("%d",&input); Data[DataNo] = input; DataNo++; }else{ DataNo--; printf("出力:%d\n",Data[DataNo]); } printf("Data="); for(i=0;i<DataNo;i++) printf(" %d",Data[i]); } }

  15. Page 15 [課題4]データ構造をキューとし,Input あるいはOutput されるたびにキュー(配列Data)の中の最大値と最大の位置を画面出力するプログラムを作成しなさい.

  16. Page 16 実行結果

More Related