0
Guys I’m hooked on this algorithm where I create an integer vector with n positions and need to show the longest consecutive sequence.
I will leave the statement to try to be more specific.
"Elaborate an algorithm where the user type 50 integer numbers of a vector and at the end the program tells the size of the largest consecutive increasing sequence."
ex:
6,7,9. Capital = 2.
5,6,7,8,11. Most important = 4
What I’ve done so far:
public class Questao02 {
public static void main(String[] args){
String aux;
int a[] = new int[5];
int cont=0;
for(int i=0;i<5;i++){
aux = JOptionPane.showInputDialog("Números");
a[i] = Integer.parseInt(aux);
if(a[i] < a[i+1] ){
cont++;
}
else{
cont=0;
}
}
System.out.println(cont);
}
}
It is not a difficult algorithm even for it is starting. You will need to use a
for
to traverse the vector and variables to account for the sequences found and verify that the sequence you have so far is larger than the one you’ve already caught so far.– Isac
When I use the for to go through the vector I need to make a comparison between the current value that is stored in the vector with the next vector value right, I tried to do it here but it’s going wrong.
– Leonardo Basilio
If you do not add your attempt and paste only the statement, the question will be closed again. Edit it and demonstrate what you have already tried to do, demonstrating that you have made some effort to try something.
– user28595
Articuno, I’ll add the code I tried to make
– Leonardo Basilio
And also take the opportunity to show exactly what point of code is not working for you and that is creating difficulties. Otherwise it is difficult for the community to help you solve the problem
– Isac
And don’t forget to explain at what point your difficulty, as @Isac said.
– user28595
My problem is that I am not able to see a solution in the if, I know it is wrong and also I’m having difficulty in getting the next vector value, I do not know if to do a[i+1], it is right.
– Leonardo Basilio
Ta mixing graphical interface with text mode? I recommend not to do this, it is a bad practice.
– user28595
I like to use Scanner, I’m using Joption out of curiosity, and not recommended in this case because it only accepts Strings, then I had to make this adaptation, using the variable aux.
– Leonardo Basilio