Go Back   Forums > Community Chatterbox > Tech Corner > Programming
Memberlist Forum Rules Today's Posts
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
Old 04-05-2005, 03:30 PM   #1
The Fifth Horseman
FUTURE SCIENCE BASTARD
 
The Fifth Horseman's Avatar


 
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
Default

I got an example task like those that might appear during the exams next week and I have certain trouble...

Quote:
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...

I ran outta ideas so far... got any?
__________________

"God. Can't you people see I'm trying to commit a crime against science and nature here?"
-- Reed Richards
The Fifth Horseman is offline                         Send a private message to The Fifth Horseman
Reply With Quote
Old 04-05-2005, 03:44 PM   #2
Kon-Tiki
[BANNED]

 
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
Default

I don't know if it's possible in Pascal, but that's usually what Typecasting's for.
Kon-Tiki is offline                         Send a private message to Kon-Tiki
Reply With Quote
Old 04-05-2005, 03:55 PM   #3
Danny252
I have a custom title!

 
Join Date: Oct 2003
Location: Telford, England
Posts: 1,303
Default

what is that all meant to do? :blink:
__________________
I liked the old forum.. =/
Danny252 is offline                         Send a private message to Danny252
Reply With Quote
Old 04-05-2005, 04:09 PM   #4
The Fifth Horseman
FUTURE SCIENCE BASTARD
 
The Fifth Horseman's Avatar


 
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
Default

Quote:
I don't know if it's possible in Pascal, but that's usually what Typecasting's for.
I don't quite understand...

Quote:
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:
__________________

"God. Can't you people see I'm trying to commit a crime against science and nature here?"
-- Reed Richards
The Fifth Horseman is offline                         Send a private message to The Fifth Horseman
Reply With Quote
Old 04-05-2005, 04:19 PM   #5
Danny252
I have a custom title!

 
Join Date: Oct 2003
Location: Telford, England
Posts: 1,303
Default

must they all be longints?
since why not just use a and n?
__________________
I liked the old forum.. =/
Danny252 is offline                         Send a private message to Danny252
Reply With Quote
Old 04-05-2005, 04:28 PM   #6
Kon-Tiki
[BANNED]

 
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
Default

Typecasting
Kon-Tiki is offline                         Send a private message to Kon-Tiki
Reply With Quote
Old 04-05-2005, 05:34 PM   #7
NrmMyth
Hero Gamer

 
Join Date: Jan 2005
Location: ,
Posts: 454
Default

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.
__________________
Never mess with me when I have a cougar, Never!
NrmMyth is offline                         Send a private message to NrmMyth
Reply With Quote
Old 05-05-2005, 06:06 AM   #8
The Fifth Horseman
FUTURE SCIENCE BASTARD
 
The Fifth Horseman's Avatar


 
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
Default

Quote:
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.

Quote:
If this can be done try this:
K:=K DIV 2;
It worked! Thanks! :w00t:
__________________

"God. Can't you people see I'm trying to commit a crime against science and nature here?"
-- Reed Richards
The Fifth Horseman is offline                         Send a private message to The Fifth Horseman
Reply With Quote
Old 05-05-2005, 03:02 PM   #9
NrmMyth
Hero Gamer

 
Join Date: Jan 2005
Location: ,
Posts: 454
Default

Quote:
Originally posted by the_fifth_horseman@May 5 2005, 08:06 AM
Quote:
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.

Quote:
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++.

EDIT: I just remembered that you could use round(x:real):integer for rounding that numbers correctly.
__________________
Never mess with me when I have a cougar, Never!
NrmMyth is offline                         Send a private message to NrmMyth
Reply With Quote
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Log-in trouble MM_not_logged_in_sorta Old Suggestions 3 30-06-2009 10:37 AM
Trouble i need help please IshaMushaHaya General compatibility fixes 3 02-07-2008 11:16 AM
Trouble dynamite1 Troubleshooting 8 11-04-2006 07:39 AM
Need To Learn Turbo Pascal 7... Fast! The Fifth Horseman Programming 17 11-03-2005 02:02 PM
Trouble With Wc Darren Lauck Troubleshooting 3 03-03-2005 02:41 PM


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 05:51 AM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.