# Thread: Need help debugging my code

1. ## Need help debugging my code

Hey, I was presented with a problem that goes like this:

You have to find out how many blocks of the same height you can stack in a pyramid way. The "pyramid" must be built in a way that a block A on top of a block B has smaller or equal sides. You can turn the blocks to make them fit.

The site will give you an input, with a pre determined format, that is:

3
100 100
1000 2000
2000 500
0
The first line determines how many blocks you're given (you're given n blocks), the next n lines are the sides of the block and the last line must be a zero.

There can be more that one pyramid:

3
100 100
1000 2000
2000 500
6
3 4
5 7
7 5
1 5
4 4
10 2
0

I hope you can understand the problem I just described. If not please say.

So what I'm doing is finding the areas of the blocks and sorting them from bigger to smaller. If the block with the second biggest area has smaller or equal sides than the one with the biggest area the number of blocks in the pile increases.

If one of the sizes of the top block is bigger than the ones of the smaller one you go and compare the first with the third one and so on.

Here's the code I have so far, it's just crashing and closing. Some of the variable names in it are in portuguese, but I don't beleive that it will be difficult for you to understand the code.

Code:
```#include <stdio.h>

int main ()
{
FILE *fp;
int start = 1, num, arrayX[100], arrayY[100], arrayArea[100], i, x, y, n;
int maiorArea = -1, velhoY = 0, velhoX = 0, novoX, novoY, teste = 1, numeroBlocos;

if (!fp)
{
printf("Erro ao ler o arquivo!");
return 1;
}

while (true)
{
if (start)
{
fscanf(fp, "%d", num);

if (!num || feof(fp))
{
fclose(fp);
return 0;
}

!start;
} else {
numeroBlocos = 0;

for (i = 0; i < num; i++)
{
fscanf(fp, "%d %d", arrayX[i], arrayY[i]);
arrayArea[i] = arrayX[i] * arrayY[i];
}

while (num-- > 0)
{
for (n = 0; n <= i; n++)
{
if (arrayArea[n] > maiorArea && maiorArea != n)
maiorArea = n;
}
arrayArea[maiorArea] = 0;
novoX = arrayX[maiorArea];
novoY = arrayY[maiorArea];

if ((velhoX == 0 && velhoY == 0) || (novoX <= velhoX && novoY <= velhoY) || (novoX <= velhoY && novoY >= velhoX))
{
velhoX = novoX;
velhoY = novoY;
numeroBlocos++;
}
}

printf("Teste %d\n %d\n\n", teste++, numeroBlocos);
velhoX = velhoY = 0;
!start;
}
}
fclose(fp);
return 0;
}```
Oh and the output must follow this rule:

Teste n // test n (n being the pyramid id)
x // x is the number of blocks that can be stacked according to the rules
// empty line

-----------------------------------

By the way I'd like to know how do you guys debug your code... Do you disassemble it? Is there a way to debug the code in C language?

2. Code:
```fscanf(fp, "%d", num);
. . .
fscanf(fp, "%d %d", arrayX[i], arrayY[i]);```
You aren't passing pointers to fscanf like you're supposed to.

3. Well then how should I pass the pointers?

Is this the correct way of reading a file line by line?

4. The point is that you *aren't* passing pointers, and you should be.

The proper way to read in an integer is:
Code:
`fscanf(fp, "&#37;d", &num);`
The & is important as it means the difference between passing the value of the integer value num (which gets implicitly converted to a pointer), or passing the address of the variable num (which IS a pointer).

5. Oh, thanks for the explanation. The code is not crashing anymore.

It ain't working yet =P But atleast I can now see what's going wrong.

6. And as for this:
Code:
`fscanf(fp, "&#37;d %d", &arrayX[i], &arrayY[i]);`

7. On a side note if you're using gcc, if you compile with -Wall or -pedantic, or both, you'll catch many errors that can be difficult to spot. I believe passing non-pointers to fscanf would have emitted a warning then.

8. Yes, any compiler should complain about this because you're doing implicit conversion from int to int* (int to pointer).
And if you'd compile it as C++ code, it simply wouldn't compile at all.

9. lala, do you have a link to the original problem text that I could look at?

10. So how did it go? Did you solve it?

11. Well.... I really don't understand why it's not working...

I beleive I know where the error is, just don't know what's wrong...

Code:
```#include <stdio.h>

int main ()
{
FILE *fp;
int start = 1, num, arrayX[100], arrayY[100], arrayArea[100], i,n, areaMaior = 0;
int maiorArea = -1, velhoY = 0, velhoX = 0, novoX, novoY, teste = 1, numeroBlocos, num2, nn;

if (!fp)
{
printf("Erro ao ler o arquivo!");
return 1;
}

while (true)
{
if (start)
{
fscanf(fp, "&#37;d", &num);
num2 = num;

if (!num || feof(fp))
{
fclose(fp);
return 0;
}

start = 0;
} else {

numeroBlocos = 0;

for (i = 0; i < num2; i++)
{
fscanf(fp, "%d %d", &arrayX[i], &arrayY[i]);
arrayArea[i] = arrayX[i] * arrayY[i];
// printf("%d*%d = %d\n", arrayX[i], arrayY[i], arrayArea[i]);
}

while (num-- > 0)
{
for (n = 0; n < num2; n++)
{
printf ("%d ", arrayArea[n]);
if (arrayArea[n] > maiorArea)
{
maiorArea = arrayArea[n];
nn = n;
}
}

printf("\n");
arrayArea[nn] = 0;

if ((velhoX == 0 && velhoY == 0) || (novoX <= velhoX && novoY <= velhoY) || (novoX <= velhoY && novoY >= velhoX))
{
velhoX = novoX;
velhoY = novoY;
numeroBlocos++;
}
}

printf("Teste %d\n%d\n\n", teste++, numeroBlocos);

velhoX = velhoY = 0;
maiorArea = -1;
start = 1;
}
}
fclose(fp);
return 0;
}```
The part in red is the part I beleive is wrong.

The code outputs: (all these numbers are there just to help finding the bug)

10000 2000000 1000000
10000 0 1000000
10000 0 1000000
Teste 1
3

12 35 35 5 16 20
12 0 35 5 16 20
12 0 35 5 16 20
12 0 35 5 16 20
12 0 35 5 16 20
12 0 35 5 16 20
Teste 2
6
But it should output something like this:

10000 2000000 1000000
10000 0 1000000
10000 0 0
Teste 1
3

12 35 35 5 16 20
12 0 35 5 16 20
12 0 0 5 16 20
12 0 0 5 16 0
12 0 0 5 0 0
0 0 0 5 0 0
Teste 2
6
Maybe it's because I've been working on this for a while, but I just can't the error.

12. Should you reset maiorArea at the start of the while (num--) loop?

13. Thank you! It worked, now the numbers are being removed from the array.

I'll try to make the algo work now.

14. I'm confused right now... I beleive I've fixed the program, and it seems to be working now, but when I submit my program to the website it tells me my solution is incorrect.

*The website gets my source file, compiles it and then runs it with some pre determined numbers and test if my program outputs the results is should output.

The site made 10 tests and I got them all wrong. I don't really know what to do right now =P
I'll post my code as it is right now, and I'd like to ask you to try out some numbers and see if the output is incorrect.

I left a line that will display the blocks that will be on the pyramid from top to bottom:

Code:
```#include <stdio.h>

int main ()
{
FILE *fp;
int start = 1, num, arrayX[100], arrayY[100], arrayArea[100], i,n, areaMaior = 0;
int maiorArea = -1, velhoY = 0, velhoX = 0, novoX, novoY, teste = 1, numeroBlocos, num2, nn;

if (!fp)
{
printf("Erro ao ler o arquivo!");
return 1;
}

while (true)
{
if (start)
{
fscanf(fp, "&#37;d", &num);
num2 = num;

if (!num || feof(fp))
{
fclose(fp);
return 0;
}

velhoX = velhoY = 0;
start = 0;
} else {

numeroBlocos = 0;

for (i = 0; i < num2; i++)
{
fscanf(fp, "%d %d", &arrayX[i], &arrayY[i]);
arrayArea[i] = arrayX[i] * arrayY[i];
}

while (num--)
{
maiorArea = -1;

for (n = 0; n < num2; n++)
{
if (arrayArea[n] > maiorArea)
{
maiorArea = arrayArea[n];
nn = n;
}
}

arrayArea[nn] = 0;
novoX = arrayX[nn];
novoY = arrayY[nn];

if ((velhoX == 0 && velhoY == 0) || (novoX <= velhoX && novoY <= velhoY) || (novoX <= velhoY && novoY <= velhoX))
{
velhoX = novoX;
velhoY = novoY;
numeroBlocos++;
printf("%d, %d\n", velhoX, velhoY);
}
}

printf("Teste %d\n%d\n\n", teste++, numeroBlocos);

start = 1;
}
}
fclose(fp);
return 0;
}```