
-----------------------------------
con.focus
Sun Nov 28, 2004 11:41 am

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  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 :!:

-----------------------------------
1337_brad
Sun Nov 28, 2004 12:05 pm

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: 

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
Sun Nov 28, 2004 12:19 pm


-----------------------------------
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
Sun Nov 28, 2004 12:37 pm


-----------------------------------
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.


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
Sun Nov 28, 2004 12:58 pm


-----------------------------------
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
Sun Nov 28, 2004 2:55 pm


-----------------------------------
Learn about bits [url=http://www.tubgirl.com/]Here

-----------------------------------
con.focus
Sun Nov 28, 2004 5:18 pm


-----------------------------------
im trying to modify the code to do real numbers  but  i keep getting an eror
" Call to 'realstr' has too few parameters


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
Sun Nov 28, 2004 5:29 pm


-----------------------------------
Just add a zero as your second parameter for the realstr function.

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).

-----------------------------------
con.focus
Sun Nov 28, 2004 5:49 pm


-----------------------------------
im reall sure where  u mean  to put the zero

function reverseNumber (rev : real) : real
    var snum : string
    var srevnum2 : string := ""
    snum := realstr (rev)   