该程序读取两个文件并将它们存储在两个数组中。 array1是数据,array2是与之对应的类型。 ex array1:183,183,182,195.3,187 array2:1,1,1,2,2它将array1中的重复数字设置为0,并将array2中的相应数字设置为零。例如:array1:0,0,182,195.3,187 array2:0,0,1,2,2然后它应该忽略for循环中的零并计算相同类型的所有数字的总和。例如:(出于某种原因称其为平均值)意味着[2] = 195.3 + 187它似乎忽略了至少有一个if语句> .5并在遇到零时转到else语句。有任何想法吗?
来自文件的示例数据(不是完整的数据),一旦它看到彼此相邻的182.6000061它将那些设置为零并且在array2中相应的数字,但它忽略了一个if语句并转到else语句。产量为2822.799,881.400,1997.8等。第一个值应该接近6000.
将数据输入到array1:182.6000061 183 182.6000061 182 177.8000031 183.3999939 183 180.6000061 147.6000061 166.6000061 168 182 181.6000061 164.1999969 180.3999939 177.3999939 177.6000061 182.6000061 182.6000061 181.6000061 179.8000031 160 177.3999939 181.3999939 183.1999969 183.1999969 182.8000031 181.3999939 179.6000061 182.1999969 183.8000031 176.8000031
将数据输入到array2:2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2912 2979 2979 2979 2979 2979 2979 2979 2979 2979
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
float array1[1000] = {0}, array2[1000] = {0}, mean[1000] = {0}, average[1000] = {0}, counter[1000] = {0}, range[1000] = {0}, upper[1000] = {0}, lower[1000] = {0};
int a, h, i, r, j = 0, pos, found = 0, extrinsic[1000] = {0};
int b=0;
int linect = 1;
char buf[128];
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 < 99; 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;
}
答案 0 :(得分:0)
为什么程序中有两个不同的循环?第一个循环999次,另一个循环99次。
for (i = 0; i < 999; i++)
for (i = 0; i < 99; i++)
答案 1 :(得分:0)
一旦它看到彼此相邻的182.6000061,它就会将它们设置为零并且数组2中的相应数字
都能跟得上!它将第一个设置为0
,将相应的设置设置为array2
也许你需要
if (array1[i] == array1[i+1])
{
array1[i] = array1[i + 1] = 0;
array2[i] = array2[i + 1] = 0;
}