Recent

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

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #135 on: September 24, 2017, 03:47:52 pm »
where do i print that number?
With a writeln(remembered_number); just like you did inside the loop with the generation of the random numbers.
You just first need to determine what that remembered_number is.

(you can also call it highest_number or something you like)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #136 on: September 24, 2017, 03:49:52 pm »
Code: Pascal  [Select][+][-]
  1. program project1;
  2. uses sysutils;
  3. var
  4.   i:integer;
  5.   randomm: array [1..100] of integer;
  6.   highest_number:integer;
  7.  
  8. begin
  9.   highest_number:=0;
  10.   for i:=1 to 100 do
  11.   begin
  12.     randomm[i]:=random(500);
  13.     writeln(inttostr(randomm[i]));
  14.     if randomm[i]>highest_number then
  15.     begin
  16.       highest_number:=randomm[i];
  17.     end;
  18.   end;
  19.   writeln(highest_number);
  20.   readln;
  21. end.      

like that?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #137 on: September 24, 2017, 03:57:47 pm »
like that?
If you reduce the loop to just 10 numbers then you could see for yourself if it worked.

I would print out the last number with some text so you can see the difference between the generated numbers and the result for highest number.
Code: Pascal  [Select][+][-]
  1.   writeln('highest number is ', highest_number);

One remark:
You cheated a little :D I asked you te create the random numbers FIRST and THEN determine what the highest number is in the generated array. So you would need to to the if randomm[i]>highest_number in a separate for-loop below the first one. But the fact you combined them showed some thinking so that's good too :D

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #138 on: September 24, 2017, 04:02:46 pm »
ohh okay it works but everytime i compile it i get same random numbers and same high number how can get different random numbers?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #139 on: September 24, 2017, 04:04:49 pm »
See the example here:
https://www.freepascal.org/docs-html/rtl/system/random.html

You need to call Randomize; at the beginning of your program if you are going to use Random() and you want different numbers each time.
Code: Pascal  [Select][+][-]
  1. Randomize; { This way we generate a new sequence every time the program is run}

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #140 on: September 24, 2017, 04:08:30 pm »
Ohh okay thank you
btw what does readln do?
is it only used for preventing the program from closing?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #141 on: September 24, 2017, 04:11:41 pm »
btw what does readln do?
is it only used for preventing the program from closing?
In this case yes.

Readln(string_variable); can also be used to read input from the command line.

But in this case it is just that you can see that black console screen. Otherwise it closes directly after your code and you can't see any result.

If you would run this project1.exe on the command line itself (like cmd.exe) you wouldn't need that line. But from inside the Lazarus GUI you do need it (to see the result).

And you can just press enter to go back to the GUI/Lazarus.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #142 on: September 24, 2017, 04:14:33 pm »
oh okay and can i put those 10 numbers in order from highest to lowest?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #143 on: September 24, 2017, 04:20:40 pm »
oh okay and can i put those 10 numbers in order from highest to lowest?
Yes, that would be another exercise. Sorting an array.

First create in human/English language how you would go about sorting those 10 pieces of paper with numbers on them.

There are a lot of sorting algoritmes to find online but for this you would probably want the simplest one (that is also the simplest to program). But you would really think about how to do it on the table first with 10 spots and 10 pieces of paper.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #144 on: September 24, 2017, 04:27:32 pm »
create an array of 10 random numbers
find the 1st~10th
if the number is first, make it to randomm[1] (do it for 1st ~10th)
clear the writing(?)
writeln (randomm[1..10])

i dont know if that is right but i tried

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #145 on: September 24, 2017, 04:38:47 pm »
create an array of 10 random numbers
find the 1st~10th
if the number is first, make it to randomm[1] (do it for 1st ~10th)
clear the writing(?)
writeln (randomm[1..10])

i dont know if that is right but i tried
Yes. But finding 1st~10th isn't that easy. The highest or lowest isn't that difficult but the 2nd highest for example is a lot harder.

So you would need to use a strategy for that.
Just saying "find the 1st~10th" isn't enough because that would involve a lot of things you would need to do. You'll need to write out those things too.

Normally the simplest sort is insertion sort.
You would need to do something like in the image below.
(it's an animated image so you need to click it)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #146 on: September 24, 2017, 04:43:07 pm »
wow that seems really hard i will try to do it. btw how to delete the writing on the program?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #147 on: September 24, 2017, 04:49:24 pm »
btw how to delete the writing on the program?
What do you mean "how to delete the writing" ??

Do you mean how to erase something you already written with writeln() ?
That's not easy in a console screen.

You could just write it like you did in a GUI in Lazarus and put a TMemo on your screen and your code in a TButton.Click. The console examples I showed you were for experimenting with code you could find online. But for the purpose of training yourself you can use whatever method you find easiest.
 

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: need help with calender
« Reply #148 on: September 24, 2017, 05:16:06 pm »
Normally the simplest sort is insertion sort.
You would need to do something like in the image below.

A(n animated) picture is worth 1,000 words. Brilliant Rick!

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #149 on: September 25, 2017, 01:57:33 am »
can i still sort it out in order without erasing the writing?

 

TinyPortal © 2005-2018