Adding digits in a number
Author |
Message |
uberwalla

|
Posted: Sat Dec 16, 2006 1:18 am Post subject: Adding digits in a number |
|
|
i do no have a big problem but i just wanted to see if there was a way i could make this code better. i made this for an assignment in tech. it is supposed to add the digits of a number together. u was just wondering if there is an easier way to make this? without using all the instr and strint's and stuff lol
here is my code:
code: | %The "AddDigits" Program
%By: Mike Lanthier
%November 29, 2006
var num : int
var YesOrNo : string
var winID := Window.Open ("nobuttonbar")
fcn AddNumber (number : int) : int
var n1, n2, n3, t : int
var IntToString, IntToString1, IntToString2, IntToString3 : string
IntToString := intstr (number)
IntToString1 := IntToString (1)
IntToString2 := IntToString (2)
IntToString3 := IntToString (3)
n1 := strint (IntToString1)
n2 := strint (IntToString2)
n3 := strint (IntToString3)
t := n1 + n2 + n3
result t
end AddNumber
loop
put "Please Enter A Three Digit Number And I Will Add The Seperate Digits Together."
get num
if num >= 1000 or num <= 99 then
put "Error. That Is Not A Three Digit Number."
else
put AddNumber (num)
end if
put "\nAdd Another Number? (Yes (y) Or No (n)?)"
get YesOrNo
exit when YesOrNo = "n"
cls
end loop
Window.Close (winID)
|
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Clayton

|
Posted: Sat Dec 16, 2006 7:46 am Post subject: (No subject) |
|
|
the way you have your function right now, it's not very dynamic. What If I don't want to input just a three digit number? Instead use a for loop to circle through all of the digits and add them, or, dare I say it, use recursion. |
|
|
|
|
 |
uberwalla

|
Posted: Sat Dec 16, 2006 6:38 pm Post subject: (No subject) |
|
|
ok thatll probably work. its just that the assignment was mainly for 3 digit number. ill try that. itll prob work better with arrays too im guessing right?  |
|
|
|
|
 |
Clayton

|
Posted: Sat Dec 16, 2006 6:46 pm Post subject: (No subject) |
|
|
not necessarily, a function with a for loop would be a better thing here. |
|
|
|
|
 |
StealthArcher

|
Posted: Sat Dec 16, 2006 9:41 pm Post subject: (No subject) |
|
|
This seems like it will work nicely, you can get any size number to work.
code: |
%The "AddDigits" Program
%By: Mike Lanthier
%November 29, 2006
var num : int
var total:int:=0
var YesOrNo : string
var winID := Window.Open ("nobuttonbar")
fcn AddNumber (number : int) : int
var n1, n2, n3, t : int
var IntToString,ITS : string
IntToString := intstr (number)
for x:1..(length(IntToString))
ITS := IntToString (x)
n1 := strint (ITS)
total:= total+n1
end for
result total
end AddNumber
loop
put "Please Enter A Three Digit Number And I Will Add The Seperate Digits Together."
get num
put AddNumber (num)
put "\nAdd Another Number? (Yes (y) Or No (n)?)"
get YesOrNo
exit when YesOrNo = "n"
cls
end loop
Window.Close (winID)
|
|
|
|
|
|
 |
uberwalla

|
Posted: Sat Dec 16, 2006 10:22 pm Post subject: (No subject) |
|
|
o ic thx man i understand what freakman was getting at thx u both. when i was thinking arrays i think i was more thinking of storing each number seperate for some reason and maybe call them up one by one and add stuff and maybe make something of it. kinda a interesting speciman to try to make a quiz type game or something . anyways i got no idea what im saying, but thx guys  |
|
|
|
|
 |
ericfourfour
|
Posted: Sat Dec 16, 2006 10:32 pm Post subject: (No subject) |
|
|
uberwalla IntToString is not a very descriptive name. I would name it numString or numChars. Also, variables and methods usually start with a lower case in Turing. Types, classes and constants start with capitals. Refer to wtd's style guide in Turing tutorials for more information.
I am also sure there is a more "math" way to do this. |
|
|
|
|
 |
uberwalla

|
Posted: Sat Dec 16, 2006 10:48 pm Post subject: (No subject) |
|
|
yea i know i made this b4 i thought to look at his guideline again
im kinda lazy. anyways i kinda still like IntToString because to me it tells me its interger to string. but i get where your getting at. i just did what im used to . BAD ME BAD.
thx. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
|
|