• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding whether the elements in an array are consecutive or not  RSS feed

 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing the code to find if the elements in an array are consecutive or not. The logic used is: if the difference between the next element and the previous element equals 1 for all the elements of the array, then the array is consecutive, otherwise it is non-consecutive.

However, my code prints the result "True" for both consecutive and non-consecutive arrays. Kindly review my code and let me know where I am going wrong.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khusbu Sinha wrote:
However, my code prints the result "True" for both consecutive and non-consecutive arrays. Kindly review my code and let me know where I am going wrong.


You do know that this code ...

is really this ...


right?

Henry
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.. That's a silly and gross mistake on my part. Thanks for pointing it out. I guess I need to stop skipping braces to avoid such errors.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the correction.




 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khusbu Sinha wrote:I guess I need to stop skipping braces to avoid such errors.

I cannot emphasize enough how true this is...not just you...but EVERYONE.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You aren't incrementing 'j'.
>
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need to increment `j` here because it gets incremented for every iteration when `i` is incremented because of its value:
j=i+!.

By the way, the program runs successfully.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khusbu Sinha wrote:There is no need to increment `j` here because it gets incremented for every iteration when `i` is incremented because of its value:
j=i+!.

By the way, the program runs successfully.

The only place I see "j=i+1" is in the initialization segment of the for() loop which only happens once. 'j' is not incremented in subsequent iterations.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using numbers to represent flags? Don't use the idenfier flag because it is too vague, and give it a boolean type. Don't use that sort of if‑else to set a boolean variable. Write this:-Depending how you wrote the loop, the consecutive && part may be redundant.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I updated my program. I hope it looks better now.





The only thing I could not understand was how come my program ran correctly in the first place without incrementing` j`.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compare how you cleaned up your code and how I cleaned it up.

Note the following:
1. Differences in clarity
2. Differences in formatting
3. Differences in the names chosen
4. Differences in how the program is organized and structured.
5. Differences in cohesiveness in the different sections of code. See cohesiveness of code.
6. I eliminated the variable j in the check for consecutive numbers. Only one index variable is needed.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Separating different concerns in your program makes it easier to refactor the code. Here's a slight improvement from what I did previously:

The allConsecutive(int[] numbers) method is concerned with going through an array of numbers whereas the notConsecutive(int i, int j) method is concerned with checking whether two specific numbers are consecutive. Notice that the expression in the notConsecutive() method is different from what I had before. It's important to separate the implementation from the intent. If you can express your intent at a high level of abstraction and hide implementation details in a small method, then your program will be much more flexible and easier to maintain and possibly improve. For instance, you might prefer to use a different expression in the notConsecutive method.

Changing the implementation can be done without much fear of somehow messing up the rest of the program. If you are also disciplined in testing your code often and after every little change you make to it, it's also very easy to limit any potential damage you do to your program when you make changes. So, there are many benefits to breaking up your program so that each little concern is separated and relatively isolated from other code.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khusbu Sinha wrote:. . . I hope it looks better now. . . .
Only slightly better style, I am afraid.

You can change Junilu's print instructions to:-
System.out.printf("The numbers are consecutive: %b%n", allConsecutive(numbers));
That will print true not True.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see..I need a lot of practice for 'Clean Coding'.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
print instructions to:- System.out.printf("The numbers are consecutive: %b%n", allConsecutive(numbers));.


In this expression, if %b is used for boolean value what is %n used for?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
%n generates a newline. Use that instead of the \n escape sequence as part of the format string argument to the System.out.printf() method.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khusbu Sinha wrote:I see..I need a lot of practice for 'Clean Coding'.

At least you're aware of the areas that you need to strengthen and even that it involves "clean code" -- get the book Clean Code by Robert Martin. it's well worth its price.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin)

Is this the book you are recommending?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another example to illustrate my point on how separation of concerns can make refactoring easier. Consider this modification to the allConsecutive() method:

I eliminated the need for a separate notConsecutive() method by choosing a variable name that expresses the same idea clearly. Admittedly, the name is a little unorthodox but I don't think it's to the detriment of clarity of the code. The only thing I might be a little put off by is the additional complexity in the for-loop header. Still, the separation of concerns allows me to quickly make that change without fear of messing up the program too much since I can easily revert it back to what it was before. So, another benefit of separation of concerns is that it affords me more opportunities to experiment with different implementations.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khusbu Sinha wrote:Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin)

Is this the book you are recommending?

Yes, that's the book.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote: Consider this modification to the allConsecutive() method:



I am sorry but right now I trying to get my head around the consecutive part of the for loop here. It will take time.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khusbu Sinha wrote:I am sorry but right now I trying to get my head around the consecutive part of the for loop here. It will take time.

If I were working on a team, that would be good enough reason for me to revert to the previous version.

There's nothing special about the name consecutive, it's just another variable. Alternatively, I could have named it expected, like so:


I just found that when I read the code *out loud*, the name consecutive expresses my intent better than the name expected.  That is, I think that "If the i-th number is not consecutive..." expresses the idea more clearly than "If the i-th number is not expected..."

Basically, that version is using one variable to set the value that you expect to find, and the variable i is your regular array index variable. So, start by taking the first element of the array and adding 1 to it. That's the value that I expect to find in the next array element. I start my loop iteration from the second array element, if it exists. That's why I initialize the loop variable i to 1 instead of 0.  As I iterate through the array, I just keep adding 1 to both the expected value and the array index variable.

It's also possible that a slight rearrangement of the code in the for-loop header expressions will help by making it look more like what most people expect to see:


 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I get it. Thank you so much Junilu.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!