使用数组在C中构建频率表

时间:2016-03-25 14:54:39

标签: c frequency-analysis

我正在尝试读取99个数字的文件,并将它们放入频率表和使用数组的直方图中。格式是正确的,但它只是因为某些原因读取了我文件中的第一个数字。我只通过" get_data"包含了代码的开头。函数,因为我的错误是调用我的"太多的数据"注意我写入了代码。请帮忙!这是我用来读取文件的功能代码:

#define MAX_ELMNTS 99
#define ANLYS_RNG 10       

//Function Declarations
int get_data(int numbers[], int size, int range);

void print_data(int numbers[], int size, int line_size);

void make_frequency(int numbers[], int size, int frequency[], int range);

void make_histogram(int frequency[], int range);

int main(void)
{
  //Local Declarations
  int size;
  int nums       [MAX_ELMNTS];
  int frequency  [ANLYS_RNG];

  //Statements
  size = get_data(nums, MAX_ELMNTS, ANLYS_RNG);
  print_data(nums, size, 20);

  make_frequency(nums, size, frequency, ANLYS_RNG);   
  make_histogram(frequency, ANLYS_RNG);

  return 0;
}//end main

int get_data(int data[], int size, int range)
{
  //Local Declarations
  int data_in;
  int loader = 0;
  FILE* fpData;

  //Statements
  range--;
  if(!(fpData = fopen("An00966898.DAT", "r")))
    printf("Error opening file\a\a\n");

  while(loader < size && fscanf(fpData, "%d", &data_in) != EOF)
    if(data_in >= 0 && data_in <= range)
       data[loader++] = data_in;
    else
      printf("\nData point %d invalid. Ignored. \n", data_in);
  if(loader == size)
    printf("Too much data. Process what read.\n");
  return loader;
}//end get_data

0 个答案:

没有答案