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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Looking For Help With Algorithm That Processes Numbers

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Looking For Help With Algorithm That Processes Numbers

    What have you tried?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. ctt-sp algorithm(cost transitive tournament shortest path algorithm)
    By seeniya in forum Java Theory & Questions
    Replies: 3
    Last Post: April 22nd, 2014, 05:36 AM
  2. Java debugging techniques and processes
    By frenley in forum Java Theory & Questions
    Replies: 1
    Last Post: January 27th, 2014, 07:46 AM
  3. Input processes using array
    By ramdevera in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 27th, 2012, 07:40 AM
  4. Need a more efficient algorithm for prime numbers.
    By firstTimeJava in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 16th, 2012, 10:15 PM
  5. Background Command Line Processes
    By KILL3RTACO in forum Java Theory & Questions
    Replies: 7
    Last Post: October 2nd, 2011, 01:35 PM

Tags for this Thread