Re: Feedback on C code?

Sorry to revive this, but I have an interesting dilemma and I'm not sure as to what to even search for.
I converted a 2d array to a 1d array. My formula for converting an index to x / y coordinate is as follows:
x = index % width
y = index / width
Assume that y is an int and thus trims off any floatingpoints.
When I printed my coordinates with the following loop, I was surprised as to the result.

    for (int i = 0; i < x * y; i ++) {
        int tempx = i % x;
        int tempy = i / x;
        if (tempx == 0 && tempy != 0)
        printf("\n");
        printf("%d, %d,", tempx, tempy);
    }

The output?
0, 0,1, 0,2, 0,3, 0,
0, 1,1, 1,2, 1,3, 1,
0, 2,1, 2,2, 2,3, 2,
0, 3,1, 3,2, 3,3, 3,
and so on.
As you can see, my x values are increasing, which makes me wonder if I screwed something up. You see, typing in a 2d list in python and looping through it yields me an increases in why first, i.e,
(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (1, 0)...
So... where did I go wrong here? My allocation for the 1d array is not an issue here, I don't think so anyways, but I'll paste it in here just in case it's at fault.

int *arr = (int *)malloc(x * y * sizeof(int));
//do stuff
free(arr);

So, yeah.



-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector

Reply via email to