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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 28

Thread: Find the the longest decreasing row of numbers in a vector

  1. #1
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Find the the longest decreasing row of numbers in a vector

    Just started to work at this problem.

    Any suggestions?


     
    public class LongestDecreasing {
        public static void main(String[] args) {
            int N = args.length;
            int[] a = new int[N];
            for (int i = 0; i < a.length; i++) {
                a[i] = Integer.parseInt(args[i]);    
            }
     
            // solve problem here
     
     
            // print answer
            System.out.println(...);
        }   
    }


    --- Update ---

    For a vector of integers a[0],....., a[N-1] define the number of integers from i to j as sequence of a[i], a[i+1],...,a[j] when i<=j
    Last edited by jps; September 22nd, 2013 at 01:12 PM. Reason: code tags


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    You can find help with the code tags on the Announcements page

    "Any suggestions?"
    Sure, start by explaining the problem you are having, perhaps what the code is supposed to do vs what it actually does. Then ask a question which you think may help you to understand how to proceed

  3. The Following User Says Thank You to jps For This Useful Post:

    einar123 (September 22nd, 2013)

  4. #3
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    Excuse me. Code tags? I did highlight. Do you mean like explanation in the code itself? Meesa don't understand this.

    My problem is I dont know where start really. Not asking for soloution, just guidance. Somebody mentioned temp, I don't even know what that is or what it has to do with this code, whatever...

    The code is supposed to find the longest decreasing numbers in a vector (>=) given from a command line, count them and print that number.

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    [java=highlight] is not a valid code tag. I edited the post and fixed it for you. See the link for proper tags.

    Start with "How would you find the answer without a computer?" ... if someone were to hand you a sheet of paper with the numbers, what steps would you take to find the answer?

  6. The Following User Says Thank You to jps For This Useful Post:

    einar123 (September 22nd, 2013)

  7. #5
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    I think I'm almost there.

    How can I fix this code so that it works?


    public class VectorDecreasing {
        public static void main(String[] args) {
            int N = args.length;
            int[] a = new int[N];
            int acounter=1;
            int bcounter=1;
            for (int i = 1; i <= a.length; i++) {
                a[i] = Integer.parseInt(args[0]);    
                  if (a[i-1]>=a[i])
                      {
                          bcounter++;
                      }
                  else 
                      {
                          bcounter=1;
                      }
     
                  if( bcounter>acounter)
                      {
                         bcounter=acounter;
                      }
                      {
              System.out.print(bcounter);
              }}
    }
    }

    Error:
    111 Exception in thread "main" java long ArreyIndexOutofBoundsExeption 4 at VectorDecreasing.main(VectorDecreasing.java:8)

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    for (int i = 1; i <= a.length; i++)

    <= a.length : get rid of the '='.

  9. #7
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    args should be = i

  10. #8
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    Hello.
    Do you know about arrays in java?
    If I have an array say 'a' of ten integers. Then these elements are accessible as a[0], a[1], ... , a[9].
    In general if an array has n elements these elements are accessed as [0], [1], ..., [n-1].
    But in your program you are doing a[n] which is incorrect. Hence the exception.

    Syed.

  11. The Following User Says Thank You to syedbhai For This Useful Post:

    einar123 (September 23rd, 2013)

  12. #9
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    GregBrannon. The System.out print...I'm just not cutting it

  13. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    Quote Originally Posted by einar123 View Post
    GregBrannon. The System.out print...I'm just not cutting it
    I have no idea what this means. Don't be timid about adding a few extra words that actually explain what you're trying to say.

  14. The Following User Says Thank You to GregBrannon For This Useful Post:

    einar123 (September 23rd, 2013)

  15. #11
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    hahahah I'l admit that I'm forum timid

  16. #12
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    Quote Originally Posted by einar123 View Post
    Error:
    111 Exception in thread "main" java long ArreyIndexOutofBoundsExeption 4 at VectorDecreasing.main(VectorDecreasing.java:8)
    This tutorial will explain in detail why you get the error, and help explain what the others are saying

  17. The Following User Says Thank You to jps For This Useful Post:

    einar123 (September 23rd, 2013)

  18. #13
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    My problem now is that I dont ,,take'' the number in a0. But I feel like I'll have to have i=1 because if I have i=0 then I'm out of bounds....but then again I'm out of bounds at the other end...

  19. #14
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    Quote Originally Posted by einar123 View Post
    if I have i=0 then I'm out of bounds....
    The index for an array starts with 0. See my previous post and visit the tutorial

  20. The Following User Says Thank You to jps For This Useful Post:

    einar123 (September 23rd, 2013)

  21. #15
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    Ok. Would you give correct for doing it this way?

     
    public class LongestDecreasing {
        public static void main(String[] args) {
            int N = args.length;
            int[] a = new int[N];
            int bcounter=1;
            int acounter=1;
            for (int i = 0; i < a.length-1; i++) {
                a[i] = Integer.parseInt(args[i]);
     
     
     
                    if(a[i]>=a[i+1])
                      {
                          bcounter++;
                      }
                    else 
                      {
                          acounter++;
                      }
     
                     if(acounter>bcounter)
                      {
                         bcounter=acounter;
                      }
                      {
     
              }
     
              }
              System.out.print(bcounter);
    }
    }


    --- Update ---

    Perhaps the question should be, is there another way of doing this?

    --- Update ---

    Ok, this is not correct.

  22. #16
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    This tutorial shows a loop with an array.
    Start at the tutorial I posted previously and work your way through, it is very useful

    Add some print statements to see the values of the variables in question.
    i, a.length, a.length-1
    Get some numbers that have meaning, and see what is happening

  23. #17
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    Ok. Is this as bad as the last one?

    And is'nt this the: We solve your homework forum?

    package forrit;
     
    public class Longesdecreasingttt {
     
    	    public static void main(String[] args) {
    	        int N = args.length;
    	        int[] a = new int[N];
    	        int downcounter=1;
    	        int upcounter=1;
    	        for (int i = 0; i < N; i++) {
    	            a[i] = Integer.parseInt(args[i]);
    	}
     
    	    {
    	               for(int i=0;i<N-1;i++)
     
    	            	   if (a[i]>=a[i+1])
    	                  {
    	                      downcounter++;
    	                  }
    	            	   	else 
    	            	   		{
    	            	   			upcounter++;
    	            	   			if (upcounter>downcounter) 
    	            	   			{
    	            	   				downcounter=1;                 
    	            	   			}
     
    	            	   		}
     
    	          System.out.print(downcounter);
    	}
    	}
    }


    --- Update ---

    Ahh-sorry did'nt see that last post.Yeah I'll look through that.

    --- Update ---

    Mr.Jps can you please fix the double posting problem.

    --- Update ---

    I'm wondering, is this correct?

  24. #18
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    It looks like you solved the problems you were having with arrays. Does the program do what it's supposed to do? How are you testing it?

  25. #19
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    Mr.Jps. I find it so difficult to read this kind a stuff...I'll always get the sleepreading syndrome we see this, and just give up. Sort of like when I read my terrible tabing(syntax) in me last post muhhahah.

    --- Update ---

    Yeah. I put trough like Jeliot. Seems to working.

    My professor puts the code throug all kinds of sequences like {1,1,-9,11, ,} {0,0,0} whatever. He's a shark, but nice shark. And he gives us zero for not fullfilling the standard.

    Anyway I trying allsorts of stuff also from the command...

  26. #20
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    Mr.Jps. I find it so difficult to read this kind a stuff...I'll always get the sleepreading syndrome we see this, and just give up.
    IMO, if you want to be successful as a programmer, I think you need to get over this. So much of learning to program is inspired by self-motivated interest and exploration, largely through printed matter, that results in skills that are practiced to death and ultimately self taught. If you can't stay awake through this important process, then you should think about pursuing another field.

  27. #21
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    Ok. Does anyone in Java-universe know why this code is giving 5 for this sequence:
    0,0,5,79,8,8,7,4

    --- Update ---

    Ahh-sorry-that is correct?!

    --- Update ---

    I'm wondering. This is a correct code for a problem in a popular textbook. Should'nt we like...wipe some of it of?

    --- Update ---

    Or at least I think it is correct

    --- Update ---

    Ok lads. Now I have to prove this code with loop invariant?! Do you know anything about that?

  28. #22
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    No this is not the "we solve your homework forum" this is the "we help you understand what you need to know so you can do your own homework forum".

    The double posts will concatenate into one post with an update in a short time after posting, just ignore it.

    Do you know what an invariant is? What invariant was given in the instructions (directly or indirectly)?

  29. The Following User Says Thank You to jps For This Useful Post:

    einar123 (September 23rd, 2013)

  30. #23
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    Thanks jps. I think directly. But my program is not correct. I'll put up a new post.

    --- Update ---

    The invariant is supposed to be something like this:

    1.F-->I
    2.I ^ not R}S{I}
    3.I ^ not R=>E

    --- Update ---

    What is wrong with me code?

    For this sequence:{1,3,3,5,4,2,3} cracks it up.

     
     
    public class Verkefni2t {
     
            public static void main(String[] args) {
                int N = args.length;
                int[] a = new int[N];
                int downcounter=1;
                int upcounter=1;
                for (int i = 0; i < N; i++) {
                    a[i] = Integer.parseInt(args[i]);
        }
     
            {
                for(int i=0;i<N-1;i++)
     
                  if (a[i]>=a[i+1])
                      {
                         downcounter++;
                      }
                       else 
                          {
                           upcounter++;
                           if (upcounter>downcounter) 
                               {
    								downcounter=1;                 
                               }
     
                           }
     
                  System.out.print(downcounter);
        }
        }
    }

    Error: at Java.lang.NumberFormatException.forInputString(unk nown Source)
    at javalanginteger.parseInt(unknownSource)
    at...[one more line of the same as the above]
    at Verkefni2t.main java10

    --- Update ---

    What is going on with that downcounter over there?

    That is the problem, right there!

  31. #24
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    Do you understand what causes the exception to be thrown? If not search Java.lang.NumberFormatException
    If you do, then look at the code to see why the exception was thrown.

  32. The Following User Says Thank You to jps For This Useful Post:

    einar123 (September 23rd, 2013)

  33. #25
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Find the the longest decreasing row of numbers in a vector

    Ok. I have a string in the array?

    --- Update ---

    Whitespace?

    --- Update ---

    It's the commas!

    --- Update ---

    I dont see it?! Something in line 10...

    But why does the ''code give'' 1 for the sequence {1 3 3 5 4 2 3}
    and {1 2 2 3}?

Page 1 of 2 12 LastLast

Similar Threads

  1. Why width of dropdown is decreasing automatically on every select?
    By ayush1989 in forum Other Programming Languages
    Replies: 0
    Last Post: February 1st, 2013, 01:39 AM
  2. Binary Tree Longest Increasing Path (Recursive)
    By varsis in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 13th, 2012, 10:19 PM
  3. [SOLVED] Display longest string on screen
    By mwardjava92 in forum Object Oriented Programming
    Replies: 1
    Last Post: January 29th, 2012, 12:12 PM
  4. Increasing or decreasing!
    By pesha in forum Algorithms & Recursion
    Replies: 4
    Last Post: November 6th, 2011, 04:06 PM
  5. Vector Addition with small numbers giving NaN
    By Zula in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 28th, 2010, 09:29 PM