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;
fp = fopen("entrada.txt", "r");
if (!fp)
{
printf("Erro ao ler o arquivo!");
return 1;
}
while (true)
{
if (start)
{
fscanf(fp, "%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.