简体   繁体   English

添加数组元素并存储在数组中

[英]Adding Array Elements and Storing in Array

These piece of code gets data from file and stores it in arrays. 这些代码段从文件中获取数据并将其存储在数组中。 It then searches though the array, if the number is between .5 and 50,000 ( just to remove 0 and big numbers) it goes into the if statement. 然后它搜索整个数组,如果数字在.5到50,000之间(仅删除0和大数字),它将进入if语句。 In this statement if the number is equal to the next it will check the other array to filter out small and large numbers then adds it and stores it to the mean and adds the counter. 在此语句中,如果数字等于下一个数字,它将检查另一个数组以滤除大小数字,然后将其相加并将其存储到均值并添加计数器。 It does this until the number in array2 is not equal to the next number where it comes out and calculates the average for it, ect. 它会一直执行到array2中的数字不等于出现的下一个数字,然后计算出该数字的平均值。

It does everything fine until it gets to the mean[j] = mean[j] + array1[i], i tried a printf to check out that part and it spits out a giant negative number. 它所做的一切都很好,直到达到均值[j] =均值[j] + array1 [i],我尝试用printf检验该部分,并吐出一个巨大的负数。 The array for mean is set to all zeros along with the counter. 均值数组与计数器一起设置为全零。 plz help :L 请帮助:L

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
  FILE *fp;
  float array1[1000], array2[1000], mean[1000], average[1000], counter[1000], range[1000], upper[1000], lower[1000];
  int a, h, i, r, j = 0, pos, found = 0, extrinsic[1000];
  int b=0;
  int linect = 1;
  char buf[128];
  extrinsic[100] = 0;
  mean[100] = 0;
  counter[100] = 0;
  range[100] = 0;
  upper[100] = 0;
  lower[100] = 0;
  average[100] = 0;
  i = 0;
  j = 0;
  fp = fopen("data.txt", "r");

  if(fp == NULL)
  {
    puts("Cannot open file for reading");
    scanf("%d", &a);
    exit(EXIT_FAILURE);
  }

  while( linect < 1000 ) 
    {
    fgets(buf, sizeof(buf), fp);
    sscanf(buf, "%f", &array1[i]);
    //printf("Set %d - 1st: %f\n", linect, array1[i]);
    linect++;
    i++;
    }
  fclose(fp);


  linect = 0; //reads lot data file and puts it in array2
  i = 0;
  fp = fopen("lot_numbers.txt", "r");
  if(fp == NULL)
  {
    puts("Cannot open file for reading");
    scanf("%d", &a);
    exit(EXIT_FAILURE);
  }
  while( linect < 1000 ) 
    {
    fgets(buf, sizeof(buf), fp);
    sscanf(buf, "%f", &array2[i]);
   // printf("Set %d - 1st: %f\n", linect, array2[i]);
    linect++;
    i++;
    }


  for (i = 0; i < 999; i++) //searches array sets repeated points to zero
  {
      if (array1[i] == array1[i+1])
      {
          array1[i] = 0;
          array2[i] = 0;
      }
  }
  for (i = 0; i < 9; i++)   //searches array
  {
      if (array2[i] > .5 && array2[i] < 50000)
      {
        if (array2[i] == array2[i+1])
        {
          if (array1[i] > .5 && array1[i] < 500){  //ignores zeros and junk data 
          mean[j] = mean[j] + array1[i];
          counter[j]= counter[j] + 1;
        }
      }
      else {
          average[j] = mean[j]/counter[j];
          range[j] = average[j] * 0.1;
          upper[j] = average[j] + range[j];
          lower[j] = average[j] - range[j];
          j++;
           }
      }
  }
  j = 0;
  while (j <10){
  printf("\n%f", mean[j]);
  j++;
  }
scanf("%d", &a);

return 0;
 }

I've never seen this syntax in C: 我从未在C中看到过这种语法:

mean[100] = 0; 平均值[100] = 0;

Try using the standard init method in How to initialize all members of an array to the same value? 尝试使用如何将数组的所有成员初始化为相同值的标准init方法

mean[100] = {0}; 均值[100] = {0};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM