Adding within :?:
Author |
Message |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: Sat Feb 19, 2005 10:10 pm Post subject: Adding within :?: |
|
|
i know the topic doesnt explain a lot but i will try my best to describe the problem, um, supposedly, i have
code: |
var amount : int
get amount
var numbers : array 1..amount of int
for s : 1..amount
get numbers(s)
end for
|
Now lets say i chose 5 as the amount and i enter following values ->
1, 2, 3, 4, 5
Now, supose there is an answer variable which is 9. How do i get it to calculate all the possible additions that are:
1 + 2 + 3 + 4 + 5 = 15
1 + 2 = 3
1 = 1
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
2 + 3 + 4 + 5 = 14
2 + 3 + 4 = 9
2 + 3 = 5
2 = 2
So on and so forth with 3, 4 and 5. After doing that i want it to pick the equal or closest value to "answer" and state that addition sum. Is there a way to do it?
![Question Question](images/smiles/icon_question.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
ssr
![](http://siruisite.port5.com/ssr.jpg)
|
Posted: Sun Feb 20, 2005 8:37 am Post subject: (No subject) |
|
|
do a loop
and change the array number each time
8) [/code] |
|
|
|
|
![](images/spacer.gif) |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: Sun Feb 20, 2005 11:31 am Post subject: (No subject) |
|
|
ok i knew that but how do i do it if i dont know the amount of numbers the user has input :S |
|
|
|
|
![](images/spacer.gif) |
Bacchus
![](http://img104.exs.cx/img104/9206/ravenmoon12ns.jpg)
|
Posted: Sun Feb 20, 2005 8:02 pm Post subject: (No subject) |
|
|
yes you do, you have the user input the amount in the very first part of the program.. its even under the variable amount.. personally in would use nested for loops |
|
|
|
|
![](images/spacer.gif) |
StarGateSG-1
![](http://img61.imageshack.us/img61/4417/jesushulk2sq.gif)
|
Posted: Mon Feb 21, 2005 12:26 pm Post subject: Arrays Suck |
|
|
arrays are not a good thing , you need to use a vector with turing doesn't support or i have never found one. you just have to ask the user for input for now. |
|
|
|
|
![](images/spacer.gif) |
AsianSensation
|
Posted: Mon Feb 21, 2005 4:28 pm Post subject: (No subject) |
|
|
true, Turing doesn't have vectors, but you can use the flexible arrays, which are just dynamic arrays.
There is a tutorial floating around in the tutorial section. Check it out, it will probably solve what you need to do. |
|
|
|
|
![](images/spacer.gif) |
zylum
![](http://compsci.ca/v3/uploads/user_avatars/1689129126468091c334ee0.gif)
|
Posted: Mon Feb 21, 2005 4:55 pm Post subject: (No subject) |
|
|
i dont think his problem is with arrays, i think he wants to find the combination of numbers that will add up as closely as possible to the given number. in this case i would go for recursion if the amount of numbers is small... or you could do this:
code: | %get numbers
var nums : flexible array 1 .. 0 of int
var s : int
put "enter numbers. end with int <= 0"
loop
get s
exit when s <= 0
new nums, upper (nums) + 1
nums (upper (nums)) := s
end loop
put "enter sum"
get s
%main proggy
var bin : string
var sum : int := 0
var temp : int
var sumStr : string
for i : 1 .. 2 ** upper (nums) - 1
bin := intstr (i, upper (nums), 2)
temp := 0
for j : 1 .. length (bin)
if bin (j) = '1' then
temp += nums (j)
end if
end for
if abs (s - temp) < abs (s - sum) then
sum := temp
sumStr := ""
for j : 1 .. length (bin)
if bin (j) = '1' then
sumStr += intstr (nums (j)) + " + "
end if
end for
end if
end for
%output answer
sumStr := sumStr (1 .. * -2)
cls
put "closest sum is: ", sum
put sumStr |
using binary ![Wink Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif) |
|
|
|
|
![](images/spacer.gif) |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: Fri Feb 25, 2005 9:02 pm Post subject: (No subject) |
|
|
mind explaining an overview of the code?? thanks! |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
|
|