C 学校程序测试没有正确覆盖我的代码,我不知道为什么
这可能不合适,但我正在大学里做一个学校项目,我们使用一个叫做WebCAT的自动测试服务。当我上传我的文件时,所有文件名和方法名等都是正确的。测试向我提出了以下问题:C 学校程序测试没有正确覆盖我的代码,我不知道为什么,c,C,这可能不合适,但我正在大学里做一个学校项目,我们使用一个叫做WebCAT的自动测试服务。当我上传我的文件时,所有文件名和方法名等都是正确的。测试向我提出了以下问题: hint: your code/tests do not correctly cover error: Cannot locate behavioral analysis output. 我的代码看起来是这样的,我找不到任何编译错误或任何会导致这种问题的东西 #include <stdio.h> double getP
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));
}
如果有必要,我可以解释所有这些方法的用途,但是测试还表明您的程序在运行任何测试之前失败。通常这是一个编译问题或过早退出问题。在上传之前,请确保您的程序已编译并运行。因此,我假设它的语法是一个编译问题,我只是不知道我所做的任何事情是否会导致类似的问题。候选问题:
编译错误
缺少主管道
在C99和C11中都正常,但在不确保阵列大小为正的情况下,请勿尝试
int countInverses(int array[], int numItems) {
if (numItems <= 0) {
return 0;
}
int negarray[numItems];
....
候选问题:
编译错误
缺少主管道
在C99和C11中都正常,但在不确保阵列大小为正的情况下,请勿尝试
int countInverses(int array[], int numItems) {
if (numItems <= 0) {
return 0;
}
int negarray[numItems];
....
getMaxCount中有一个输入错误。调用函数“countRangeValue”时忘记了一个“s”。您还尝试定义动态大小的数组,这在C中是不允许的,至少不像您这样做。函数countInverses中的numItems必须是常量。主函数在哪里?@fussel:C标准当然允许可变长度数组。这是一个实现选项。getMaxCount中有一个输入错误。调用函数“countRangeValue”时忘记了一个“s”。您还尝试定义动态大小的数组,这在C中是不允许的,至少不像您这样做。函数countInverses中的numItems必须是常量。主函数在哪里?@fussel:C标准当然允许可变长度数组。这是一个实现选项。我意识到我的IDE没有检查该文件的语法错误,还有其他问题,我忘记了s。感觉很愚蠢,但我真的很感谢你的帮助!我意识到我的IDE没有检查该文件的语法错误,还有其他问题,我忘记了s。感觉很愚蠢,但我真的很感谢你的帮助!
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;
}