Search code examples
cbmpcs50fseek

Can 6/2 return a different result than 6/a (where a=2)? (and how that affects the output of a .bmp file)


I am working on an assignment in which a program to resize .bmp images has to be written.

I know the title might sound weird but I find myself with this situation.

I have this piece of code:

if (l < factor - 1)
        {
            fseek(inptr, -(bi.biWidth / factor), SEEK_CUR);
        }

Now, I would like you to focus on the offset. bi.biWidth == 6 and factor == 2, so -(bi.biWidth / factor) should equal -3. With the code written above, the output is this.

And, writing directly -3, like this:

if (l < factor - 1)
        {
            fseek(inptr, -3, SEEK_CUR);
        }

the output is this.

 

So, apparently it should return the same, but it doesn't. And I would like to know why.

 

P.S.: In case it matters, factor is a command line argument that's been through atoi, and bi.biWidth is called from another file (and has also been multiplied with the factorvariable above, before being written in the code I attached - still I don't think that matters).

EDIT1: LONG is an int32_t type. factor is an int type.

EDIT2: full code: https://pastebin.com/TYjQkupw and headers definitions: https://pastebin.com/aAY3U2fG

LAST EDIT: I will close this question soon. Among others, the main problem was that the IDE, for some reason that the course's staff is still trying to figure out, wasn't showing the actual pixels of the .bmp files. As a file, the output was perfect, but on the screen it didn't look like that. The Staff checked my outputs in their computers and all them saw perfect/normal .bmp files; they couldn't understand my problem until I sent them an imgur link showing what I was seeing on my screen -which was completely different from what they saw in theirs, even though the .bmp files were the same. Thanks to everyone that took some time to answer my question!


Solution

  • First of all, thanks to everyone who helped and tried to solve this question.

    This is the answer to it (also added as a "last edit" in the question text): apparently, the problem had nothing to do with the code. There was some kind of problem in my IDE/screen/computer (still not clear), that made the output look different than it actually was.

    To make it cristal clear, it was like if my computer performed a sum (1+2) and, in the inside, the processors knew the result was 3, but still -for some unknowkn reason- the screen showed a 4.

    So that's what happened. The course staff still does not know why, but the code was "okay".

    Thanks again!!