7 Replies - 268 Views - Last Post: 06 September 2012 - 12:41 PM Rate Topic: -----

#1 Thumbsup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-March 12

Different values in an Array

Posted 06 September 2012 - 11:43 AM

Hello,

I'm trying to make an algorithm which checks my array and returns the total amount of different values. I wrote my code, but it gives me wrong answer. Could anyone just check my code and see what I've done wrong?

public static int differentValues(int[] array] {
     int n = array.length;
     int total = 0;
     if(n < 1) return total;

     // This checks if array is sorted or not
     for(int i = 0; i < n-1; i++) {	
         if(a[i] > a[i+1]) System.out.println("Array not sorted!");		
      }

     // This should calculate the amount of total different values
     for(int i = 0; i < n-1; i++) {
         if(a[i] != a[i+1]) total++;
     }
     return total;
}


If my array is [1,1,2,3] I should get 3 as output, but my code gives me 2... What have I done wrong?

Edit: Both if-statements should be in same for loop, I just had to test with different values in for loop.

Is This A Good Question/Topic? 0
  • +

Replies To: Different values in an Array

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7643
  • View blog
  • Posts: 12,889
  • Joined: 19-March 11

Re: Different values in an Array

Posted 06 September 2012 - 12:04 PM

 for(int i = 0; i < n-1; i++) {


Since n is equal to array.length, counting out to i<n-1 will only get to the penultimate item: you'll go through each item whose index is less than length-1, not including the last element whose index is length-1.


There's also a wee problem that you seem to have two different arrays. You take the length of the array provided by the method call (which you're calling "array") but you're working on some other array, called "a". If your program is compiling (which it might not be, I see another problem) then "a" must be a global somewhere. Might be the same reference as "array", might not be.
Was This Post Helpful? 1
  • +
  • -

#3 Thumbsup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-March 12

Re: Different values in an Array

Posted 06 September 2012 - 12:10 PM

View Postjon.kiparsky, on 06 September 2012 - 12:04 PM, said:

 for(int i = 0; i < n-1; i++) {


Since n is equal to array.length, counting out to i<n-1 will only get to the penultimate item: you'll go through each item whose index is less than length-1, not including the last element whose index is length-1.


There's also a wee problem that you seem to have two different arrays. You take the length of the array provided by the method call (which you're calling "array") but you're working on some other array, called "a". If your program is compiling (which it might not be, I see another problem) then "a" must be a global somewhere. Might be the same reference as "array", might not be.


I tried
 for(int i = 0; i < n; i++) 
but I get ArrayIndexOutOfBoundsException error. Is it because I compare a[i] and a[i+1]?

About the "wee" problem, a and array is supposed to be the same array :P My mistake ^^
Was This Post Helpful? 0
  • +
  • -

#4 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Different values in an Array

Posted 06 September 2012 - 12:19 PM

View PostThumbsup, on 06 September 2012 - 03:10 PM, said:

I tried
 for(int i = 0; i < n; i++) 
but I get ArrayIndexOutOfBoundsException error. Is it because I compare a[i] and a[i+1]?


Yes, because when i == n-1, then i+1 == n. Because index's are 0-indexed, the highest index will always be (n-1). So array[(n-1) + 1] will throw an error because your asking the computer to access an index in the array, that is outside of the array's possible indexes.
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7643
  • View blog
  • Posts: 12,889
  • Joined: 19-March 11

Re: Different values in an Array

Posted 06 September 2012 - 12:19 PM

Sorry, my bad. No, your loop conditional was good. I was reading too fast.
if(a[i] != a[i+1]) total++;


What you're doing here is adding 1 to the total for each distinct value, that is, counting the distinct values (giving you an incorrect count, of course, because you start with your total at zero and you never add 1 for the first one). If you want to add up the distinct values, you want
if(a[i] != a[i+1]) total+=a[i]




EDIT: Now, looking at this again, I'm not sure what it is you want to do. Do you want to count the number of different values in the array, or add them up?

should [1,1,2,2,3,3] give 3, or should it give 12?

This post has been edited by jon.kiparsky: 06 September 2012 - 12:34 PM

Was This Post Helpful? 1
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: Different values in an Array

Posted 06 September 2012 - 12:20 PM

View PostThumbsup, on 06 September 2012 - 07:10 PM, said:

View Postjon.kiparsky, on 06 September 2012 - 12:04 PM, said:

 for(int i = 0; i < n-1; i++) {


Since n is equal to array.length, counting out to i<n-1 will only get to the penultimate item: you'll go through each item whose index is less than length-1, not including the last element whose index is length-1.


There's also a wee problem that you seem to have two different arrays. You take the length of the array provided by the method call (which you're calling "array") but you're working on some other array, called "a". If your program is compiling (which it might not be, I see another problem) then "a" must be a global somewhere. Might be the same reference as "array", might not be.


I tried
 for(int i = 0; i < n; i++) 
but I get ArrayIndexOutOfBoundsException error. Is it because I compare a[i] and a[i+1]?

About the "wee" problem, a and array is supposed to be the same array :P My mistake ^^


Yes it is because of the a[i+1]

If the length of the array is 10 then:
n = 10
i = 1, 2, 3 ..., 9
i+1 = 10 which would be out of bounds of the array
Was This Post Helpful? 1
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: Different values in an Array

Posted 06 September 2012 - 12:31 PM

Hmm....
[1,1,2,3]
i=0, (1!=1)==false, total=0
i=1, (1!=2)==true, total=1
i=2, (2!=3)==true, total=2



Of course, having starting number the same is your edge case.

While less neat, you could do something like:
int total = 1, last=a[0];
for(int i = 1; i < n; i++) {
	if(a[i] != last) { total++; last = a[i]; }
}


Was This Post Helpful? 1
  • +
  • -

#8 Thumbsup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-March 12

Re: Different values in an Array

Posted 06 September 2012 - 12:41 PM

Thanks guys, I didn't notice that I never counted the first one, that's why I always got one less that the answer. :P Also thanks for the explanation, just the mathematical failure from my side ^^

int total = 1;

if(array.length < 1) return 0;

for(int i = 0; i < array.length - 1; i++) if(array[i] != array[i+1]) total++;


This worked! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1