8 Replies - 20278 Views - Last Post: 11 November 2009 - 08:20 PM Rate Topic: -----

#1 bdavis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-November 09

Distinct Values in an Array

Posted 11 November 2009 - 12:59 PM

I have confused myself way to long with this simple program. I am supposed to write a program that reads 10 numbers and displays the distinct numbers. Example layout of the output is:

Enter an integer: 1
Enter an integer: 3
Enter an integer: 2
Enter an integer: 1
Enter an integer: 5
Enter an integer: 4
Enter an integer: 5
Enter an integer: 2
Enter an integer: 4
Enter an integer: 5
The number of distinct values is 5
1 3 2 5 4


I understand the logic. I even researched using a set, but this would be outside the assignment and I wouldnt probably get credit. I'm just missing how to link Array1 to Array2 and how does this run all in one loop. Any help would be appareciated it.

import java.util.Scanner;

/**
 *
 * @author Bear
 */
public class Exercise6_5 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);


		String output = "";
		String output1 = "";
		int[] numbers = new int[10];
		int DistinctArray[] = new int[numbers.length];
	   int count = 0;
	   


		for (int i = 0; i < numbers.length; i++) {
			System.out.print("Enter a number:");
			// Conver String into integer
			numbers[i] = input.nextInt();

		}

		   
			for (int i = 0; i < numbers.length; i++) {
				
				int temp = numbers[i];
				if (temp != numbers[i]) {
							  
					output += numbers[i] + " ";
					

			}
				else
				   DistinctArray[i] = temp - numbers[i];
				output1 += DistinctArray[i] + " ";
				count++;
		
			System.out.println("The number of distinct values is: " + count);
			System.out.println("The array is: " + output);
			System.out.println("The test distinct array is: " + output1);



		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Distinct Values in an Array

#2 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: Distinct Values in an Array

Posted 11 November 2009 - 01:06 PM

so can you clearly explain what the problem is
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Distinct Values in an Array

Posted 11 November 2009 - 01:26 PM

To do all of the distinct numbers in the loop, I would use a separate method, which I call isIn() to test whether or not an element is actually in the DistinctArray. If it is, then we simply add the number to the output list. If it is not yet in there, then we add it to the DistinctArray, increase the count, and add it to both strings. If you need just one loop for input and processing, I'm sure that would not require much at this point. I re-wrote your code with the above adjustments.

import java.util.Scanner;

/**
 *
 * @author Bear
 */
public class Exercise6_5 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		int NUMELEMS = 10;

		String output = "";
		String output1 = "";
		int[] numbers = new int[NUMELEMS];
		int DistinctArray[] = new int[NUMELEMS];
		int count = 0;
	   


		for (int i = 0; i < numbers.length; i++) {
			System.out.print("Enter a number(Maximum of 10 numbers total): ");
			// Conver String into integer
			numbers[i] = input.nextInt();
		}

		   
		for (int i = 0; i < numbers.length; i++) {
			   
			int temp = numbers[i];
			
			// I would use a method call here
			if (!isIn(temp, DistinctArray))
			{
				DistinctArray[i] = temp;
				count++;
				output += temp + " ";
				output1 += temp + " ";
			} 
			else
			{
				output += temp + " ";
			}
			
		}
	   
		System.out.println("The number of distinct values is: " + count);
		System.out.println("The array is: " + output);
		System.out.println("The test distinct array is: " + output1);
	}
	
	public static boolean isIn(int num, int[] a)
	{
		for (int i = 0; i < a.length; i++) {
			if (a[i] == num)
				return true;
		}
		
		return false;
	}
}


Was This Post Helpful? 1
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,278
  • Joined: 27-December 08

Re: Distinct Values in an Array

Posted 11 November 2009 - 01:42 PM

You could use a HashSet<Integer> to solve your problem. A Set (HashSet implements the Set interface) is a data structure that holds only unique values. So I would just add all the numbers to the Set and then get the size() as the number of distinct values. So if you add the value 2 to the Set and it already exists, nothing will happen. Here's what I mean:

HashSet<Integer> distinct = new HashSet<Integer>();
Scanner scan = new Scanner(System.in);
for(int i = 0; scan.hasNextInt() && i < 10; i++)
	 distinct.add(scan.nextInt());
System.out.println("Number of unique values " + distinct.size());



Also, like an ArrayList, Sets are resizable. In this way, you don't have to keep checking for unique values in your array. Good luck!
Was This Post Helpful? 1
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Distinct Values in an Array

Posted 11 November 2009 - 01:44 PM

Quote

I understand the logic. I even researched using a set, but this would be outside the assignment and I wouldnt probably get credit.


That's why I didn't use a set. It still is nice to know though!
Thanks!

This post has been edited by Dogstopper: 11 November 2009 - 01:46 PM

Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,992
  • Joined: 20-September 08

Re: Distinct Values in an Array

Posted 11 November 2009 - 01:55 PM

I didn't notice - were you given bounds on the actual values entered? I know that there should be maximum of ten values entered
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,278
  • Joined: 27-December 08

Re: Distinct Values in an Array

Posted 11 November 2009 - 02:00 PM

View PostDogstopper, on 11 Nov, 2009 - 04:44 PM, said:

Quote

I understand the logic. I even researched using a set, but this would be outside the assignment and I wouldnt probably get credit.


That's why I didn't use a set. It still is nice to know though!
Thanks!


I completely missed that. Thanks for pointing that out. :)
Was This Post Helpful? 0
  • +
  • -

#8 bdavis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-November 09

Re: Distinct Values in an Array

Posted 11 November 2009 - 08:05 PM

THank you for the help! Doggstopper I appreciate it. I knew i was close. Just had to interchange variables. Also the method statement was key. Thanks!
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Distinct Values in an Array

Posted 11 November 2009 - 08:20 PM

View Postbdavis, on 11 Nov, 2009 - 09:05 PM, said:

THank you for the help! Doggstopper I appreciate it. I knew i was close. Just had to interchange variables. Also the method statement was key. Thanks!


You're very welcome! Good luck in future endeavors!

-- Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1