0

I am just learning Pascal at school and have run into a weird problem in my assignment.

What I need to do is create two arrays and then read in integers for the first array until 10 numbers are read or a negative number is read, then move on to the second array with the same rules.

I have that all working fine except for the first number in the second array is always messed up. -1 seems to always be copied to array 2 index 1.

I can't give away to much code because this is an assignment but it is something like this:

while input >= 0 and index < 10 do
    begin
    read(input);
    array1[index] := input;
        index++
    end;

input:= 0; //to reset it

another while loop but for list2...

If I input for array1 1, 2, 3, -1 and array2 1, 2, 3, 4, -1 my output would be something like:

list 1: 1 list 2: -1
list 1: 2 list 2: 2
list 1: 3 list 2: 3
list 1: -1 list 2: 4

Does this make sense? I just need a little help understand why this is happening, I'm stuck here.

7
  • 1
    You want to necessarily read 10 integers (positive and negative) in the two arrays or the stop condition is a negative number? Commented Sep 28, 2012 at 18:09
  • 1
    I want to only read 10 positive ints, and stop when 10 are read or when the first negative int is read. Commented Sep 28, 2012 at 18:15
  • 2
    There's nothing in the code you gave away that would cause this behavior. The only thing I notice is that you're incrementing some 'index' in the while loop but then testing the terminating while condition against some 'inputs'. BTW, is there really a pascal compiler that would compile this code? Commented Sep 28, 2012 at 18:50
  • 4
    You're expecting someone to point an error in code that he cannot see. Good luck with that.. Commented Sep 28, 2012 at 21:12
  • 2
    @Sam: Then post your real code. As Sertac said, you're expecting us to guess at what might be wrong based on nonsense you posted as a sample. If you want help, provide actual information. If you don't care enough about your question to do that, you might as well not ask for help in the first place. :-) Try going to your doctor and saying "I have a sore on my leg that looks something like this. What's wrong?" and hand him/her a pencil drawing on a piece of paper, and see if you get a diagnosis and treatment. Commented Sep 28, 2012 at 22:59

1 Answer 1

1

As comments to your question have pointed out, it's a bit difficult to find what's wrong when almost certainly the problem is with the code that you didn't post. That being said, however, there are a few visible problems

  1. Read 'input' before the 'while' loop. Entering the 'while' loop is dependent on the initial value of 'input'; I imagine that you are presuming that its initial value is 0, but it could be some garbage number with a negative value.
  2. 'index++' is not Pascal syntax, but C. This should be 'inc (index)'.
  3. Instead of writing 'input:= 0' after the first loop, this should be 'index:= 0'.

I imagine that the code, after the first 'while' loop should be

index:= 0;
readln (input);
while (input >= 0) and (index < 10) do
 begin
  inc (index);
  array2[index]:= input;
  readln (input) // there is no need for a semicolon before 'end'!
 end;
Sign up to request clarification or add additional context in comments.

5 Comments

Just out of interest, why are you refusing to put a semicolon before an end?
There is no need to put a semicolon there; a semicolon is a statement separator and not a statement 'concluder'.
Doesn't that get back to you when you want to add more statements after the previously last one? Not judging or anything, just curious.
Possibly, but in this case, 'readln (input)' has to be the last statement in the 'while' block.
So if I understand correctly, you selectively decide whether to put the semicolon or not depending on whether the last statement may change or not... seems a bit inconsistent, no? I mean, I don't know..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.