Thread: ACM Problem Confusion

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    ACM Problem Confusion

    Hello:
    I am attempting at this Online Judge problem:
    http://icpcres.ecs.baylor.edu/online...m&problem=1464

    The mathematics calculation part is extremely easy; but I do not understand what type of input will be given to my problem; therefore I do not know how to do this problem.
    Am I expecting the input to be from a flat text file? If so; then I would need to go through every line and calculate the answer; put it into an array and output the entire array at the end... I would keep doing that until the EOF character right? How do I look for the EOF character?
    Sorry for the beginner questions; but I am really confused by what this problem really wants... Any input would be greatly appreciated!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Am I expecting the input to be from a flat text file?
    That is possible. It is also possible that it is from stdin perhaps by input redirected from a file.

    If so; then I would need to go through every line and calculate the answer; put it into an array and output the entire array at the end... I would keep doing that until the EOF character right?
    Yes.

    How do I look for the EOF character?
    That depends on how you read the input. For example, fscanf() returns EOF if EOF is reached. Note that EOF is not really a character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Most online code tests are based on redirecting a text file as input and redirecting the apps output to a result file, then comparing the result with a template result. The input may be the same all the time, or generated at runtime, and the template result generated by a "known good" application.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Thanks for the help guys.
    However; so I am going to assume the input is from a text file... then what would the loop for the input look like? Would I use a "do-while" loop or another one? I am thinking I would do something like this:
    Code:
    do
      {
        /* calculate and get result */
        //put result into array
      }
    while (*test for EOF*)
    But the obvious questions are... how would I make the array if I have no idea how big it will be? I thought dynamic arrays did not exist in C...
    Also; and what would the test for EOF look like for redirected text files?


    Thanks

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why do you need to put the result in an array? You should be fine to just output the result when you have finished one calculation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Didn't you ask this problem before?

    Do not store the answers. Print the result as soon as it is computed.

    As for the reading in of numbers: the test for EOF doesn't change depending on whether it's redirected from stdin or read using fscanf (or similar). When you have tried to read a number that isn't there, feof is set true for that file handle.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    All input for those problems will be redirected from a file to stdin. A basic skeleton for a competition problem typically looks like this:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void) {
       int num, answer;
    
       while (1) {
          // read input
          scanf("%d", &num);
    
          // handle termination condition
          if (num == 0) break;
    
          // do calculations
          answer = num * 2;
    
          // print answer
          printf("%d\n", answer);
       }
    
       return 0;
    }
    To test your program, type their sample test case into a text file (input.txt), save it, and run your program like this:

    prog < input.txt

    Some competitions require you to print EXACTLY the right number of newline characters (meaning your last output would not have a newline after it). The Online Judge problems do not (from what I have seen), so make sure you print a single newline after every output (unless the problem states otherwise). If you fail to do this, your submission will get flagged "wrong answer", and it will probably drive you crazy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM