• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

working with arrays

 
Ralph Reinbeck
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there. I'm trying to do a few simple things with arrays of integers, but I'm having a bit of trouble. First, I'd like to write some code which returns true if two adjacent array elements have the same value, and it returns false otherwise. So as soon as it finds two matching entries (if they exist), it should return true. Here's what I tried (the array and the duplicates variable have been declared) -- these types of for loops always give me trouble, because I don't necessarily want to run the test for EVERY pair of entries, but rather pairs that don't match:

=====
for (int k = 0; k < array.length; k++)
if (array[k] = array[k + 1]) {
duplicates = true;
else
duplicates = false;
}
=====

I can see right away that this is going to be trouble, because when k = array.length - 1, there is no entry a[k + 1]. But I'm not sure what else to try.

My second problem is similar -- I'd like to see if ANY two elements of the array have the same value. I think I'll need two indices here, so this is what I've tried so far:

=====
for (j = 0; j < array.length; j++) {
for (k = 0; k < array.length; k++) {
if (array[0] == array[j])
duplicates = true;
else
duplicates = false;
}
}
=====

Again, I realize that I don't necessarily need to cycle through the entire range of values for my indices; I could get lucky and be able to stop as soon as the first pair of values. Any suggestions for how to avoid that mistake in logic?

I really appreciate any help that's out there.

Thanks!

R
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main problem here is that the duplicates flag is set after every comparison; therefore, the final value will be the value of the very last comparison done. Even if there are duplicates, this loop will only report if the last two elements are duplicated.

So what you need to do is terminate the loop early. There are two ways to do this: use the "break" keyword, or use "return" if this loop is the whole body of a method. There are people who prefer both styles. My personal preference is to eliminate the duplicates flag altogether, and use "return", putting this loop in its own method.
 
Ralph Reinbeck
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,

Thanks for your reply! I still have a question, though -- I understand that my approach sets the boolean flag after every comparison, but I don't understand how to use the "return" as you suggested.

Thanks!

R
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EFH: There are two ways to do this: use the "break" keyword, or use "return" if this loop is the whole body of a method...

You can also use your flag as a condition in your for loop.
 
Chad Clites
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use return like this (in psuedocode):

if(a == b) then return true;
else return false;

translating it to code:



So your loop would do something like:

I can see right away that this is going to be trouble, because when k = array.length - 1, there is no entry a[k + 1]. But I'm not sure what else to try.


Believe it or not, you actually answered your own question. If k = array.length, there indeed will not be a a[k + 1]. But what if you stop your loop one iteration before k = array.length?
[ July 23, 2006: Message edited by: Chad Clites ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

is better written as:

which in turn is better written by deleting the method altogether.
 
Ralph Reinbeck
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone -- your suggestions were very helpful. I appreciate them!

R
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic