第5回 (6/3) 演習の解答例

Problem 1

#include <stdio.h>

int main()
{
  int i, n;
  float sum=0.0, v;
  scanf("%d", &n);
  for (i=0; i<n; i++)
  {
      scanf("%f", &v);
      sum += v;
  }
  printf("%f\n", sum/n);
  return 0;
}

Problem 2

#include <stdio.h>
#define MAX_N 100

int main()
{
    double v[MAX_N], w[MAX_N], total;
    int i, N;

    scanf("%d", &N);
    for (i=0; i<N; i++)
    {
        scanf("%lf", &v[i]);
    }
    for (i=0; i<N; i++)
    {
        scanf("%lf", &w[i]);
    }

    total = 0.0;
    for (i=0; i<N; i++)
    {
        total += v[i]*w[i];
    }

    printf("%lf\n", total);

    return 0;
}

Updated: