プログラミング入門II 実習課題 2019.07.12

Back to text page


実習課題

  1. 縦書き

    以下に示す5文字の単語5つからなる配列を用意します.

    	char word[5][6] = {"river", "table", "socks", "house", "shoes"};
    

    これを普通に表示した後,縦書に変えて表示するプログラムを作成しましょう.

    river
    table
    socks
    house
    shoes
    
    s  h  s  t  r
    h  o  o  a  i
    o  u  c  b  v
    e  s  k  l  e
    s  e  s  e  r
    

    縦書きの方は %c を使います.
  2. サイコロ野球ゲーム

    Webテキストに例示していた配列を用いて,プログラミング入門Iでも行ったサイコロ野球ゲームを改造して,条件分岐無しのプログラムにしましょう.

    	char bb[6][6][11] = {{"HR!", "Out", "Out", "Out", "Out", "Single hit"},
    			{"Out", "3B hit", "Out", "Out", "Single hit", "Out"},
    			{"Out", "Out", "2B hit", "Single hit", "Out", "Out"},
    			{"Out", "Out", "Single hit", "2B hit", "Out", "Out"},
    			{"Out", "Single hit", "Out", "Out", "Single hit", "Out"},
    			{"Single hit", "Out", "Out", "Out", "Out", "Single hit"}};
    

    Dice: 2 2
    3B hit
    

  3. 成績並べ替え

    これまで何度か成績の並べ替えを行ってきましたが,今回はまた別の表示パターンで行ってみましょう.6教科のテストの点数を乱数により0から100点までで発生させて,得点の高い順に並べ替えますが,以下のように表示させましょう.

    	char class[6][12] = {"English", "Mathematics", "Physics", "Chemistry", "History", "Geography"};
    	int score[6][2];
    

    English ---------  26
    Mathematics -----  95
    Physics ---------  12
    Chemistry -------   9
    History ---------  93
    Geography -------  76
    
    Mathematics -----  95
    History ---------  93
    Geography -------  76
    English ---------  26
    Physics ---------  12
    Chemistry -------   9
    

    教科書p.249の strlen() を使います.そのためには,ヘッダに <string.h> をインクルードすることも忘れないで下さい.
  4. 文字列並べ替え

    以下のようなチーム名の配列を用意して,文字数の多い順に並べるプログラムを作成しましょう.同じ数の場合の順番は気にしなくても大丈夫です.

    	char fc[10][34] = {"Arsenal Football Club",
    				"Liverpool Football Club",
    				"Manchester United Football Club",
    				"Ballspielverein Borussia Dortmund",
    				"Fussball-Club Bayern Muenchen",
    				"Real Madrid Club de Futbol",
    				"Futbol Club Barcelona",
    				"Paris Saint-Germain Football Club",
    				"Associazione Calcio Milan",
    				"Sanfrecce Hiroshima Football Club"};
    	char tmp[34];
    

    Sanfrecce Hiroshima Football Club
    Paris Saint-Germain Football Club
    Ballspielverein Borussia Dortmund
    Manchester United Football Club
    Fussball-Club Bayern Muenchen
    Real Madrid Club de Futbol
    Associazione Calcio Milan
    Liverpool Football Club
    Futbol Club Barcelona
    Arsenal Football Club
    

  5. 文字列の切り出し

    以下に示す配列を用意します.

    	char str[43] = "A quick brown fox jumps over the lazy dog";
    	char split[9][10];
    

    配列 str から各単語を切り出して,配列 split に一つ一つ入れていくプログラムを作成しましょう.

    プログラムは最後に以下の for 文を用意して,実際に配列に入っていることを確認します.

    	for(i=0; i<=8; i++)
    	{
    		printf("%s\n", split[i]);
    	}
    

    A
    quick
    brown
    fox
    jumps
    over
    the
    lazy
    dog
    

    1文字ずつ操作しないと作業できません.また,その時に,特殊文字である ' ' '\0' の扱い方がポイントです.

Back to text page