简体   繁体   中英

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. 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.

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. The array for mean is set to all zeros along with the counter. plz help :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:

mean[100] = 0;

Try using the standard init method in How to initialize all members of an array to the same value?

mean[100] = {0};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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