Recent

Author Topic: Help please  (Read 13131 times)

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Help please
« Reply #15 on: September 22, 2014, 08:31:52 am »
I'm curious.

Why using setlengh(aux,y) if aux is already a pointed string?
And why not using
Code: [Select]
for i:= y downto 1  do 
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Help please
« Reply #16 on: September 22, 2014, 08:35:24 am »
I'm curious.

Why using setlengh(aux,y) if aux is already a pointed string?
And why not using
Code: [Select]
for i:= y downto 1  do 

high schooler alert let him understand first.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Derian

  • New Member
  • *
  • Posts: 20
Re: Help please
« Reply #17 on: September 22, 2014, 08:45:45 am »
i have another question :
program project4;
var num,c,cont:byte;

 begin
   writeln('entry number');
   cont:=0;

   Repeat
   readln(num);
   c:=num div 2 ;

   inc(cont);
    readln(c);
   until c=0;
   writeln('number of cycles are ',cont);
end.

theres no error ,but i type a number and it just stops there.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Help please
« Reply #18 on: September 22, 2014, 08:53:54 am »
i have another question :
program project4;
var num,c,cont:byte;

 begin
   writeln('entry number');
   cont:=0;

   Repeat
   readln(num);
   c:=num div 2 ;

   inc(cont);
    readln(c);
   until c=0;
   writeln('number of cycles are ',cont);
end.

theres no error ,but i type a number and it just stops there.

you ask from the user to type a number twice once in the start of the loop ee readln(num); and then 3 commands down you ask for an other number ee  readln(c); I assume that this one should have been writeln instead?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Derian

  • New Member
  • *
  • Posts: 20
Re: Help please
« Reply #19 on: September 22, 2014, 08:58:45 am »
if i have any number l, i want to give it a dimensión in terms of 2^x , i want to know how much is x
like 17 < 2^5

i try to do this using :
x:=0
17 div 2 = 8 inc(x)
8 div 2 = 4 inc(x)
4 div 2 = 2 inc(x)
2 div 2 = 1  inc(x)
1 div 2 = 0 inc(x)

then cont := 5

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Help please
« Reply #20 on: September 22, 2014, 09:25:30 am »
then you need to move the readln(num) before the Repeat line to make sure that you read once and ony once and remove the second readln(c) line completely.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Derian

  • New Member
  • *
  • Posts: 20
Re: Help please
« Reply #21 on: September 22, 2014, 09:29:55 am »
like this: ??
[ program project4;
var num,c,cont:byte;

 begin
   writeln('entry number');
    readln(num);
   cont:=0;

   Repeat

   c:=num div 2 ;

   inc(cont);

   until c=0;
   writeln('number of cycles are ',cont);
end.    ]
 but it still doesnt work , it just asks for the number and stops there.                   
« Last Edit: September 22, 2014, 09:40:03 am by Derian »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Help please
« Reply #22 on: September 22, 2014, 09:33:07 am »
FYI: Surrounding your code with [ code ] and [ /code ]tags (without the spaces helps in reading/copying..
Code: [Select]
program project4;
var num,c,cont:byte;

 begin
   writeln('entry number');
    readln(num);
   cont:=0;

   Repeat

   c:=num div 2 ;

   inc(cont);

   until c=0;
   writeln('number of cycles are ',cont);
end.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Help please
« Reply #23 on: September 22, 2014, 09:45:08 am »
like this: ??
program project4;
var num,c,cont:byte;

 begin
   writeln('entry number');
    readln(num);
   cont:=0;

   Repeat

   c:=num div 2 ;

   inc(cont);

   until c=0;
   writeln('number of cycles are ',cont);
end.
 but it still doesnt work , it just asks for the number and stops there.                 
Well lets run it and see what happens
prints on the screen the phrase "entry number" and waits for you to enter a number
you type the number 7 and press enter
assigned the value of 0 to the variable Cont;
starts the loop
calculates the result of Num div 2 and assigns it to the variable C in this case num equals 7 so C becomes 3
increases the value of cont by 1 so now cont has a value of 1;
checks if c equals 0, C has the of 3 as calculated earlier.
jumps back to repeat line for the iteration.

calculates the result of Num div 2 and assigns it to the variable C in this case num equals 7 so C becomes 3
increases the value of cont by 1 so now cont has a value of 2;
checks if c equals 0, C has the of 3 as calculated earlier.
jumps back to repeat line for the iteration.

calculates the result of Num div 2 and assigns it to the variable C in this case num equals 7 so C becomes 3
increases the value of cont by 1 so now cont has a value of 3;
checks if c equals 0, C has the of 3 as calculated earlier.
jumps back to repeat line for the iteration.

calculates the result of Num div 2 and assigns it to the variable C in this case num equals 7 so C becomes 3
increases the value of cont by 1 so now cont has a value of 4;
checks if c equals 0, C has the of 3 as calculated earlier.
jumps back to repeat line for the iteration.

calculates the result of Num div 2 and assigns it to the variable C in this case num equals 7 so C becomes 3
increases the value of cont by 1 so now cont has a value of 5;
checks if c equals 0, C has the of 3 as calculated earlier.
jumps back to repeat line for the iteration.
........

well you get the point I guess. since num is always 7 C wil always be 3 and it will never reach 0 and you have fallen for the most common logical error in programming called infinite loop.
I'm going to give you the chance of coming up with an alternative method to solve the problem if you can't find a solution then ask again presenting the various way to tried to solve the problem and I'll post the answer.

In the mean time here is a hint you need to use the last calculated value from X div 2 instead of always the input value.
« Last Edit: September 22, 2014, 09:50:49 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Derian

  • New Member
  • *
  • Posts: 20
Re: Help please
« Reply #24 on: September 22, 2014, 09:52:14 am »
well i tried with this :
[program project4;
var num,c,cont:byte;

 begin
   writeln('entry number');
    readln(num);
   cont:=0;

   Repeat

   c:=num div 2 ;
   num:=c;
   inc(cont);

   until c=0;
   writeln('number of cycles are ',cont);
end.  ]
and it Works , thank you .
                                   

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Help please
« Reply #25 on: September 22, 2014, 09:54:02 am »
congrats.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Derian

  • New Member
  • *
  • Posts: 20
Re: Help please
« Reply #26 on: September 22, 2014, 12:43:09 pm »
well i got another problem:
i want a program which gives me the sum of digits of any number(byte type) but in binary form:
e.g if i type 87(which is 01010111) i have to get 0+1+0+1+0+1+1+1=5

ive done this program to get the sum of digits of any number , but i cant imagine a program to convert a number to their binary form

Code: [Select]
[/program project5;
var num,suma,y,code,ff,i,code1:integer;
  ye,c :string;

begin
    writeln('entry number');
    readln(num);
    str(num,ye);
    suma:=0;
    y:=length(ye);
    repeat
    c:=ye[i];
    inc(i);
    val(c,ff,code1);
    suma:=suma+ff;
    until i=y+1 ;
    writeln('la suma de digitos es ',suma);
  end.                               code]
« Last Edit: September 22, 2014, 01:10:29 pm by Derian »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Help please
« Reply #27 on: September 22, 2014, 12:57:41 pm »
Why don't you start a new thread and surround your code with tags? (The latter is actually important as the forum software will mangle some combination of character making it difficult to reconstruct the code)

Makes it easier for people to help you with your latest assignment...

Finally, have you looked through the wiki and documentation? There's a lot out there that could help you...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Help please
« Reply #28 on: September 22, 2014, 01:45:19 pm »
I'm curious.

Why using setlengh(aux,y) if aux is already a pointed string?
And why not using
Code: [Select]
for i:= y downto 1  do 

Because the original instruction uses string with indexed position.

Something like "aux := aux + ..." doesn't require the setlength instruction,
while "aux[ i ]  := ..." does.

Anyway, I agree there is a lot of thing to say about this code, from the "educational" point of view.

Well, we all have been beginners one day ...

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Help please
« Reply #29 on: September 22, 2014, 04:44:10 pm »
well i got another problem:
i want a program which gives me the sum of digits of any number(byte type) but in binary form:
e.g if i type 87(which is 01010111) i have to get 0+1+0+1+0+1+1+1=5

ive done this program to get the sum of digits of any number , but i cant imagine a program to convert a number to their binary form

Code: [Select]
[/program project5;
var num,suma,y,code,ff,i,code1:integer;
  ye,c :string;

begin
    writeln('entry number');
    readln(num);
    str(num,ye);
    suma:=0;
    y:=length(ye);
    repeat
    c:=ye[i];
    inc(i);
    val(c,ff,code1);
    suma:=suma+ff;
    until i=y+1 ;
    writeln('la suma de digitos es ',suma);
  end.                               code]
How about forget the sum for a bit since this is something easy lets first convert a decimal number to binary. What would you do on paper?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018