简体   繁体   English

学校课程测试无法正确覆盖我的代码,我不确定为什么

[英]School program tests not covering my code correctly and I'm not sure why

This might be inappropriate but I'm working on a school project in university and we use an automated test service called Web-Cat. 这可能不合适,但是我正在大学的一个学校项目中工作,我们使用了称为Web-Cat的自动化测试服务。 When I upload my file (everything is correct as for the filename and method names etc.) the tests throw this at me: 当我上传文件时(文件名和方法名等所有信息都正确),测试向我抛出了这个信息:

hint: your code/tests do not correctly cover error: Cannot locate behavioral analysis output.

My code looks like this, I can't find any compile errors or anything that would cause such a problem. 我的代码看起来像这样,我找不到任何编译错误或任何会导致此类问题的东西。

#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));
}

If necessary, I can explain the purpose of all of these methods but the test also says "Your program failed before running any tests. Usually this is a compile problem or premature exit problem. Make sure your program compiles and runs before uploading." 如有必要,我可以解释所有这些方法的用途,但测试还显示“您的程序在运行任何测试之前失败。通常是编译问题或过早退出问题。请确保您的程序在上载之前已编译并运行。” So I assume its syntax a compiling problem, I just don't know if anything I did would cause something like that. 所以我以为它的语法是编译问题,我只是不知道我所做的任何事情是否会导致类似的问题。

Candidate problems: 候选问题:

  1. Compilation error @fussel 编译错误@fussel

      // return countRangeValue(array, numItems, getMaxAbsolute(array, numItems)); return countRangeValues(array, numItems, getMaxAbsolute(array, numItems)); 
  2. Missing main() @Bob__ . 缺少main() @Bob__

  3. Variable length arrays are OK @Eric Postpischil in C99 and optionally in C11 yet do not attempt without insuring a positive array size. 可变长度数组在C99中可以用@@@@@@@@@@@@@@@@@@@@ Eric Postpischil ,在C11中也可以选择,但是在没有确保正数组大小的情况下不要尝试。

     int countInverses(int array[], int numItems) { if (numItems <= 0) { return 0; } int negarray[numItems]; .... 
  4. Instead of if (posItems > negItems) { I'd expect if (posItems >= negItems) { or maybe even if (1) , yet the goal of countInverses() lacks details for deep analysis. 而不是if (posItems > negItems) {我希望if (posItems >= negItems) {甚至可能是if (1) ,但countInverses()的目标缺少深入分析的细节。

  5. countRangeValues(..., double count) is suspicious with double count . countRangeValues(..., double count)可疑为double count When count is a large value, countPlus == count and countMinus == count . count是一个大值时, countPlus == countcountMinus == count The function always returns 0 as array[i] >= countMinus) && (array[i] < countPlus) is always false. 该函数始终返回0,因为array[i] >= countMinus) && (array[i] < countPlus)始终为false。


OP's getMaxAbsolute() looks too convoluted. OP的getMaxAbsolute()看起来太复杂了。 Suggested alternative: 建议的替代方法:

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.

相关问题 我的程序无限循环,我老实说不清楚为什么 - My program is looping infinitely, and I'm honestly not sure why 我的代码有问题,我不确定我是否写对了 - i have a problem with my code and i'm not sure if i even wrote it correctly 我不确定为什么该程序无法编译 - I'm not sure why this program won't compile 我的程序有 AddressSanitizer 错误,但我不知道如何阅读 - My program has AddressSanitizer error but I'm not sure how to read it 当我运行我的程序时,我收到“Segmentation fault 11”错误。 我不确定我的代码中的什么会导致此错误 - When I run my program I get the 'Segmentation fault 11' error. I'm not sure what in my code would cause this error 我不确定为什么我的代码产生这个算术错误? - I am not sure why my code is producing this arithmetic error? 我需要为学校做一个程序“给定一个数字确定其主要除数的总和”,但我不明白为什么我的程序不起作用 - I need to do a program for school “given a number establish the sum of its prime divisors” but I don't understand why my program doesn't work 我不确定为什么我的嵌套 while 循环在第一次迭代后停止 - I'm not sure why my nested while loop is stopping after the first iteration 我的阅读程序中不断显示“双重释放或损坏(输出)”,我不确定它是什么意思(已修复) - I keep getting shown “double free or corruption (out)” in my reading program and I'm not sure what it means (fixed) 如何在下面实现这些图形函数以确保我覆盖了所有具有给定约束的测试用例? - How do I implement these graph functions below to make sure I'm covering all test cases with the given constraints?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM