Reputation:
This might be inappropriate but I'm working on a school project in university and we use an automated test service called 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.
Upvotes: 1
Views: 207
Reputation: 153358
Candidate problems:
Compilation error @fussel
// return countRangeValue(array, numItems, getMaxAbsolute(array, numItems));
return countRangeValues(array, numItems, getMaxAbsolute(array, numItems));
Missing main()
@Bob__.
Variable length arrays are OK @Eric Postpischil in C99 and optionally in C11 yet do not attempt without insuring a positive array size.
int countInverses(int array[], int numItems) {
if (numItems <= 0) {
return 0;
}
int negarray[numItems];
....
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.
countRangeValues(..., double count)
is suspicious with double count
. When count
is a large value, countPlus == count
and countMinus == count
. The function always returns 0 as array[i] >= countMinus) && (array[i] < countPlus)
is always false.
OP's getMaxAbsolute()
looks too convoluted. 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;
}
Upvotes: 4