Recent

Author Topic: need help with calender  (Read 49779 times)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #60 on: September 23, 2017, 01:23:21 am »
Code: Pascal  [Select][+][-]
  1.   rank[i].score:=rank[i+1].score;
  2.      rank[i].name:=rank[i+1].name;    

isn't it this?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #61 on: September 23, 2017, 08:21:35 am »
isn't it this?
Sigh  :-\
Let's take another approach. Lets draw it out.

You have a scoreboard top 5 with the following values: 43, 55, 75, 80 and 85.
Let draw (please do this with me on a peace of paper):
Code: [Select]
rank 1 = 43
rank 2 = 55
rank 3 = 75
rank 4 = 80
rank 5 = 85
So we now have a new score of 33.
What do you want to happen... please draw with me...
Code: [Select]
rank 1 = 43          rank 1 = new spot 33
rank 2 = 55          rank 2 = 43 (moved down one)
rank 3 = 75   ---->  rank 3 = 55 (etc)
rank 4 = 80          rank 4 = 75
rank 5 = 85          rank 5 = 80
You see the second column. That's the new ranking you want, right?

Now lets see your code.
Code: Pascal  [Select][+][-]
  1. rank[i] := rank[i + 1]
What does that do? If you have a loop of I := 1 to 5 what does this do for each rank ?

Let even make it easier... What does it do for just I := 1; ?
Could you give me the new rank-array if you do this?
Code: Pascal  [Select][+][-]
  1. rank[1].score := rank [1 + 1].score
(You drew with me on a piece of paper so you could give me the answer)

See what's going wrong?
If you don't please give me the ranking you think happens after that line.

If you do see what's wrong we can go to the next step. Because we can't just iterate forward through that list. But that will become clear if you do the second iteration (and draw it on that piece of paper).

(has anybody got good learning material about arrays and moving/inserting items in that array)
« Last Edit: September 23, 2017, 08:23:29 am by rvk »

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #62 on: September 23, 2017, 09:08:48 am »
isn't it this?
Sigh  :-\
Let's take another approach. Lets draw it out.

You have a scoreboard top 5 with the following values: 43, 55, 75, 80 and 85.
Let draw (please do this with me on a peace of paper):
Code: [Select]
rank 1 = 43
rank 2 = 55
rank 3 = 75
rank 4 = 80
rank 5 = 85
So we now have a new score of 33.
What do you want to happen... please draw with me...
Code: [Select]
rank 1 = 43          rank 1 = new spot 33
rank 2 = 55          rank 2 = 43 (moved down one)
rank 3 = 75   ---->  rank 3 = 55 (etc)
rank 4 = 80          rank 4 = 75
rank 5 = 85          rank 5 = 80
You see the second column. That's the new ranking you want, right?

Now lets see your code.
Code: Pascal  [Select][+][-]
  1. rank[i] := rank[i + 1]
What does that do? If you have a loop of I := 1 to 5 what does this do for each rank ?

Let even make it easier... What does it do for just I := 1; ?
Could you give me the new rank-array if you do this?
Code: Pascal  [Select][+][-]
  1. rank[1].score := rank [1 + 1].score
(You drew with me on a piece of paper so you could give me the answer)

See what's going wrong?
If you don't please give me the ranking you think happens after that line.

If you do see what's wrong we can go to the next step. Because we can't just iterate forward through that list. But that will become clear if you do the second iteration (and draw it on that piece of paper).

(has anybody got good learning material about arrays and moving/inserting items in that array)

sorry i still do not know what's going wrong

Code: Pascal  [Select][+][-]
  1. rank[1].score := rank [1 + 1].score

that becomes to

Code: Pascal  [Select][+][-]
  1. rank[1].score := rank [2].score

so isn't that right?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #63 on: September 23, 2017, 09:14:13 am »
sorry i still do not know what's going wrong

Code: Pascal  [Select][+][-]
  1. rank[1].score := rank [1 + 1].score

that becomes to

Code: Pascal  [Select][+][-]
  1. rank[1].score := rank [2].score

so isn't that right?
You didn't draw it out, did you?

Take a piece of paper and begin drawing it out.
If you have the rank array of 43, 55, 75, 80 and 85
and you do the rank[1] = rank[2]... what is the rank-array then ??

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #64 on: September 23, 2017, 09:58:46 am »
sorry i still do not know what's going wrong

Code: Pascal  [Select][+][-]
  1. rank[1].score := rank [1 + 1].score

that becomes to

Code: Pascal  [Select][+][-]
  1. rank[1].score := rank [2].score

so isn't that right?
You didn't draw it out, did you?

Take a piece of paper and begin drawing it out.
If you have the rank array of 43, 55, 75, 80 and 85
and you do the rank[1] = rank[2]... what is the rank-array then ??


rank[1] = rank[2]
rank[2] = rank[3]
rank[3] = rank[4]
rank[4] = rank[5]
rank[5] = rank[6]

rank[1] = 55
rank[2] = 75
rank[3] = 80
rank[4] = 85
rank[5] = x

so the 1st has 2nd value and 2nd has third value

so

Code: Pascal  [Select][+][-]
  1. rank[1] = score(new value)
  2. rank[2] = rank[1]
  3. rank[3] = rank[2]
  4. rank[4] = rank[3]
  5. rank[5] = rank[4]

should it be like that?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #65 on: September 23, 2017, 10:13:49 am »
rank[1] = rank[2]
rank[2] = rank[3]
rank[3] = rank[4]
rank[4] = rank[5]
rank[5] = rank[6]

rank[1] = 55
rank[2] = 75
rank[3] = 80
rank[4] = 85
rank[5] = x
Yep. That's it. You see now that all the scores are moved UP. And you wanted to move them down to make place on spot 1.

Quote
Code: Pascal  [Select][+][-]
  1. rank[1] = score(new value)
  2. rank[2] = rank[1]
  3. rank[3] = rank[2]
  4. rank[4] = rank[3]
  5. rank[5] = rank[4]
should it be like that?
Almost !!!. But again... lets draw it out.
You have 43, 55, 75, 80 and 85
What happens if you do the first line?
You get (new), 55, 75, 80 and 85
Where is value 43 gone?

And if you take rank[2]=rank[3] and the rest of the lines... what happens if you draw it out each line by line.
rank[2] = rank[3] will give you
(new), 75, 75, 80 and 85
Do you see what's going wrong.

If you don't see it yet... let's do it another way. Write down 33, 43, 55, 75, 80 and 85 on a small piece of paper.
Put 43 to 85  on the table above each other (like in the drawing).
Now remove 85 from it's spot (it's a score which will be discarded because it's too high).
Now, how (AND IN WHAT ORDER) would you move the numbers from spot to spot so that you get the following order: 33(new), 43, 55, 75 and 80.

(Please really do it with those pieces of paper. It will become really clear.)

(PS. I do this with you very slow. Because this is some vital understanding of arrays and you need to learn and try this a few times yourself. It will "burn" in your memory and in a few month you look upon this moment and say... yeah, that's when I got it)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #66 on: September 23, 2017, 10:33:44 am »
sorry i've drawn out on the paper but i still do not get it.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #67 on: September 23, 2017, 10:34:15 am »
can you give me any hint?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #68 on: September 23, 2017, 10:38:18 am »
If you have those numbers 43, 55, 75, 80 and 85 in front of you on the table and you remove 85 from spot 5 and want to move the others to their new spot.... what is the first piece of paper you are going to move and to what spot?

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #69 on: September 23, 2017, 10:54:01 am »
80 to spot 5
75 to spot 4
55 to spot 3
43 to spot 2?

so
rank 5 = rank 4
rank 4 = rank 3
rank 3 = rank 2
rank 2 = rank 1

like that?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #70 on: September 23, 2017, 11:03:13 am »
like that?
Yes, you've got it. And last you can put the new highest number on spot 1.

Now you need to do that in a loop.
But the loop can't go from 2 to 4 but needs to go from 4 to 2.
You can read here how you can do that:
http://wiki.lazarus.freepascal.org/FOR..DO

Now you can go back to the AddRank procedure. In there you first need to see where the new number needs to be in the rank. Because if you want to add 60 as score it needs to go on spot 3 and only 3 and 4 need to move down (so not 2 to 4).

Yesterday at 16:22 I gave you the code how to get the correct spot with a while loop.
Use that code in AddRank and use the above for-loop to move the correct values down so you can add the new number at the correct spot in the rank. (Now you can go coding again :) )

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #71 on: September 23, 2017, 11:10:39 am »
wait so

Code: Pascal  [Select][+][-]
  1. rank[1] = score(new value)
  2. rank[2] = rank[1]
  3. rank[3] = rank[2]
  4. rank[4] = rank[3]
  5. rank[5] = rank[4]

is different to that?

Code: Pascal  [Select][+][-]
  1. rank[1] = score(new value)
  2. rank[5] = rank[4]
  3. rank[4] = rank[3]
  4. rank[3] = rank[2]
  5. rank[2] = rank[1]

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #72 on: September 23, 2017, 11:14:22 am »
Code: Pascal  [Select][+][-]
  1. procedure Tform1.AddRank(Namee: String; Score: Integer);
  2. begin
  3.  x := 1;
  4. while (x <= 10) and (rank[x].score >= score) do inc(x);
  5. if x <= 10 then // are we still below 10?, so we broke out the previous loop with score > rank[x].score
  6. begin
  7.  for i:= 10 downto x+1 do
  8.     begin
  9.  
  10.      rank[i].score:=rank[i-1].score;
  11.      rank[i].name:=rank[i-1].name;
  12.      score:=rank[x].score;
  13.     namee:=rank[x].name;
  14.     reset(highscorefile);
  15.     seek(highscorefile, filesize(highscorefile));
  16.     write(highscorefile, info);
  17.     closefile(highscorefile);
  18.  
  19.     end;
  20.  
  21.   // now move everything from x onward one down and put score on rank[x]
  22. end;
  23. end;                        

so is it like this?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #73 on: September 23, 2017, 11:30:31 am »
wait so
Code: Pascal  [Select][+][-]
  1. rank[1] = score(new value)
  2. rank[2] = rank[1]
  3. rank[3] = rank[2]
  4. rank[4] = rank[3]
  5. rank[5] = rank[4]
is different to that?
Code: Pascal  [Select][+][-]
  1. rank[1] = score(new value)
  2. rank[5] = rank[4]
  3. rank[4] = rank[3]
  4. rank[3] = rank[2]
  5. rank[2] = rank[1]
Yes. (Although in the last code you need to move rank[1] = new score to the end)

If you look at the first code you see that you do rank[2]=rank[1]
Then in the next you do rank[3]=rank[2].
But you've ALREADY DONE rank[2]=rank[1] so doing rank[3]=rank[2] AFTER rank[2]=rank[1]
is the same as rank[2]=rank[1] and rank[3]=rank[1].
You see that rank[2] and rank[3] get the SAME value.

So the ORDER in which you do these assignments is really important.

Remember the pieces of paper on your table. You couldn't move the piece of paper in spot 1 to spot 2 because there was already a piece in spot 2. You saw immediately that the only way that was going to work if you began at the end and FIRST moved the piece on spot 4 to spot 5. Didn't you?

Again... the ORDER in which you do these assignments is really important.
(and visually doing it with drawing it out or writing it on a piece of paper made it really clear.)

That also goes for your first line in your second code above.
rank[1] = score(new value)
You can't do that yet if you had those pieces of paper on the table. Spot 1 still has that 43 score. So you can't put the 33 (new score) on spot 1 because you first need to move all the other pieces. That's why you needed to put the line rank[1] = new score at the end.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #74 on: September 23, 2017, 11:35:00 am »
Code: Pascal  [Select][+][-]
  1. procedure Tform1.AddRank(Namee: String; Score: Integer);
  2. begin
  3.  x := 1;
  4. while (x <= 10) and (rank[x].score >= score) do inc(x);
  5. if x <= 10 then // are we still below 10?, so we broke out the previous loop with score > rank[x].score
  6. begin
  7.  for i:= 10 downto x+1 do
  8.     begin
  9.  
  10.      rank[i].score:=rank[i-1].score;
  11.      rank[i].name:=rank[i-1].name;
  12.      score:=rank[x].score;
  13.     namee:=rank[x].name;
  14.     reset(highscorefile);
  15.     seek(highscorefile, filesize(highscorefile));
  16.     write(highscorefile, info);
  17.     closefile(highscorefile);
  18.  
  19.     end;
  20.  
  21.   // now move everything from x onward one down and put score on rank[x]
  22. end;
  23. end;                        

so is it like this?
Almost.
But you have a line:
Code: Pascal  [Select][+][-]
  1. score:=rank[x].score;
That's the wrong one. You want score into rank[ x ].
Also... you need to move the assignment of the new score and the writing to the file outside the for-loop.

 

TinyPortal © 2005-2018