Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can someone tell me why is the first one gets rejected (what i wrote) on our college's online tester, and the second gets accepted (what i had to write and seems totally wrong) ?
First: pastebin.com/xsq6dLGY[^]
Second: pastebin.com/eM9i1FDG[^]

Sample inputs:
pastebin.com/eaLEz2gJ[^]

What I have tried:

I tried them with all the sample inputs, and they seemed to give the same results but my first one still gets rejected.
Posted
Updated 29-May-16 4:08am
v5
Comments
Patrice T 28-May-16 18:36pm    
Paste the 2 source codes in the question, sample data too.
Use Improve question to update your question.
Patrice T 28-May-16 18:44pm    
Check that you didn't post 2 times the same code as First and Second.
Member 12552229 28-May-16 19:40pm    
That i did.
Sorry, it's been corrected
George Jonsson 28-May-16 20:56pm    
Actually, both variants should be rejected.
You allocate memory with calloc(), but you never release it with free().

What is the purpose of the program? What is the expected output?
Member 12552229 28-May-16 21:16pm    
First you are right, I did not free them because it causes problems in the tester.
Other codes worked without free()-ing.
Pretty funny stuff, the sad thing is we have to send our exams to it.

It reads from a file, first two numbers m,n which are the rows and maximal columns of a matrix.
Then it should count how many different uppercase letters are in the said matrix, until EOF or until another number.

1 solution

You have already worked out the problem you commented the offending line out in the second example.
sor = c - '0';

Its going to throw warnings with all but the slackest compiler settings.

The error is correct because the result value can go negative if c is small, which has no meaning to the original type being a char. In laymans term you subtracted two oranges and got an apple.

You have two correct fixes to that line of code.

1.) sor need to be an unsigned type that covers the range of char so unsigned char, unsigned int, unsigned long would all work.

2.) The other solution is to IMPLICIT TYPECAST the subtraction so the compiler knows you actually mean to do that and have allowed for a negative if it occurs.

sor = (int)(C-'0');

You are trying to learn to be a programmer so learn types and there rules. The rejection of your code was correct on every ground. It isn't clear to anyone or the uni lexx checker that you know what you are doing and have accounted for it. There is a lot that can go wrong with that line of code in the wrong situation.

What it tells me is you are running your compiler on very loose settings and I suggest you turn the error level up to max and actually bother to remove the warnings that get thrown up. Those warnings contain dangers and in the student setting especially code submitted should contain zero warnings.

For the record the checker will never pick off malloc/free issues that requires real intelligence of what the code is doing when it runs. There are specific tools we use for memory leak and no tool is perfect at it because the order and timings can matter. So perhaps learn not to scoff at something you don't understand.
 
Share this answer
 
v7
Comments
George Jonsson 29-May-16 18:10pm    
sor = c - '0';
This is a perfectly valid line in C, as char is an integer type and is normally signed by default, hence it can contain the values between -128 and 127.
leon de boer 29-May-16 22:07pm    
The rules of C are EXPLICIT and not up for you to debate, they are covered by specifications C90, C99 etc.

If you want to claim that your character is signed then you needed to define it so there is a type "signed char" for that exact reason, the code as stands uses "char" which is unsigned. Bottom line if you want negatives in your char type you need to tell the compiler thats what you are doing. Any compiler will produce a warning on that line of code and the uni are obviously testing with there warnings turned up to full and it rightfully rejects the code. They are trying to teach him to be a programmer, not someone punching out hack error prone code that you see of the internet.

So looking at your claim, yes if you had defined c as signed char the compiler would have no problem and so you just created a 3rd correct way to fix it. So this would be the fix using your suggestion and that would be valid and lift no compiler warning.

signed char c;
int sor = c - (signed char)'0';

The code as written is definitely wrong, period and you can't argue it and why the uni submission program rejects it. I agree you will see the code as written all over the internet but that doesn't make it right and there is a lesson about types in it for him :-)
George Jonsson 29-May-16 22:49pm    
I was born in a free country and as far as I know I can basically debate any subject I choose to debate.

The rules maybe explicit but the implementation of the rules are not always so strict. Different compilers have different implementations.

In this case it doesn't matter, however, as the result of c - '0' will be implicitly type casted to an int if the receiving variable is an int.

The code
char c = '5';
int i = c - '0';
compiles without any warnings, at least in Visual Studio with warning level 4.
You can do the same thing in C# as well, and the result will be the expected i = 5.
Don't know what kind of compiler you have, though, it might give a different result.
I'm not saying if it is good practice to do this or not, but the the compilers I have used will not complain about it.

It is probably a good idea to check if the value of c is a digit, if you want the desired result.

So you see I can argue it, and you are free to argue back if you so wish.
leon de boer 30-May-16 2:28am    
The fact is he can't submit his work and pass his programming coarse until he does what is required and what you believe in your little free country doesn't come into it. He is required to program to the standard to pass his coarse and he was asking why it wouldn't accept it, well because it doesn't meet the standard the Uni coarse demands.

For the record I often set that as a problem when hiring programmers to sort out the good ones.

I am aware Visual Studio since VS2013 allows character subtraction basically because too many idiots couldn't deal with warning it threw. They do at least acknowledge it isn't to C99 standard. The official MS line is Visual C++ partly supports some C99 language features under a slightly different syntax and possibly with slightly different semantics. The new standard C11 which very few compilers meet that code will still create a warning.
George Jonsson 30-May-16 3:08am    
What is a coarse?
If you have to start to comparing size and calling people idiots then there is not much more to discuss.
Personally I worry more about memory leaks when I hire programmers. A nice personality is also a key factor.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900