last day (15 days later) » 

9:13 AM
-3
Q: c++ quadratic equation not working properly for a code checker

PapaI am submitting code to my university's tester, the code for me works thoroughly for the the test cases I provided. The checker tells there are some test cases that can't pass, I am testing in visual studio, and that's exactly compatible with the online judge compiler. The problem descriptio...

 
Can you at least explain in short what the code does? and/or provide a couple of test case examples (input/output)?
 
Would it be practical to load input.txt? How many lines does it have?
 
@DimChtz, yes, the code is about a c++ implementation of the quadratic equation, the inputs are a, b, c, the coefficients, the output is the solution or -1 for no solutions. Thanks
 
Why are you using int variables? Your code would fail for, say, a = 1.5.
 
@Adrian, actually, the problem states that the inputs are integers
 
9:13 AM
OK - just asking. However, in delta = ((b * b) - (4 * a * c)) the multiplications are performed as int before the subtraction promotes to double, so what if the products are greater than INT_MAX?
 
If the target machine supports a 32-bit int (fairly common in practice, including on a lot of windows boxes) then a value of b entered of 100000 will cause b*b to overflow. That causes undefined behaviour - and, odds are, if there is a test case which enters such a value, that test case will not produce the required results.
 
@Peter, that's a great note, though, the problem states that |a,b,c| <= 1000
 
Presumably -1 is the expected output if a solution cannot be found (a=b=0 or delta negative)? Could there be a problem with rounding/default-precision of outputting the x/x1/x2 values making what you produce and what they expect not quite match? (e.g. 1.999999 vs. 2 or 2.0 vs 2)
 
Do you have example test cases? Do you know if at least one test case passes the test? Is it possible that all test cases are wrong because you have one whitespace or a newline to much? Maybe a newline is missing at the end.
 
@ThomasSablik, actually I tested with manually with differents test cases, here is the thing, the checker hides test cases, it says wrong for case n.
 
9:13 AM
I think your problem is the delta==0 case. You're outputting 1 and a root; the correct answer is the same root twice. E.g. x^2 + 2x + 1 = 0 has a repeated root -1, but you produce 1 -1
 
Have you tried reversing the order of x1 and x2
 
@Albjenow, they demand the two solutions to be in an ascending order
 
@Papa - I'm not a mindreader, and can't be aware of constraints before you provide them. Also, consider what happens if input contains 1.0 as one the value of a - that is an integral value, but will prevent b and c being read as ints. Also, consider what happens if negative values are entered - the output values may not be ascending (in magnitude)
 
Does at least one test case pass the test? "it says wrong for case n" does this n change?
 
@Papa You also don't line-terminate the output (... << endl) which might break their checker. But the repeated root error is probably the most likely.
 
9:13 AM
Please provide the exact assignment text
 
@ThomasSablik, i have included the problem description. the example output is described as follows; intput; 1 -8 15 output 2 3 5
@TripeHound, actually I thought of that, and asked our tutor, her only answer was the problem is clearly defined!
 
@Papa Which point are you referring to? An input of 1 2 1 gives the wrong answer (1 -1) instead of -1 -1
 
@TripeHound, input 1 -2 1 gives right output 1 -1 (1 refers to the number of soluttions), please get back to the problem description above that I have include the formal problem statement by our online judge.
 
@Papa "please get back to the problem description above" Wasn't there when I replied! One could argue that the correct answer in that case is 2 -1 -1. Other possibilities: might they want the output on separate lines? You say "The checker tells there are some test cases that can't pass"... does it explicitly say that some of the test cases do pass (in which case it could be overflow etc. for specific inputs that's the problem). If it doesn't, it could be that all test cases are failing (which would imply something common to all inputs, like not being on separate lines).
 
@TripeHound, actually, it mentions some tests passed. No, output in the same line with space, and first output number is about the number of solutions otherwise -1
 
9:13 AM
No, output in the same line with space You should try to add a newline or space at the end of your output line. If they write all test cases to the same output file the first number of the second test case is next to the last number of the first test case. TC1: 1 2 1, TC2: 1 - 8 15, outputfile: 2 -12 3 5 instead of 2 -1 2 3 5
 
@Papa "output in the same line with space" THAT isn't mentioned in what you edited into the question. As others have said, without the EXACT requirements, we're only guessing at what might be wrong.
 
@TripeHound, that wasn t stated, it s just an image of the input output
 
Are you supposed to provide imaginary solutions? Which cases does it fail on? We NEED more info.
 
9:36 AM
No imaginary solutions, only real ones.
 

last day (15 days later) »