Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help reverse number
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
con.focus




PostPosted: Sun Nov 28, 2004 11:41 am   Post subject: Help reverse number

i need to be able to in out a number and get the code to reverse in and output it on to the screen
code:
function reverseNumber (var n : int) : int
var inum1 : string
var inum2 : string := ""
put "type in a number: "..
get inum1
inum1 := intstr (n)
for decreasing i : length (inum1) .. 1
inum2 += inum1 (i)
end for
delay (500)
put " this is the reverse of your number"
put inum2
end reverseNumber



the only problem im having ( i think is outputting it on the screen , can somone tell me wut im doing wrong Exclamation
Sponsor
Sponsor
Sponsor
sponsor
1337_brad




PostPosted: Sun Nov 28, 2004 12:05 pm   Post subject: Well..

First of all, functions should not have put statements in them. A function is only for finding one value, (whether it be string, int, etc) but I think the code you want it:
code:

function revfunct(num: string): string
var newnum: string:= ""
for decreasing i: length(num).. 1
newnum :=newnum + num(i)
end for
result newnum
end revfunct


var num1: string
put "enter a number: "..
get num1
put "The reversed number is: ", revfunct(num1)
con.focus




PostPosted: Sun Nov 28, 2004 12:19 pm   Post subject: (No subject)

the only problem wit that is i need to convert it using a int , or real number

not a string i know wut i had but i use intstr(n) to convert it
Cervantes




PostPosted: Sun Nov 28, 2004 12:37 pm   Post subject: (No subject)

Here's a nice simple way to do it, for an integer. You can change it to have some error checking and also do for real numbers as well.

code:

function reverseNumber (n : int) : int
    var numStr : string
    var reversedNumStr : string := ""
    numStr := intstr (n)
    for decreasing r : length (numStr) .. 1
        reversedNumStr += numStr (r)
    end for
    result strint (reversedNumStr)
end reverseNumber

var num : int
put "Enter an integer"
get num
put "The num backwards is ", reverseNumber (num)
con.focus




PostPosted: Sun Nov 28, 2004 12:58 pm   Post subject: (No subject)

thx carvantes i didnt know you could do that i thought the fucntion would have to be with the reverse Number thx alot

btw wut are bits ne thing special like forum gold on different sites ???
Cervantes




PostPosted: Sun Nov 28, 2004 2:55 pm   Post subject: (No subject)

Learn about bits Here
con.focus




PostPosted: Sun Nov 28, 2004 5:18 pm   Post subject: (No subject)

im trying to modify the code to do real numbers but i keep getting an eror
" Call to 'realstr' has too few parameters

code:

function reverseNumber (rev : real) : real
    var snum : string
    var srevnum2 : string := ""
    snum := realstr (rev)
    for decreasing i : length (snum) .. 1
        srevnum2 += snum (i)
    end for
    result strreal (srevnum2)
end reverseNumber

% had trouble with this because function dont let you
% use the "put" statment  so i thought of by passing it
% by ending the function statement and putting the reverse number
% in the new statement

var rnum : real
put "Enter an number"
get rnum
put "The reverse of your number is: ", reverseNumber (rnum)




wut am i doing wrong
Cervantes




PostPosted: Sun Nov 28, 2004 5:29 pm   Post subject: (No subject)

Just add a zero as your second parameter for the realstr function.
The Turing Reference wrote:

Syntax realstr ( r : real, width : int ) : string

Description
The realstr function is used to convert a real number to a string. For example, realstr (2.5e1, 4)="bb25" where b represents a blank. The string is an approximation to r, padded on the left with blanks as necessary to a length of width.
The width parameter must be non-negative. If the width parameter is not large enough to represent the value of r it is implicitly increased as needed. The displayed value is rounded to the nearest decimal equivalent with this accuracy. In the case of a tie, the display value is rounded to the next larger value.

So, putting a 0 for your width will ensure that your all of your real number is changed to a string, but that it is not padded (if it was, you wouldn't be able to convert it back to a real number later).
Sponsor
Sponsor
Sponsor
sponsor
con.focus




PostPosted: Sun Nov 28, 2004 5:49 pm   Post subject: (No subject)

im reall sure where u mean to put the zero

code:
function reverseNumber (rev : real) : real
    var snum : string
    var srevnum2 : string := ""
    snum := realstr (rev)   <----  u mean in here or where point
    for decreasing i : length (snum) .. 1
        srevnum2 += snum (i)
    end for
    result strreal (srevnum2)
end reverseNumber
Cervantes




PostPosted: Sun Nov 28, 2004 5:54 pm   Post subject: (No subject)

Yep, you've got it Smile
code:
snum := realstr (rev,0)

Heh, you could have simply tried it to see what would happen. Wink
con.focus




PostPosted: Sun Nov 28, 2004 6:22 pm   Post subject: (No subject)

thx alot it not much but i giving u my bits
Cervantes




PostPosted: Sun Nov 28, 2004 7:37 pm   Post subject: (No subject)

Heh, I appreciate your gift, though, being a mod, my bits are locked at 1000. A warm thanks is all the gratification I need. Smile
con.focus




PostPosted: Sun Nov 28, 2004 9:17 pm   Post subject: (No subject)

o lol thx though
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: