1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Question about sequences in Pascal

  1. Nov 22, 2015 #1
    1. The problem statement, all variables and given/known data
    I have a question about sequences in Pascal. I have been wondering if there is a way to write more than one element of succession on the output at once, without using arrays or strings. I guess best to illustrate on an example.
    Let's take a program that should determine whether some number is a palindrome.
    The code should look something like this:
    Code (Text):

      ...

      begin
      readln (number);
      copy := number;
      modifier := 0;

      while number > 0 do
      begin
          modulo := number mod 10;
          number := number div 10;
          modifier := (modulo + modifier*10);
      end;
      if copy = modifier then
      writeln (copy);

      end.
     
    Basically we just take the number, look at how it looks if written conversely and if it's the same, then we have a palindrome...

    But what if we know wanted to take a whole sequence of numbers and, at the end, write all the ones that were palindromes? Again without any usage of arrays or strings.

    We would need to modify the code, the readln (number) would be at the end of the while cycle so after every number it would ask for a new one.
    Writing 0 would end the cycle. That is pretty clear but how about the output?
    How can we make the program remmember all the palindromes and not lose them during the cycle?

    Thanks


    2. Relevant equations


    3. The attempt at a solution
     
    Last edited: Nov 22, 2015
  2. jcsd
  3. Nov 22, 2015 #2

    Mark44

    Staff: Mentor

    To remember the palindromes, your program needs to store them in some way, such as in an array. Alternatively, you could write them to a file, if you know how to do that.

    Your code, as shown above, has some problems.
    1) prevracene is not initialized, so it shouldn't be used in a comparison (if prevracene = modifier then).
    2) It's not clear what your do loop is trying to accomplish. Try it out for a user input of 909, which is a palindrome.
     
    Last edited: Nov 23, 2015
  4. Nov 22, 2015 #3
    I edited it. The variable was meant to be "copy". Now it works for everything, even 909:)
    Yes, with an array I can see a way to do this, but can we store it without it? And no, I do not know how to write them to a file.

    What I am trying to accomplish here is this:
    User inputs a sequence of numbers (each number on each line) ending with zero. The program determines which of those numbers are palindromes and then writes all of them at once on one line separated by a space.

    Example..

    Input:
    125
    505
    482
    11
    8971
    42424
    0

    Output:
    505 11 42424

    All of that without using arrays/strings.
     
    Last edited: Nov 22, 2015
  5. Nov 22, 2015 #4

    Mark44

    Staff: Mentor

    I don't believe that you can do this. If the user enters a sequence of numbers, they have to be stored somewhere, so that the program can come back and deal with each one.
     
  6. Nov 22, 2015 #5

    gneill

    User Avatar

    Staff: Mentor

    It seems to me that your problem is that your output device is the same as your input device (the console). Why don't you provide the list of numbers as an input file, then you can process it and write the palindromes to the output sequentially and not worry about storing them separately.

    Yes, this just shifts the storage problem to the input side of things from the output side, but it seems natural to have a prepared data set ready to go to feed to the program.
     
  7. Nov 22, 2015 #6
    Because there are some restrictions to what tools I am allowed to use:) Such as external files and arrays.
    Not that I´ve ever worked in Pascal with external files but just out of curiostiy, how would you do it? I could try it, just to learn something new.
     
  8. Nov 22, 2015 #7

    gneill

    User Avatar

    Staff: Mentor

    Can you share the complete set of rules and restrictions? Is there a problem statement?
     
  9. Nov 22, 2015 #8
    User inputs a sequence of integers (each number on each line) ending with zero (not part of the sequence). The program determines which of those numbers are palindromes and then writes those numbers at once. The output numbers are on one line separated by a space.
    (If a number has zeroes at the end, it cannot be a palindrome.)

    Example..

    Input:
    125
    505
    482
    11
    8971
    42424
    0

    Output:
    505 11 42424

    All of that without using arrays/strings. And external files as we have never worked with them:)
     
  10. Nov 22, 2015 #9

    Mark44

    Staff: Mentor

    See http://pascal-programming.info/lesson9.php.

    I'm also interested in knowing what rules and restrictions your program needs to adhere to.
     
  11. Nov 22, 2015 #10

    Mark44

    Staff: Mentor

    This doesn't tell us anything beyond what you already posted earlier in this thread.

    What rules? What restrictions?
     
  12. Nov 22, 2015 #11
    What do you mean? There are no more restrictions than what I have written there.
     
  13. Nov 22, 2015 #12

    gneill

    User Avatar

    Staff: Mentor

    So if your version of Pascal supports it you could use direct cursor addressing on the console to separate input and output areas on the screen? Sounds like a lot of work to avoid using an input or output file.

    There are always cheats and loopholes around rules and restrictions, but they are often more complex than practical. You cannot create temporary storage without using some medium for it such as memory or files. I suppose you could set up communication sockets with buffers and write the output through them and claim it's not an array or file....
     
  14. Nov 22, 2015 #13
    I am almost 100% sure this can be done with some basic stuff, without using anything external. Defining a function or a procedure would probably make no difference here, right?
     
  15. Nov 23, 2015 #14
    Maybe some sort of recursion?
     
  16. Nov 23, 2015 #15

    gneill

    User Avatar

    Staff: Mentor

    Sure, but that's just another "cheat" using the call stack to store the values (along with all the other information involved with process instantiations). You may find it tricky getting the palindromes printed out in the order they were input though. Popping up through the recursion layers tends to be a last-in first-out sort of deal. You might need to do a second set of recursions to reverse the order :smile:
     
  17. Nov 23, 2015 #16
    Hmmm.. it also would put spaces in between the results. I don´t know.. This probably also isn´t the way it is supposed to be done.
     
  18. Nov 23, 2015 #17

    NascentOxygen

    User Avatar

    Staff: Mentor

    The task lends itself beautifully to recursion. The results will be available in reverse order, so after printing each just backspace over it before printing the next. :smile:

    I'd try a raw CR to do the backtracking, providing the console can be set for it; otherwise look at cursor control procedures.
     
  19. Nov 24, 2015 #18

    NascentOxygen

    User Avatar

    Staff: Mentor

    I'm unconvinced that the task does require storage. The fact that echoed input can intermingle with program output when they share the console may be irrelevant to meeting the problem specifications. If the screen presentation is going to be a consideration, then why not simply turn off keyboard echo? :woot: The specs don't state that keyboard input must be echoed, anyway.
    I don't see it asking for the results to be printed all at once; it seems to be asking for each palindrome to be printed immediately it is detected, i.e., at once. Still, I think you need to ask your teacher to clarify this. By using writes rather than writelns you will be printing the numbers all on one line of output, even where the input echoes may make it seem superficially otherwise.

    What operating system are you writing this for? It should be possible to use the operating system to keep input and output separated on the screen, and keep your Pascal code simple and straightforward.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Question about sequences in Pascal
  1. Pascal problem (Replies: 1)

  2. Pascal programming (Replies: 1)

  3. Question about turbine (Replies: 1)

  4. Question about torque (Replies: 1)

Loading...