// converter.c
// by Dave Meyer
//
// takes an input file, reads all the numbers, and does a big nasty
// calculation on them.  It then outputs some numbers.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
  // we need a file and a couple of arrays.
  FILE *infile;
  float A[180];
  float B[180];
  int input_array[360];
  int counter, counter2;
  float D,C2,C3,C4,C6,C12;

  // get the input file.
  if(argc < 2) {
    printf("Error -- must specify file of numbers.\n");
    exit(1);
  }

  infile = fopen(argv[1], "r");

  if(infile == NULL) {
    printf("Error reading input file.\n");
    exit(1);
  }

  printf("Reading numbers...\n");

  for(counter = 0 ; counter < 360; ++counter) {
    fscanf(infile, "%i \n", &(input_array[counter]));
  }

  fclose(infile);

  printf("Calculating Ax and Bx...\n");

  for(counter = 1; counter <= 180; ++counter) {
    A[counter-1] = 0;
    B[counter-1] = 0;
    for(counter2 = 0; counter2 < 360; ++counter2) {
      A[counter-1] += ((float)1/180) * ((float)input_array[counter2]) *
	cos((float)counter * (float)counter2 * M_PI/180.0);
      B[counter-1] += ((float)1/180) * ((float)input_array[counter2]) *
	sin((float)counter * (float)counter2 * M_PI/180.0);
    }
  }

  D = 0;

  for(counter = 0; counter < 180; ++counter) {
    D += sqrt((A[counter]*A[counter]) + (B[counter]*B[counter]));
  }

  C2 = sqrt((A[1]*A[1]) + (B[1]*B[1]));
  C3 = sqrt((A[2]*A[2]) + (B[2]*B[2]));
  C4 = sqrt((A[3]*A[3]) + (B[3]*B[3]));
  C6 = sqrt((A[5]*A[5]) + (B[5]*B[5]));
  C12 = sqrt((A[11]*A[11]) + (B[11]*B[11]));

  printf("c2 = %f, C = %f, (c2/C) = %f\n", C2, D, (C2/D));
  printf("c3 = %f, C = %f, (c3/C) = %f\n", C3, D, (C3/D));
  printf("c4 = %f, C = %f, (c4/C) = %f\n", C4, D, (C4/D));
  printf("c6 = %f, C = %f, (c6/C) = %f\n", C6, D, (C6/D));
  printf("c12 = %f, C = %f, (c12/C) = %f\n", C12, D, (C12/D));
  return 0;
}






