Thread: Need help debugging my code

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Consider the four blocks: 10x10 (100), 6x6 (36), 5x7 (35), 3x7 (21). Your program finds the first two as a stack, and then since neither 5x7 nor 3x7 fit that's it. However, blocks 1, 3, and 4 stack nicely, meaning the answer should be 3.

  2. #17
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    A brute force algorithm could be used, but i would like to see a nice description of your own once you find one.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #18
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Lala, I have to admit, I'm a little bit of a junkie when it comes to programming problems, which is why I asked you to post a link to the problem.

    Getting over the terrible altavista babelfish translation of that site, I managed to get the gist of the problem and wrote a solution. It works for all the cases. If you want to discuss the problem let me know, although I know that it would be much more satisfying if you figured it out yourself.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Google's Translator is far better than Babelfish, so you know.
    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.

  5. #20
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Elysia View Post
    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.
    Shure, a compiler can diagnose if parameters to the the scanf family of functions are not pointers but only if the compiler treats diagnosing this functions differently.
    Generally it is impossible for the compiler to check the type of parameters that are passed to functions that use variable argument lists. Also there is no way for the compiler to know if a paramiter is an output parameter ( must be a pointer ) or an input parameter ( could be a pointer ).
    Kurt

  6. #21
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    arpsmack

    I'd really like to see your solution... I've been struggling with this problem for a while and haven't gotten anywhere =/

    Perhaps you could just describe your algo and let me try to implement it....

    BTW did you submit your solution? Did you score all points?

  7. #22
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Dam.... I just realised... How is the site going to input my program?

    There's nowhere telling me how...I'm reading from a file but how am I supposed to know if they're using a file too? And if so what's the files name?

  8. #23
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    site will redirect stdinput and output, so your program should read from stdinput and output to stdoutput
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #24
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Vart

    So should I start the program with something like:

    Code:
    char str[1000];
    scanf("%1000s", str);
    And then break that string into the numbers I need? Is there a way to know the length of the input?

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that you should specify the size of your buffer - 1 to scanf because it will read the number of chars you specify. You need space for the NULL char too.
    Instead of writing from a file, read chunks from stdin instead, just as if inputting info from a user.
    I believe this is what it's all about.
    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.

  11. #26
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    So should I start the program with something like:
    No

    Is there a way to know the length of the input?
    No, and no need.
    When you read a number of blocks and it is zero - exit your main loop

    So in pseudocode it will be something like
    Code:
    while(read number)
    {
       if(number == 0) break;
       for(i=0;i<number;i++)
       {
          read line;
          split it in block sizes;
       }
       order blocks;
       count piramid hight and ouput it;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #27
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Thanks! Now I finally scored something =P I got 20/100 points, now I beleive the problem is only whithin my algorithm.

    In case anyone wants to know:

    Code:
    #include <stdio.h>
    
    struct caixas
    {
        unsigned int x[5000];
        unsigned int y[5000];
        unsigned int area[5000];  
    };
    
    int main (int argc, char *argv[])
    {
        struct caixas caixa;
        int start = 1, num, i, n, maiorArea, velhoY = 0;
        int velhoX = 0, teste = 1, numeroBlocos, num2, nn;
    
        while (true)
        {
              if (start)
              {              
                  if (scanf("&#37;d", &num) == EOF || !num)
                     return 0;
                     
                  num2 = num;
                  velhoX = velhoY = 0;
                  start = 0;
              } else {
    
                  numeroBlocos = 0;
                  
                  for (i = 0; i < num2; i++)
                  {
                      scanf("%d %d", &caixa.x[i], &caixa.y[i]);
                      caixa.area[i] = caixa.x[i] * caixa.y[i]; 
                  }
    
                  while (num--)
                  {                  
                      maiorArea = 0;
    
                      for (n = 0; n < num2; n++)
                      {
                          if (caixa.area[n] > maiorArea)
                          {
                              maiorArea = caixa.area[n];
                              nn = n;
                          }
                      }
    
                      caixa.area[nn] = 0;
                      
                      if ((!velhoX && !velhoY) || (caixa.x[nn] <= velhoX && caixa.y[nn] <= velhoY) || (caixa.x[nn] <= velhoY && caixa.y[nn] <= velhoX))
                      {
                          velhoX = caixa.x[nn];
                          velhoY = caixa.y[nn];
                          numeroBlocos++;
                      }
                  }
    
                  printf("Teste %d\n%d\n\n", teste++, numeroBlocos);
              
                  start = 1;
              }
        }
        return 0;
    }
    I got one more question now though...

    Since I altered the program to read from stdin I can not test it here... How do I make the input?
    Last edited by lala123; 03-15-2008 at 10:54 AM.

  13. #28
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Just put the input set in a text file (like boxes.txt) and execute your program on the command line like this:

    boxes < boxes.txt

    Ok now here's a description of how I solved the problem:

    Code:
    1) Read in N (the number of boxes)
    2) Read in each box's length and width into an array (I used an array of structs)
       If a box's length is less than its width, swap the two values
       (this is needed to make sorting easier)
    4) Sort (in increasing order) the array of boxes by their largest dimension, if two boxes have
       the same largest dimension, sort by the sum of their dimensions
    5) See the following pseudo-code:
    
       // stacks[n] = the number of boxes in the stack with box n at the bottom
       // This is initialized to all 1's since each box is initially in its own stack
       stacks = integer array of size N
       globalMax = 1
       for j in 1 to N-1
          localMax = stacks[j]
          for k in 0 to j-1
             if box[k] fits on top of box[j]
                localMax = max(localMax, stacks[k] + stacks[j]);
          stacks[j] = localMax
          globalMax = max(globalMax, localMax)
    
    6) The answer is now in globalMax
    
    Here's a basic skeleton of my program:
    
    int main(void) {
       // declare variables
    
       while (1) {
          // read in N
          if (N == 0) break;
    
          // create an array big enough to hold N boxes (optionally you could just use
          // a statically allocated array large enough to hold the max value for N)
    
          // read in boxes
    
          // sort boxes
    
          // do algorithm
    
          // print result
       }
    }
    The algorithmic part operates in O(N^2) (approximately N^2 / 2). The sort, assuming you use a decent sorting algorithm, should take O(n log n). Is this the optimal solution? Probably not. But it works, and yes I submitted it and it gets all 100 points.
    Last edited by arpsmack; 03-15-2008 at 06:34 PM. Reason: Wasn't consistent with array indexing

  14. #29
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Amazing =) Thank you.

    The awnser is actually much more complicated than what I had imagined :P

  15. #30
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    I'll now go and try to implement arpsmack's solution but before that I have one more problem I couldn't solve =/

    This one is so easy... I'm actually ashamed for having to ask for help in this one...

    He says that he'll give a list in this format:

    Code:
    4
    4 5 3 1
    10
    9 8 7 6 1 4 3 2 12 10
    0
    He then asks you to identify where the number equals it's position in the list. The correct output would be:

    Code:
    Teste 1
    3
    
    Teste 2
    10
    So I came up with this:

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        unsigned int start = 1, numero = 0, num,  c, posicao = 1, teste = 1;
        while (1)
        {
            if (scanf("&#37;d", &num) == EOF || !num)
                return 0;
    
            posicao = 1;
            while ((c = getc(stdin)) != '\n')
            {
                if (c == 32) // if c equals space
                {
                    if (numero == posicao++)
                    {
                        printf("Teste %d\n%d\n\n", teste++, posicao - 1);
                        break;
                    }
                    numero = 0;
                 } else
                     numero = numero*10 + (c - 48); // (c - 48) turns '9' into 9
              }
        }
        return 0;
    }
    Wich seemed to work, but when I submitted my solution I only got 10/50 points... Any help would be really appreciated.

    Here's the link to the problem: http://olimpiada.ic.unicamp.br/prati...vel2/quermesse

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