Question about sequences in Pascal

In summary, The conversation discusses the possibility of writing more than one element of succession in Pascal without using arrays or strings. The conversation includes an example of a program that determines whether a number is a palindrome and the group explores the challenge of writing a whole sequence of numbers that are palindromes without using arrays or strings. The conversation suggests storing the palindromes in an array or writing them to a file, but the group is restricted from using these tools. They consider using an input file instead, but the restriction of not using arrays or strings still poses a challenge.
  • #1
tawi
33
0

Homework Statement


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:
  ...

  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

Homework Equations

The Attempt at a Solution

 
Last edited:
Physics news on Phys.org
  • #2
tawi said:

Homework Statement


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.
Mod note: Modified the following code as follows:
1. Changed the starting code tag to [code=pascal]

2. Indented code to increase readability

The code should look something like this:
Code:
  ...

  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 prevracene = modifier then
        writeln (prevracene);

  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?
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:
  • #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:
  • #4
tawi said:
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.
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.
 
  • #5
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.
 
  • Like
Likes Mark44
  • #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.
 
  • #7
Can you share the complete set of rules and restrictions? Is there a problem statement?
 
  • #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:)
 
  • #9
tawi said:
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.
See http://pascal-programming.info/lesson9.php.

I'm also interested in knowing what rules and restrictions your program needs to adhere to.
 
  • #10
tawi said:
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:)
This doesn't tell us anything beyond what you already posted earlier in this thread.

What rules? What restrictions?
 
  • #11
What do you mean? There are no more restrictions than what I have written there.
 
  • #12
tawi said:
What do you mean? There are no more restrictions than what I have written there.
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...
 
  • #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?
 
  • #14
Maybe some sort of recursion?
 
  • #15
tawi said:
Maybe some sort of recursion?
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:
 
  • #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.
 
  • #17
tawi said:
Maybe some sort of recursion?
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.
 
  • #18
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.
The program determines which of those numbers are palindromes and then writes those numbers at once.
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.
 

FAQ: Question about sequences in Pascal

1. What is a sequence in Pascal?

A sequence in Pascal is a series of values that follow a specific pattern or rule. It can be represented as a list of numbers, characters, or other data types.

2. How do you create a sequence in Pascal?

To create a sequence in Pascal, you can use the "for" loop or the "while" loop to generate a series of values based on a given rule or pattern. You can also use arrays to represent a sequence of values.

3. What is the difference between an arithmetic and geometric sequence in Pascal?

An arithmetic sequence in Pascal is a sequence where each term is obtained by adding a constant value to the previous term. A geometric sequence, on the other hand, is a sequence where each term is obtained by multiplying the previous term by a constant value.

4. How can you find the nth term in a sequence in Pascal?

To find the nth term in a sequence in Pascal, you can use the formula "a + (n-1)d" for an arithmetic sequence, where "a" is the first term and "d" is the common difference. For a geometric sequence, you can use the formula "ar^(n-1)", where "a" is the first term and "r" is the common ratio.

5. What are some real-life applications of sequences in Pascal?

Sequences in Pascal are used in many real-life applications, such as generating number patterns, calculating compound interest rates, and creating computer algorithms. They are also commonly used in physics, engineering, and other scientific fields to model and predict natural phenomena.

Back
Top