: : : I am a student taking a class in Turbo Pascal (the 5th Ed. book).
: : : I'm terrible at programming and missed the last class. I haven't
: : : been able to find anyone that could help me understand the last work
: : : we were given . I was hoping someone might be able
: : : to explain this to me. I won't receive a grade in this until I complete
: : : all my work and I don't seem to be able to get past this.
: : :
: : : self-check pg. 439 #1: (on arrays) Turbo Pascal
: : : The following sequence of statements changes the contents of array X.
: : : Describe what each statement does to the array, and show the final
: : : contents of array X after all statements execute.
: : :
: : : X = array
: : : I = index range
: : :
: : : I :=3;
: : : X[I] := X[I] + 10.0;
: : : X[I - 1] := X[2 * 1 - 1];
: : : X[I + 1] := X[2 * 1] + X[2 * I + 1);
: : : for I :=3 downto 1 do
: : : X[I + 1] := X[I]
: : :
: : : I would appreciate any help I can get with this.
: : : Serina
: :
: :
: : Thanx Nat,
: I appreciate your time. I think I understand it better.
: Is this right?
: 1. I :=3; {index = 3}
:
: 2. X[3] := X[3] +10; {stores value of 10 in index 3}
:
: 3. X[3 - 1] := X[2 * 3 - 1]; {stores same value in index [2] as in index[5]}
:
: 4. X[3 + 1] := X[2 * 3] + X[2 * 3 + 1]; {index 4 = sum of index 6 and index 7}
:
: 5. for I := 5 to 7 do {initilizes I to 5}
:
: 6. X[I] := X[I + 1]; {adds 1 to I}
: {index 5 := 5 + 1}
: {index 6 := 6 + 1}
: {index 7 := 7 +1}
:
: 7. for I := 3 down to 1 do {initializes I to 3}
:
: 8. X[3 + 1] := X[3] {adds 1 to I}
: {X[2 + 1] := X[2]}
: {X[1 + 1] := X[1]}
:
: Does this look ok or am I still way off?
: Thanx,
: Serina
:
A number of your explanations is still a little off. These are 2 and 5 thru 8. 5 & 6 are actually 2 parts of the same statement:
for I := 5 to 7 do
X[I] := X[I + 1]; { <== statement ends here }
Just remember that Pascal uses a semicolon as statement separator. The for-do loop does a little more than initializing I to 5. It also checks if I <= 7 and increments I with each step. The same goes for 7 & 8.