第2回演習の解答例

Problem 1

#include <stdio.h>

int main(void)
{
    int a = 3;
    float b = 1.2;
    float result1 = a * b;
    float result2 = 2 / a;
    float result3 = 2.0 / a;
    a = b;
    printf("%f\n", result1);
    printf("%f\n", result2);
    printf("%f\n", result3);
    printf("%d\n", a);
    
    return 0;
}

Problem 2

#include <stdio.h>
#include <math.h>

int main(void)
{
  float pi = 3.1415926;
  float x = 0.2;
  float a, b, c;
  printf("%f\n%f\n", sin(pi), cos(pi));
  a = sin(x);
  b = cos(x);
  c = a*a + b*b;
  printf("%f\n", c);
  return 0;
}

Problem 3

#include <stdio.h>
#include <math.h>

int main(void) {
  float a = 70.0;
  float b = 61.0;
  float c = 66.0;
  float d = 82.0;
  float ave = (a + b + c + d)/4;
  printf("%f\n", ave);
  float sd = sqrt(((a - ave) * (a - ave) + (b - ave) * (b - ave) + (c - ave) * (c - ave) + (d - ave) * (d - ave))/4);
  printf("%f\n", sd);
  return 0;
}

Problem 4

#include <math.h>
#include <stdio.h>

int main(void) {
  float a, b, c;
  float discriminant;
  float real, imag;

  a = 6;
  b = 2;
  c = 8;

  discriminant = b * b - 4 * a * c;

  real = -b / (2 * a);
  imag = sqrt(-discriminant) / (2 * a);
  printf("The solutions are %.2f+%.2fi, %.2f-%.2fi\n", real, imag, real, imag);

  return 0;
}

Updated: