Thread: Need help debugging my code

  1. #1
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91

    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;
        fp = fopen("entrada.txt", "r");
        
        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?

    Thank you for your help.

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    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. #3
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Well then how should I pass the pointers?

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

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    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. #5
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    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. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And as for this:
    Code:
    fscanf(fp, "&#37;d %d", &arrayX[i], &arrayY[i]);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    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. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    lala, do you have a link to the original problem text that I could look at?

  10. #10
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    So how did it go? Did you solve it?

  12. #12
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    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, "&#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.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Should you reset maiorArea at the start of the while (num--) loop?

  14. #14
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Thank you! It worked, now the numbers are being removed from the array.

    I'll try to make the algo work now.

  15. #15
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    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;
        fp = fopen("entrada.txt", "r");
        
        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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. backward debugging in Visual Studio??
    By George2 in forum Tech Board
    Replies: 12
    Last Post: 11-05-2006, 02:17 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. Replies: 8
    Last Post: 04-28-2003, 07:29 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Debugging leads to buggy code and longer hours?
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-28-2002, 11:14 AM