Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why looping through array gives non existent value?

I have 2 multidimensional arrays filled with x and y coordinates. Im comparing both arrays to find a match in nested loop like this

int order;
order = 0;
float c[4][2];
for (int i = 0;i <= sizeofarrB; i++ ) {
   for (int j = 0;j <= sizeofarrA; j++ ) {
        if (a[j][0] == b[i][0] && a[j][1] == b[i][1]){
        printf("testx = %f y = %f\n " , a[j][0], a[j][1]);
        printf("testx = %f y = %f\n", b[i][0], b[i][1]);
            c[order][0] = b[i][0];
            c[order][1] = b[i][1];
            order++;
        }

   }
}

first array

these are values of first array

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

second array

these are values of second array

and these are the values that i get from running that nested loop
final values

i have no idea how to fix the code to get correct values

>Solution :

You use i <= sizeofarrB as a loop condition, but you should use i < sizeofarrB, as an array of size 4 will have indices from 0 to 3.

You are reading out of the memory area that belongs to the array, a typical way of corrupting memory in C.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading