My program should print out the smallest monitone decreasing
sequence. It just prints the number 4. I believe it has to do with switching the conditional statements. Anyone able to see where the error is.
public class BetterMonotonesequence
{
public static void main (String [ ] args)
{
int list[ ] = {4,3,2,1,7,8,13,12,11};
int incrseqindex = 0;
int incrseqlength = 1;
int crntincrseqindex = 0;
int crntincrseqlength = 1;
for(int i = 1; i %26lt; list.length; i++) {
if(list[i-1] %26gt; list[i]) {
crntincrseqlength++;
} else {
int c=incrseqindex;
int d=incrseqlength;
if(crntincrseqlength %26gt; incrseqlength) {
crntincrseqindex=c;
crntincrseqlength=d;
crntincrseqindex = i;
crntincrseqlength = i;
}
} // end else
} // end for
int a=incrseqindex;
int b=incrseqlength;
System.out.println("\nThe longest monotone decreasing sequence is:");
for (int i=a; i%26lt;b; i++)
System.out.print (list[i]+" ");
}
}
Why doesn't my java program print out the correct results?
You never updated incrseqindex or incrseqlength, so your loop looks like this:
for (int i=0; i%26lt;1; i++)
Therefore it will only loop through once and print the first value in list, which is 4.
email cards
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment