Ex02Ans

From Prog0

Jump to: navigation, search

演習第2回

Contents

解答例

A問題

A-1 変数名の規則

ファイル名: ex02a1.txt

(x)int  666pokemon;      // 変数名は数字で始めてはいけない 
(o)int  _666pokemon;     
(x)int  class.1;           // 「.」 は使用できない
(o)int  num1, num2;      
(x)int  good-day;        // 変数名が「-」を含んでいる
(o)int  __init__;        
(x)int  for = 0;           // 「for」は予約語のため
(o)int  a1,a2=0;           
(o)int  speed2fast;      
(x)int  prog@uoa;        // 変数名が「@」を含んでいる

A-2 printf

ファイル名: ex02a2.c

#include <stdio.h>

int main() 
{
  int num;

  printf("整数値を入力してください:");
  scanf("%d", &num);

  printf("入力された値は %d で、これを5倍した結果は %d です。\n", num, num*5);

  return 0;
}

A-3 演算と演算子

ファイル名: ex02a3.txt

(o)result = (i + j) * k;          // 正しい式。計算結果:(1 + 2) * 3 = 9
(o)result = i + (j * k) / (k - 1);  // 正しい式。計算結果:1 + (2 * 3) / 2 = 1 + 6 / 2 = 1 + 3 = 4
(x)result = (i + (j - k);         // 括弧が閉じていない 
(x)i + j + k = result;            // 左辺は値であり、変数ではない
(x)result = 3k;                   // 3k は文法的に不正 、乗算の "*" が必要
(o)result = (k / (j * 2));       // 正しい式。計算結果:3 / (2 * 2) = 3 / 4 = 0(整数除算)
(x)result = j+*k;               // 「+*」 という演算子はない
(o)result = result + i*j;         // 正しい式。計算結果:0 + 1 * 2 = 2

B問題

B-1 平成/西暦変換

ファイル名: ex02b1.c

#include <stdio.h>

int main()  
{
  int heisei;

  printf("年を平成で入力してください [1-31]: ");
  scanf("%d", &heisei);

  printf("平成 %d 年は西暦 %d 年です。\n", heisei, heisei+1988);
  printf("平成 %d 年に生まれた人は今年 %d 才です。\n", heisei, 2025-(heisei+1988));

  return 0;
}

B-2 台形面積の計算

ファイル名: ex02b2.c

#include <stdio.h>

int main()
{
  int top, bottom, height;
  int area;
  
  printf("上底の長さを整数値で入力しなさい:");
  scanf("%d", &top);
  printf("下底の長さを整数値で入力しなさい:");
  scanf("%d", &bottom);
  printf("高さを整数値で入力しなさい:");
  scanf("%d", &height);

  area = (top + bottom) *height / 2;

  printf("面積:%d\n", area);

  return 0;
}

B-3 プログラムのデバッグ(上級編)

ファイル名: ex02b3.c

#include <stdio.h>

int main()
{
 int a, b, c, res;

 printf("Input a, b, and c: ");
 scanf("%d%d%d", &a, &b, &c);
 printf("\n");

 res = a*b;
 printf("Compute a * b\n");
 printf("%d * %d = %d\n", a, b, res); /* cを削除、resを追加*/
 
 printf("\n");
  
 printf("Compute (2 * a + b) %% c\n");
 /* printfで%を表示させたい場合は%%と書く */
 res = (2*a+b)%c; /* 計算を行う位置を変更 */
 printf("(2 * %d + %d) %% %d = %d\n", a, b, c, res); /* resを追加 */
  
 return 0;
}

Extra問題

E-1 二次関数の計算

ファイル名: ex02e1.c

#include <stdio.h>

int main()
{
  int a;
  int x, xm1, xm2, xp1, xp2;
  int y, ym1, ym2, yp1, yp2;
  printf("Enter a -> ");
  scanf("%d", &a);

  xm2 = a - 2;
  xm1 = a - 1;
  x   = a;
  xp1 = a + 1;
  xp2 = a + 2;

  ym2 = 3 * xm2 * xm2 - 4 * xm2 + 5;
  ym1 = 3 * xm1 * xm1 - 4 * xm1 + 5;
  y   = 3 * x * x - 4 * x + 5;
  yp1 = 3 * xp1 * xp1 - 4 * xp1 + 5;
  yp2 = 3 * xp2 * xp2 - 4 * xp2 + 5;


  printf("x : y\n");
  printf("%d : %d\n", xm2, ym2);
  printf("%d : %d\n", xm1, ym1);
  printf("%d : %d\n", x, y);
  printf("%d : %d\n", xp1, yp1);
  printf("%d : %d\n", xp2, yp2);

  return 0;
}

コメント:変数を減らした、次のようなプログラムでももちろんよい。

#include <stdio.h>

int main()
{
  int a;
  int x, y;

  printf("Enter a -> ");
  scanf("%d", &a);

  printf("x : y\n");

  x = a - 2;
  y = 3 * x * x - 4 * x + 5;
  printf("%d : %d\n", x, y);

  x = a - 1;
  y = 3 * x * x - 4 * x + 5;

  printf("%d : %d\n", x, y);

  x = a;
  y = 3 * x * x - 4 * x + 5;
  printf("%d : %d\n", x, y);

  x = a + 1;
  y = 3 * x * x - 4 * x + 5;
  printf("%d : %d\n", x, y);

  x = a + 2;
  y = 3 * x * x - 4 * x + 5;
  printf("%d : %d\n", x, y);

  return 0;
 }
Personal tools