第5回レポート課題の解答例


課題 5-1 の解答例

プログラム

#include<stdio.h>
#include<stdlib.h>

main(){
    printf("Hello\n");
    exit(0);
    }

実行結果

Script started on Thu Jan 28 14:24:21 1999
soejima@bashful-3[1]_% gcc -o program_5-1 program_5-1.c
soejima@bashful-3[2]_% program_5-1
Hello
soejima@bashful-3[3]_% exit
soejima@bashful-3[4]_% 
script done on Thu Jan 28 14:24:57 1999

課題 5-2 の解答例

実行結果

Script started on Thu Jan 28 14:25:41 1999
soejima@bashful-3[1]_% gcc -o program_5-2 program_5-2.c
soejima@bashful-3[2]_% program_5-2
x[1]=1
x[2]=4
x[3]=-3
x[4]=9
x[5]=8
結果は 19 です。
soejima@bashful-3[3]_% exit
soejima@bashful-3[4]_% 
script done on Thu Jan 28 14:26:12 1999

プログラムの”計算の本体”は何を求めているのか?

データの総和。

課題 5-3 の解答例

各※には次の文またはコマンドが入る。
[※1]if
[※2]n>0
[※3]while
[※4]i<=n
[※5]i=i+1;

プログラム

#include <stdio.h>

main()
{
  int i, n;
  double r, s;

  printf("Input Number\n"); 
  scanf( "%d", &n );

  if(n>0)
    {
      s=0.0 ;
      i=1   ;
      while(i<=n)
	{
          printf("Input r[%d]\n", i );
          scanf("%lf", &r );
          s=s+1.0/r;
	  i=i+1;
	}
      printf("Total resistance =%f\n", 1.0/s );
    }else{
      printf("Illegal Number\n");
    }
}

実行結果

Script started on Thu Jan 28 14:28:36 1999
soejima@bashful-3[1]_% gcc -o program_5-3 program_5-3.c
soejima@bashful-3[2]_% program_5-3
Input Number
3
Input r[1]
10
Input r[2]
20
Input r[3]
30
Total resistance =5.454545
soejima@bashful-3[3]_% exit
soejima@bashful-3[4]_% 
script done on Thu Jan 28 14:29:10 1999