Recent

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

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #165 on: September 25, 2017, 09:43:34 am »
Code: Pascal  [Select][+][-]
  1. program practice;
  2. uses
  3. sysutils;
  4. var
  5.   x, y, swap:integer;
  6.  
  7. begin
  8.   x:=5;
  9.   y:=3;
  10.  
  11.   swap:=x; //swap:5
  12.   x:=y; //x=3
  13.   y:=swap; //y=5
  14.  
  15.   writeln('x is', x);
  16.   writeln('y is', y);
  17.   readln;
  18.  
  19. end.    

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: need help with calender
« Reply #166 on: September 25, 2017, 09:58:21 am »
Ah, drats rhong7   ;)

I've been working on a nice tutorial on swapping and now you already are doing it just fine all by yourself  :D

Do you understand how to use that same approach for swapping values inside an array ?

rvk

  • Hero Member
  • *****
  • Posts: 6109
Re: need help with calender
« Reply #167 on: September 25, 2017, 10:20:36 am »
Ok, now that's out of the way... No, you don't actually need real swapping for insertion sort :D
(sorry molly  :D)

You do need the helper variable to take one value out of the array and insert it somewhere else. You need that variable because when inserting the spot where you took it from is going to be overwritten.

Loop at this:

1 2 4 5 3
Now you want 3 to go to the third spot.
So you take out 3 (put it in a helper variable just as in highscore) and get this
1 2 4 5 -
(- is an empty spot)
Now you are going to insert the three at spot 3. What do you do?
You move 4 and 5 from index 3 and 4 to index 4 and 5 like this
(Remember you had trouble because you first tried to put 4 on the spot of 5 and 5 was erased? so you needed to do this backwards)
1 2 - 4 5
and put 3 in the - spot.

Does this look familiar?
It's exactly the same as your highscore problem.

With insertion sort you take each number in the array
and try to insert it into the array BEFORE it.
So the 6th number... put it into a helper variable... now you will try to insert that into the array 1..6 (yes if it doesn't fit in 1..5 you need to put it on 6)
Then you take the 7th number... put it into a helper variable... now you try to insert it into array 1..7.
Etc. etc.

Remember that line 3&4 from this post for determining in what spot you want a number in your highscore-array?

« Last Edit: September 25, 2017, 10:22:53 am by rvk »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: need help with calender
« Reply #168 on: September 25, 2017, 10:35:50 am »
Ok, now that's out of the way... No, you don't actually need real swapping for insertion sort :D
(sorry molly  :D)
:P (all good  ;))

But on a more serious note: i wasn't aware you already discussed this implementation (the thread was a bit too long to get over each individual post), so that would be a very good starting point for TS.

PS: i'll post TS' corrected code after your approach rvk, because in principle that code from TS had the potential for finding a solution to the problem. There were only two problems: the swapping and doing the iterations in the wrong order (not counting the unnecessary checks and 'wrong swaps' that followed). Things can always be done in a better and/or more efficient way.
« Last Edit: September 25, 2017, 11:09:28 am by molly »

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #169 on: September 25, 2017, 11:39:58 am »
Code: Pascal  [Select][+][-]
  1. for i:= 5-1 downto 1 do    //rank 1: 274 rank 2: 296 rank 3: 357
  2.   begin
  3.    if rank[5]> rank[i] then
  4.     h:=rank[5];
  5.  
  6.     rank[i+1]:=rank[i];
  7.    if rank[5]< rank[i] then
  8.     rank[i]:=h;
  9.   end;

hi i did this but it is still not working

rvk

  • Hero Member
  • *****
  • Posts: 6109
Re: need help with calender
« Reply #170 on: September 25, 2017, 11:43:51 am »
hi i did this but it is still not working
You are trying to do too much in one loop.
What exactly were you trying to do here?

Was this to insert the number from spot 5 into the array [1..5] ?
If so... then first please write out in English what and how you are going to do this.
Like...
*) Take the number from spot 5 and set it in a variable h
*) what next...? remember what you needed to do with the highscore problem first.
« Last Edit: September 25, 2017, 11:49:28 am by rvk »

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #171 on: September 25, 2017, 11:59:27 am »
*) Take the number from spot 5 and set it in a variable h
*)if spot 5 is bigger than spot betwen 1 to 4, then spot +1
*)if spot 5 is smaller than x spot,  then assign spot 5 value to x+1 spot

rvk

  • Hero Member
  • *****
  • Posts: 6109
Re: need help with calender
« Reply #172 on: September 25, 2017, 12:28:51 pm »
*)if spot 5 is bigger than spot betwen 1 to 4, then spot +1
Not sure what you mean by this line.

But the next step is:
*) take the number from spot 5 and set it in a variable h
*) --- we first need to find the correct spot to insert h into the array before spot 5 (so in spot 1..4)
*) -------- we do this by looping 1..4 with Q (with a while loop, see line 3&4 in AddRank() )
*) --------------- if h > number at Q then break out the loop (remember line 3&4 in mentioned code before?)
*) --- if Q < 5 then we need to move everything from index Q..4 to Q+1..5 (remember how to do that, hint: in reverse !!)
*) --- ... if Q was 5 we can just go on
*) --- put h in the spot of index Q
*) take the number from spot 6 and set it in a variable h
*) etc.

(Please take the function AddRank we made earlier as reference to put a number in a top 10 array. Because that top 10 array is also sorted)

Try to follow these steps with the pieces of paper on your table.
Does it work ?

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #173 on: September 25, 2017, 12:41:31 pm »
Code: Pascal  [Select][+][-]
  1.  h:=rank[5];
  2.    q:=1;
  3.    while (q<5) and (rank[q]< h) do inc(q);
  4.    if q< 5 then
  5.    begin
  6.      for i:=5-1 downto 1 do
  7.        begin
  8.         rank[i+1]:=rank[i];
  9.        end;
  10.    end;
  11.     rank[q]:=h;  

like that?

rvk

  • Hero Member
  • *****
  • Posts: 6109
Re: need help with calender
« Reply #174 on: September 25, 2017, 12:52:35 pm »
like that?
Yes, that's almost perfect.

Except for one little small thing.
Look at line 6. What are you doing there?

What text in the pseudo code I gave does line 6 represent?
(you are moving spot 1..4 to spot 2..5 there in code)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #175 on: September 25, 2017, 01:16:12 pm »
Code: Pascal  [Select][+][-]
  1.  for i:=5-1 downto q do  
is it this?

rvk

  • Hero Member
  • *****
  • Posts: 6109
Re: need help with calender
« Reply #176 on: September 25, 2017, 01:23:50 pm »
Code: Pascal  [Select][+][-]
  1.  for i:=5-1 downto q do  
is it this?
Yep.
And now wrap this around the complete loop for all the numbers.
(remember... that code/project in where you generated the random numbers)

You can begin at number 2 because if you have number 1 you always know that it is the highest number in the array 1..1.

You can change everything which is 5 in your code to the loop-variable.
The loop variable can't be I because you already used that inside the code you already have so you need to use another variable.

It best to print the numbers to screen first directly after generating them (just a small for / print loop).
Then print a '--------'
Then do your sorting-loop
Then print the sorted numbers again.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #177 on: September 25, 2017, 01:28:13 pm »
hi thank you it works very well
but i have a question
why couldn't i use if function?

rvk

  • Hero Member
  • *****
  • Posts: 6109
Re: need help with calender
« Reply #178 on: September 25, 2017, 01:54:12 pm »
but i have a question
why couldn't i use if function?
Where?

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #179 on: September 25, 2017, 02:06:33 pm »
instead of while

 

TinyPortal © 2005-2018