Recent

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

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: need help with calender
« Reply #150 on: September 25, 2017, 02:13:17 am »
can i still sort it out in order without erasing the writing?
If by "without erasing the writing" you meant without 'deleting' the characters from your console-output, then the answer to that question would be yes.

Remember that using Write and WriteLn is only meant to help you "the programmer" to print out and validate the current state of your program and/or variables. That, and informing the end-user if that would be required for your application/program (ask for input, show progress etc).

So, for instance and as a simple example, it would be perfectly fine to print out the following lines in order to verify your progress when sorting:
Before sorting: 6 5 3 1 8 7 2 4
iteration 1: 5 6 3 1 8 7 2 4
iteration 2: 3 5 6 1 8 7 2 4
iteration 3: 1 3 5 6 8 7 2 4
iteration 4: 1 3 5 6 7 8 2 4
iteration 5: 1 2 3 5 6 7 8 4
iteration 6: 1 2 3 4 5 6 7 8
after sorting: 1 2 3 4 5 6 7 8

That way you are able to see what your program does before, after and on/for each iteration of your sorting loop.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #151 on: September 25, 2017, 06:18:46 am »
hi should i code for insertion sort?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: need help with calender
« Reply #152 on: September 25, 2017, 06:22:29 am »
hi should i code for insertion sort?
I have no idea what you were coding  :D

If you would like to implement your own sorting routines, then please feel free to do so, but also please don't do that on my account.

My post was simply meant as an example. Also because we do not know/understand what you meant by "without erasing the writing".

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #153 on: September 25, 2017, 06:33:55 am »
hi i meant how should i code for insertion sort?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: need help with calender
« Reply #154 on: September 25, 2017, 06:43:12 am »
ah, that means something completely different  ;D

Did you have a good look at the picture that rvk posted ?

Here is another animated image that shows how an insertion sort is done, but it shows a bit more details on the steps that are done in between (make sure that you select insertion sort in case the link is not pointing automatically to it, and press the green insertion sort link to show the animation together with the pseudo-code at the bottom right).

And did you also took note of rvk's remark to put things down on paper ?

What would you do, if you have a couple of numbers in an array that is unsorted. You can use the numbers from rvk's picture (and that i used in my post) as example.

So you start with an array of numbers:
6 5 3 1 8 7 2 4

what would you do on paper (see also the animated pictures) to have these numbers sorted correctly from lowest to highest number ?

If you are able to write that down using plain english (*) words/sentences then you are already half way there to implement that in a program.

(*) you can of course use your native tongue for that, but please translate those to English first when you paste them in a post.
« Last Edit: September 25, 2017, 07:12:13 am by molly »

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #155 on: September 25, 2017, 07:22:28 am »
hi i got the idea form the animated image and i came up with this
Code: Pascal  [Select][+][-]
  1.  randomize;
  2.   for i:=1 to 10 do
  3.     begin
  4.       rank[i]:=random(500);
  5.     end;
  6.  
  7.  
  8.    for i:= 2-1 downto 1 do
  9.   begin
  10.    if rank[2] < rank[i] then
  11.     rank[i+1]:=rank[i];
  12.    if rank[2]> rank[i] then
  13.     rank[i+1]:=rank[2];
  14.   end;
  15.  
  16.   for i:= 3-1 downto 1 do
  17.   begin
  18.    if rank[3] < rank[i] then
  19.     rank[i+1]:=rank[i];
  20.    if rank[3]> rank[i] then
  21.     rank[i+1]:=rank[3];
  22.   end;
  23.  
  24.   for i:= 4-1 downto 1 do
  25.   begin
  26.    if rank[4] < rank[i] then
  27.     rank[i+1]:=rank[i];
  28.    if rank[4]> rank[i] then
  29.     rank[i+1]:=rank[4];
  30.   end;
  31.  
  32.   for i:= 5-1 downto 1 do
  33.   begin
  34.    if rank[5] < rank[i] then
  35.     rank[i+1]:=rank[i];
  36.    if rank[5]> rank[i] then
  37.     rank[i+1]:=rank[5];
  38.   end;
  39.  
  40.   for i:=1 to 10 do
  41.    begin
  42.     writeln(inttostr(rank[i]));
  43.    end;                              

but it's not working

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: need help with calender
« Reply #156 on: September 25, 2017, 07:42:42 am »
Step forwards and then step backwards, why the goal is not approaching?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: need help with calender
« Reply #157 on: September 25, 2017, 07:54:35 am »
hi i got the idea form the animated image and i came up with this
...
but it's not working
Not bad !, not bad at all  :)

Just forget the fact that it is not working, you seem to have a good idea on approach. It is just a bit poorly executed.

I took the liberty to write a console application that uses you sorting implementation and adapted things a little so that it shows more information:
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. var
  9.   rank : array[1..10] of integer;
  10.   i : integer;
  11.  
  12. procedure DisplayRanks(nr: integer = 0);
  13. begin
  14.   Write(nr, '  :  ');
  15.   for i:=1 to 10 do
  16.   begin
  17.     write(inttostr(rank[i]), '  ');
  18.   end;
  19.   WriteLn;
  20. end;
  21.  
  22.  
  23. begin
  24.   // for testing purposes we want to have the same numbers each and every time.
  25.   // Otherwise we could end up searching for a needle in a haystack
  26.   // randomize;
  27.  
  28.   for i:=1 to 10 do
  29.   begin
  30.     rank[i]:=random(500);
  31.   end;
  32.  
  33.   DisplayRanks(0);
  34.  
  35.   for i:= 2-1 downto 1 do
  36.   begin
  37.     if rank[2] < rank[i] then
  38.     begin
  39.       WriteLn('rank[',i+1, '] := rank[',i,']');
  40.       rank[i+1]:=rank[i];
  41.     end;
  42.     if rank[2] > rank[i] then
  43.     begin
  44.       WriteLn('rank[',i+1, '] := rank[',2,']');
  45.       rank[i+1]:=rank[2];
  46.     end;
  47.   end;
  48.  
  49.   DisplayRanks(1);
  50.  
  51.   for i:= 3-1 downto 1 do
  52.   begin
  53.     if rank[3] < rank[i] then
  54.     begin
  55.       WriteLn('rank[',i+1, '] := rank[',i,']');
  56.       rank[i+1]:=rank[i];
  57.     end;
  58.     if rank[3] > rank[i] then
  59.     begin
  60.       WriteLn('rank[',i+1, '] := rank[',3,']');
  61.       rank[i+1]:=rank[3];
  62.     end;
  63.   end;
  64.  
  65.   DisplayRanks(2);
  66.  
  67.   for i:= 4-1 downto 1 do
  68.   begin
  69.     if rank[4] < rank[i] then
  70.     begin
  71.       WriteLn('rank[',i+1, '] := rank[',i,']');
  72.       rank[i+1]:=rank[i];
  73.     end;
  74.     if rank[4] > rank[i] then
  75.     begin
  76.       WriteLn('rank[',i+1, '] := rank[',4,']');
  77.       rank[i+1]:=rank[4];
  78.     end;
  79.   end;
  80.  
  81.   DisplayRanks(3);
  82.  
  83.   for i:= 5-1 downto 1 do
  84.   begin
  85.     if rank[5] < rank[i] then
  86.     begin
  87.       WriteLn('rank[',i+1, '] := rank[',i,']');
  88.       rank[i+1]:=rank[i];
  89.     end;
  90.     if rank[5] > rank[i] then
  91.     begin
  92.       WriteLn('rank[',i+1, '] := rank[',5,']');
  93.       rank[i+1]:=rank[5];
  94.     end;
  95.   end;
  96.  
  97.   DisplayRanks(4);
  98. end.
  99.  

Apart from the fact that your array contains 10 numbers (so needs more than 4 iterations) there seems to be going something else wrong:

This is my output:
Code: [Select]
0  :  274  296  357  422  301  428  272  423  211  311
rank[2] := rank[2]
1  :  274  296  357  422  301  428  272  423  211  311
rank[3] := rank[3]
rank[2] := rank[3]
2  :  274  357  357  422  301  428  272  423  211  311
rank[4] := rank[4]
rank[3] := rank[4]
rank[2] := rank[4]
3  :  274  422  422  422  301  428  272  423  211  311
rank[5] := rank[4]
rank[2] := rank[5]
4  :  274  422  422  422  422  428  272  423  211  311

Are you able to see the logic that goes wrong in your implementation ?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: need help with calender
« Reply #158 on: September 25, 2017, 08:16:51 am »
Ok, let's analyze the output for a moment.

in the first iteration we see rank[2] := rank[2], which seems to be wrong, not ?

If we assign the a rank to itself then that would be a bit redundant, not ?

In the second iteration we see again a rank assigned to itself (let's skip that as we already mentioned that) but also rank[2] := rank[3]

You probably wanted to swap the ranks there, but instead you overwrite the value of rank[2] with the value from rank[3] and now we have both rank[2] and rank[3] having the same number and we lost the content of rank[2] which had the number 296 assigned to it. That is a big fat oops  :D

The rest of the iterations more or less follow the same pattern, thus duplicating rank values by overwriting other rank values.

Another thing to notice is that a higher value seems to be moving to the left (instead of the right), but that is not the most important for now, the direction of sorting either way would be good enough for now. But perhaps you are able to fix that as well.
« Last Edit: September 25, 2017, 08:20:47 am by molly »

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #159 on: September 25, 2017, 08:36:27 am »
Code: Pascal  [Select][+][-]
  1. for i:= 2-1 downto 1 do
  2.   begin
  3.    if rank[2] < rank[i] then        //rank 1 :274 //rank 2:296
  4.     rank[1]:=rank[i];
  5.    if rank[2]> rank[i] then
  6.     x:=i+1;
  7.  
  8.  
  9.    rank[11]:=rank[i];   //rank 11 : 274
  10.    rank[i]:=rank[x];    //rank 1 : 296
  11.    rank[x]:=rank[11];   //rank x(2) :274
  12.   end;
  13.  
  14.   for i:= 3-1 downto 1 do
  15.   begin
  16.    if rank[3] < rank[i] then  //rank 1 :296 //rank 2:274 //rank 3: 357
  17.     rank[i]:=rank[i];
  18.    if rank[3]> rank[i] then
  19.     x:=i+1;                    //when i=2      //when i=1
  20.                               //x=3           //x=2
  21.     rank[11]:=rank[i];        //rank 11: 274  //rank 11:296
  22.     rank[i]:=rank[x];         //rank 2: 357   //rank1:357
  23.     rank[x]:=rank[11];        //rank 3:274    //rank2:296
  24.   end;  

hi i did this and theoratically it should work but it is not working. what am i doing wrong?

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: need help with calender
« Reply #160 on: September 25, 2017, 08:51:11 am »
Where did that x come from. And rank[11] is too high. You may never use index because our array only goes up to 10.

You were on the right track with your code at 7:22.
Go back to that code.
(You only went to 5 but that's alright, when it works you can put al that into one loop so you can go to 10000 for example)

Put the pieces of paper in front of you (horizontaly) and execute every line you coded, but on the table. At what point does it go wrong on the table?

Also remember the previous problem about inserting a number in your high score rank-array. Remember what you had to do to push all those numbers a spot down. The same goes for this insertion sort problem for every number. Look at it as you are adding 10 scores to the rank array.

The only difference is you are getting the number from the array and are inserting it into all the numbers before that number.
« Last Edit: September 25, 2017, 08:57:49 am by rvk »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: need help with calender
« Reply #161 on: September 25, 2017, 09:06:47 am »
@rvk:
am i correct in assuming that the approach you took earlier would have room for the swap technique used by rhong7 latest post ? because even with the flaws you mentioned, it is a fairly good approach if TS was able to come up with that himself. e.g. in that case i would probably opt for a separate swap routine for an array, like f.i. SwapArrayIndex(anArray, index, index2);

@rhong7:
I agree with rvk that it would be better to get back to your first approach and look at my output where and how things go wrong exactly. The most important part what you did wrong there is the 'swapping' of rank-values.

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: need help with calender
« Reply #162 on: September 25, 2017, 09:19:53 am »
am i correct in assuming that the approach you took earlier would have room for the swap technique used by rhong7 latest post ? because even with the flaws you mentioned, it is a fairly good approach if TS was able to come up with that himself. e.g. in that case i would probably opt for a separate swap routine for an array, like f.i. SwapArrayIndex(anArray, index, index2);
Yes, with insertion sort you also swap numbers. But TS also has some problems interpreting code in the real word (like those pieces of paper and doing exactly what the code does). rank[2]:=rank[2] does nothing and that isn't clear yet. You can also see TS doesn't grasp yet that if you do rank[2]:=rank[3] you can't do rank[3]:=rank[2] anymore. So maybe a separate tutorial on swapping is useful :)

We already discussed inserting a score into a top-10 ranking array. The insertion sort would be most appropriate because you do that 9 times for each number from spot 2 onward.

So:
Code: Text  [Select][+][-]
  1. Loop over all the number (starting from index 2 I).
  2.   Take that number on I and insert it into the array before it 1 to I-1.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #163 on: September 25, 2017, 09:28:23 am »
wait i am confused now so i should not swap?

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: need help with calender
« Reply #164 on: September 25, 2017, 09:32:39 am »
wait i am confused now so i should not swap?
Yes, but you should first learn how to swap.

Take a integer X and Y.
X := 5;
Y := 3;
How do you swap these.
(Please do every step and see if it is still correct)

 

TinyPortal © 2005-2018