[英]School program tests not covering my code correctly and I'm not sure why
这可能不合适,但是我正在大学的一个学校项目中工作,我们使用了称为Web-Cat的自动化测试服务。 当我上传文件时(文件名和方法名等所有信息都正确),测试向我抛出了这个信息:
hint: your code/tests do not correctly cover error: Cannot locate behavioral analysis output.
我的代码看起来像这样,我找不到任何编译错误或任何会导致此类问题的东西。
#include <stdio.h>
double getPositiveAverage(double array[], int numItems)
{
double average = 0;
int numOfItems = 0;
for (int i = numItems-1; i >= 0; i--)
{
if (array[i] > 0)
{
average = average + array[i];
numOfItems++;
}
}
if (numOfItems == 0) {
return 0;
}
else {
return average / numOfItems;
}
}
int countRangeValues(double array[], int numItems, double count)
{
double countPlus = count + .5;
double countMinus = count - .5;
int counter = 0;
for (int i = numItems-1; i >= 0; i--)
{
if ((array[i] >= countMinus) && (array[i] < countPlus))
{
counter++;
}
}
return counter;
}
double getMaxAbsolute(double array[], int numItems)
{
double max = 0;
for (int i = numItems-1; i >= 0; i--)
{
if (array[i] == max * -1 && array[i] > 0)
{
max = array[i];
}
else if (array[i] < 0)
{
if (max < 0)
{
if ((array[i] * -1) > (max * -1))
{
max = array[i];
}
}
else
{
if ((array[i] * -1) > max)
{
max = array[i];
}
}
}
else
{
if (max < 0)
{
if ((array[i]) > (max * -1))
{
max = array[i];
}
}
else
{
if ((array[i]) > max)
{
max = array[i];
}
}
}
}
return max;
}
int countInverses(int array[], int numItems)
{
int negarray[numItems];
int negItems = 0;
int posarray[numItems];
int posItems = 0;
int counter = 0;
for (int i = numItems-1; i >= 0; i--)
{
if (array[i] < 0)
{
negarray[negItems] = array[i];
negItems++;
}
else if(array[i] > 0)
{
posarray[posItems] = array[i];
posItems++;
}
}
if (posItems == 0 || negItems == 0)
{
return 0;
}
else
{
if (posItems > negItems)
{
for (int i = posItems-1; i >= 0; i--)
{
for (int j = negItems-1; j >= 0; j--)
{
if ((negarray[j]*-1) == posarray[i] && negarray[j] != 0)
{
counter++;
negarray[j] = 0;
posarray[i] = 0;
}
}
}
}
}
return counter;
}
int getMaxCount(double array[], int numItems)
{
return countRangeValue(array, numItems, getMaxAbsolute(array, numItems));
}
如有必要,我可以解释所有这些方法的用途,但测试还显示“您的程序在运行任何测试之前失败。通常是编译问题或过早退出问题。请确保您的程序在上载之前已编译并运行。” 所以我以为它的语法是编译问题,我只是不知道我所做的任何事情是否会导致类似的问题。
候选问题:
编译错误@fussel
// return countRangeValue(array, numItems, getMaxAbsolute(array, numItems)); return countRangeValues(array, numItems, getMaxAbsolute(array, numItems));
缺少main()
@Bob__ 。
可变长度数组在C99中可以用@@@@@@@@@@@@@@@@@@@@ Eric Postpischil ,在C11中也可以选择,但是在没有确保正数组大小的情况下不要尝试。
int countInverses(int array[], int numItems) { if (numItems <= 0) { return 0; } int negarray[numItems]; ....
而不是if (posItems > negItems) {
我希望if (posItems >= negItems) {
甚至可能是if (1)
,但countInverses()
的目标缺少深入分析的细节。
countRangeValues(..., double count)
可疑为double count
。 当count
是一个大值时, countPlus == count
和countMinus == count
。 该函数始终返回0,因为array[i] >= countMinus) && (array[i] < countPlus)
始终为false。
OP的getMaxAbsolute()
看起来太复杂了。 建议的替代方法:
double getMaxAbsolute(const double array[], int numItems) {
double max = 0.0;
double amax = 0.0;
for (int i = numItems - 1; i >= 0; i--) {
double aelement = fabs(array[i]);
// If greater or the same, favor + over -
if (aelement > amax || (aelement == amax && array[i] > max)) {
max = array[i];
amax = aelement;
}
}
return max;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.