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

Username:   Password: 
 RegisterRegister   
 Adding within :?:
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MysticVegeta




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
ssr




PostPosted: Sun Feb 20, 2005 8:37 am   Post subject: (No subject)

do a
code:
for
loop
and change the array number each time
8) [/code]
MysticVegeta




PostPosted: 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
Bacchus




PostPosted: 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
StarGateSG-1




PostPosted: 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.
AsianSensation




PostPosted: 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.
zylum




PostPosted: 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
MysticVegeta




PostPosted: Fri Feb 25, 2005 9:02 pm   Post subject: (No subject)

mind explaining an overview of the code?? thanks!
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: