[ create a new paste ] login | about

Link: http://codepad.org/16zuXnfY    [ raw code | output | fork ]

C, pasted on Jul 20:
#include <stdio.h>
#include <stdlib.h>
#define N 1024

void input(int *n, double *a) {
  int i;
  printf("n = ");
  scanf("%d", n);
  if (*n > N || *n < 0) {
    printf("not supported, n is too large.\n");
    exit(-1);
  }
  for (i = 0; i < *n; i++) {
    printf("%d: ", i + 1);
    scanf("%lf", a + i);
  }
}

void calcu(int n, double *a, double *s, double *ave) {
  int i;
  *s = 0;
  for (i = 0; i < n; i++)
    *s += a[i];
  *ave = *s / n;
}

void output(double s, double ave) {
  printf("sum: %f, average : %f\n", s, ave);
}

int main() {
  int n;
  double a[10];
  double xsum, ave;
  int i;
  
  input(&n, a);
  calcu(n, a, &xsum, &ave);
  output(xsum, ave);
  return 0;
}
/* end */


Output:
1
2
3
n = not supported, n is too large.

Exited: ExitFailure 255


Create a new paste based on this one


Comments: