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

• September 22nd, 2013, 01:09 PM
einar123
Find the the longest decreasing row of numbers in a vector
Just started to work at this problem.

Any suggestions?

Code java:

```  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,....., 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
• September 22nd, 2013, 01:11 PM
jps
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
• September 22nd, 2013, 02:18 PM
einar123
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.
• September 22nd, 2013, 03:22 PM
jps
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?
• September 23rd, 2013, 12:12 PM
einar123
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?

Code Java:

```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); 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)
• September 23rd, 2013, 12:30 PM
GregBrannon
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 '='.
• September 23rd, 2013, 12:31 PM
einar123
Re: Find the the longest decreasing row of numbers in a vector
args should be = i
• September 23rd, 2013, 12:32 PM
syedbhai
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, a, ... , a.
In general if an array has n elements these elements are accessed as , , ..., [n-1].
But in your program you are doing a[n] which is incorrect. Hence the exception.

Syed.
• September 23rd, 2013, 12:34 PM
einar123
Re: Find the the longest decreasing row of numbers in a vector
GregBrannon. The System.out print...I'm just not cutting it
• September 23rd, 2013, 12:38 PM
GregBrannon
Re: Find the the longest decreasing row of numbers in a vector
Quote:

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.
• September 23rd, 2013, 12:42 PM
einar123
Re: Find the the longest decreasing row of numbers in a vector
hahahah I'l admit that I'm forum timid
• September 23rd, 2013, 12:43 PM
jps
Re: Find the the longest decreasing row of numbers in a vector
Quote:

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
• September 23rd, 2013, 12:46 PM
einar123
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...
• September 23rd, 2013, 12:49 PM
jps
Re: Find the the longest decreasing row of numbers in a vector
Quote:

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
• September 23rd, 2013, 02:44 PM
einar123
Re: Find the the longest decreasing row of numbers in a vector
Ok. Would you give correct for doing it this way?

Code Java:

```  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.
• September 23rd, 2013, 03:29 PM
jps
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
• September 23rd, 2013, 04:37 PM
einar123
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?

Code java:

```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?
• September 23rd, 2013, 04:43 PM
GregBrannon
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?
• September 23rd, 2013, 04:51 PM
einar123
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...
• September 23rd, 2013, 04:57 PM
GregBrannon
Re: Find the the longest decreasing row of numbers in a vector
Quote:

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.
• September 23rd, 2013, 05:10 PM
einar123
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?
• September 23rd, 2013, 05:17 PM
jps
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)?
• September 23rd, 2013, 05:47 PM
einar123
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.

Code Java:

```    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!
• September 23rd, 2013, 05:49 PM
jps
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.
• September 23rd, 2013, 06:21 PM
einar123
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}?