Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Looking For Help With Algorithm That Processes Numbers

1. ## Looking For Help With Algorithm That Processes Numbers

I have a set of numbers within a text file.

2 6 4
1 4 4 1

When the program reads these numbers it will start with the first line. It reads 2, this is the number of times the program should loop through the numbers of the second line. Next it reads 6, this is the number that the sum of numbers on line 2 cannot go beyond, it must equal 6 or be less than. Finally the 4 is how many numbers need to be processed.

Next it goes to line 2 and processes the numbers, 2 is less than 6 so it gets processed, so is 4 and when added to 2 it equals 6. This passes the rules therefore gets processed and added together and put in a variable. Once 2 and 4 has been processed it gets put at the back of the number queue, so the numbers look like 1 1 2 4.

With one loop done there is only one left, so it processes the next sum of numbers 1 1 2 as these equal 4 and pass the rule. So these get added together and and put to the back of the queue, 4 1 1 2. But all the loops are finished and the sum of the last loop is added on to the sum of the previous loop. 6 + 4 = 10.

What I need the algorithm to do is print the sum of this whole process when it reads these numbers from a file. I am able to read the numbers from a text file but not sure where to proceed from there.

Also the set of numbers vary, for example the next 2 lines of numbers could be,

10 7 6
3 1 1 1 4 4  Reply With Quote

3. ## Re: Looking For Help With Algorithm That Processes Numbers

What have you tried?  Reply With Quote

4. ## Re: Looking For Help With Algorithm That Processes Numbers

Nothing as yet, as I am unsure where to start. I've an idea that uses stack queue but I am not sure that is relevant for my problem.  Reply With Quote

5. ## Re: Looking For Help With Algorithm That Processes Numbers

Start by reading the numbers from the first line into variables.
how many numbers need to be processed.
What does "processed" mean?

Use a piece of paper to do all the "processing" as per the instructions.
Make a list of the steps that were done, one after the other.  Reply With Quote

6. ## Re: Looking For Help With Algorithm That Processes Numbers

Ok sure, ill list the process with the above example

2 6 4
1 4 4 1

1, Reads 2, how many times the algorithm loops
2, Reads 6, addition of numbers can only be equal to or less than in a loop
3, Reads 4, how many numbers there are
4, reads 1, 1 is less than or equal to 6 so that get put into a total sum variable
5, reads 4, this is less than or equal to 6 so it gets added on to the previous number which becomes 5 now
6, reads 4, this number is less than or equal to 6 but when added on to the current total sum it become greater than 6 so it stops there, 1 loop done
7, with one loop done the numbers 1 and 4 go to the back of the number list, list is now 4 1 1 4
8, second loop starts
9, reads 4, this number is less than or equal to 6 so stored in total sum variable
10, reads 1, this number is less that or equal to 6 so its stored in total sum variable and when added to current number it is still less that 6,
11 reads 1, this number is less than or equal to 6 so it's stored in total sum and when added to current number 5 it equals 6
12, reads 4, this number is less than or equal to 6 but when added to total sum it is greater than 6, so number doesn't get added and loop finishes
13, total sum is displayed, which is this case is 11

Hope this helps.  Reply With Quote

7. ## Re: Looking For Help With Algorithm That Processes Numbers

That looks like a good description of what the program should do.
Can you write code to do that?  Reply With Quote

8. ## Re: Looking For Help With Algorithm That Processes Numbers

I can write the code to read the numbers and put them in an array with a loop but I cannot code the steps I've outline above as I've no idea how to implement something like that.  Reply With Quote

9. ## Re: Looking For Help With Algorithm That Processes Numbers

If the numbers are read and saved in a collection of some kind, it will have methods for getting the numbers out one at a time.
Step 4) says "read number". Use one of the collection methods to "read" the next number.
Follow the rules for putting the number into the total
or for stopping if > limit (6 in the example)

Use your paper and pencil notes to see what kind of collection is need to hold the numbers. How do the numbers need to be added and removed from the collection? Then look at the API doc to find a collection that fits.  Reply With Quote 