Just started to work at this problem.
Any suggestions?
--- 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
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.
Just started to work at this problem.
Any suggestions?
--- 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
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
einar123 (September 22nd, 2013)
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.
[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?
einar123 (September 22nd, 2013)
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)
for (int i = 1; i <= a.length; i++)
<= a.length : get rid of the '='.
args should be = i
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.
einar123 (September 23rd, 2013)
GregBrannon. The System.out print...I'm just not cutting it
einar123 (September 23rd, 2013)
hahahah I'l admit that I'm forum timid
This tutorial will explain in detail why you get the error, and help explain what the others are saying
einar123 (September 23rd, 2013)
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...
einar123 (September 23rd, 2013)
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.
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
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?
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?
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...
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.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.
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?
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)?
einar123 (September 23rd, 2013)
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!
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.
einar123 (September 23rd, 2013)
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}?