PDA

View Full Version : Pascal Trouble...


The Fifth Horseman
04-05-2005, 03:30 PM
I got an example task like those that might appear during the exams next week and I have certain trouble...

program wykladniki;
uses crt;
var a:longint; n:longint; wynik:longint; x:longint; k:longint;

begin
clrscr;
writeln('Podaj podstawę potęgi:');
readln(a);
writeln('Podaj wykładnik:');
readln(n);
wynik:=1;
x:=a;
k:=n;
readkey;
repeat
begin
* if Odd(k) then
* begin
* wynik:= wynik * x;
* k:= k - 1;
* end
* else
* begin
* k:= k / 2 ;
* x:= x * x ;
* end;
writeln('' ,wynik);
end
until k>0;
end.

Odd(k) requires k to be of an integer type. On the other hand, k:= k / 2 requires the k to be of real type.
In theory I could try something with extra variables and stuff, but in this task I am limited to pre-defined variables with no space for this...
:wall:
I ran outta ideas so far... got any?

Kon-Tiki
04-05-2005, 03:44 PM
I don't know if it's possible in Pascal, but that's usually what Typecasting's for.

Danny252
04-05-2005, 03:55 PM
what is that all meant to do? :blink:

The Fifth Horseman
04-05-2005, 04:09 PM
I don't know if it's possible in Pascal, but that's usually what Typecasting's for.
I don't quite understand...

what is that all meant to do?
It's a program done in Pascal programming language. Read it a few times and you will see how it works. As to what is the purpose - training. Similar tasks can be expected on the exams and I _must_not_fail_. :ranting:

Danny252
04-05-2005, 04:19 PM
must they all be longints?
since why not just use a and n?

Kon-Tiki
04-05-2005, 04:28 PM
Typecasting (http://en.wikipedia.org/wiki/Typecasting_(programming))

NrmMyth
04-05-2005, 05:34 PM
What can you edit in your code??

If this can be done try this:
K:=K DIV 2;

DIV is an operator that deals with integer division, so K will stay int.

The Fifth Horseman
05-05-2005, 06:06 AM
What can you edit in your code??

Everything. You see, I got the task with a step-by-step listing of needed operations, but I had to code it by myself.

If this can be done try this:
K:=K DIV 2;

It worked! Thanks! :w00t:

NrmMyth
05-05-2005, 03:02 PM
Originally posted by the_fifth_horseman@May 5 2005, 08:06 AM
What can you edit in your code??

Everything. You see, I got the task with a step-by-step listing of needed operations, but I had to code it by myself.

If this can be done try this:
K:=K DIV 2;

It worked! Thanks! :w00t:
NP, i'm learning Pascal at school, but really like C/C++. :cheers:

EDIT: I just remembered that you could use round(x:real):integer for rounding that numbers correctly.