6 Replies - 4478 Views - Last Post: 08 February 2015 - 07:39 PM Rate Topic: -----

#1 njcountrygirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 20-October 14

Printing Distinct Numbers

Posted 08 February 2015 - 05:00 PM

so for class I have an assignment that involves printing distinct numbers. the assignment is as follows: Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly on space. Here is what the input and the output are supposed to look like:

Input: Enter Ten Numbers: 1 2 3 2 1 6 3 4 5 2
Output: The Number of distinct numbers is 6
The distinct numbers are: 1 2 3 6 4 5

So far I have the entire code but right now it only prints out the distinct numbers, not how many distinct numbers there are. Can someone please help me figure out what that part of the code would look like? Thanks in advance! Here is my current code:

public class exer75 {
        public static void main(String[] args) {
                java.util.Scanner input = new java.util.Scanner(System.in); //create scanner system
 
                System.out.println("");
                System.out.print("Enter Ten Numbers: "); //prompt the user to enter 10 numbers
                int[] values = new int[10]; //create an array to hold the numbers
 
                int count = 0; //initalize an integer to count the number of distinct numbers
 
                //create two loops to find which numbers are distinct
                for (int i=0; i<10; i++){
                        int number = input.nextInt();
                        boolean distinct = true;
  
                for (int j=0; j<count; j++){
                        if (number == values[j]){
                                distinct = false;
                                break;
                                }
                        }
                        if (distinct) values[count++] = number;
                }
                System.out.println("");
                System.out.print("The Distinct Numbers are: "); //print out "The Distinct Numbers are"
                for (int i=0; i<count; i++){ //create a loop to print out each distinct number
                        System.out.print(values[i]+ " ");
                }
                        System.out.println(""); //add another print line
        }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Printing Distinct Numbers

#2 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10720
  • View blog
  • Posts: 18,353
  • Joined: 19-March 11

Re: Printing Distinct Numbers

Posted 08 February 2015 - 05:38 PM

You could keep a counter, and when you find a number that is distinct, increment that counter.


Also, you should fix your indentation. Good style is important. Bad style makes it easier to make silly mistakes. (for example, in this case you have to look carefully to see that you have a nested loop, and not two loops one after then other)
Was This Post Helpful? 0
  • +
  • -

#3 njcountrygirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 20-October 14

Re: Printing Distinct Numbers

Posted 08 February 2015 - 06:53 PM

View Postjon.kiparsky, on 08 February 2015 - 05:38 PM, said:

You could keep a counter, and when you find a number that is distinct, increment that counter.


Also, you should fix your indentation. Good style is important. Bad style makes it easier to make silly mistakes. (for example, in this case you have to look carefully to see that you have a nested loop, and not two loops one after then other)


don't I want a nested loop? if not what should that look like? also how do I keep a counter? I feel like I have some idea but I can't quite remember exactly. Your help is greatly appreciated!
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10720
  • View blog
  • Posts: 18,353
  • Joined: 19-March 11

Re: Printing Distinct Numbers

Posted 08 February 2015 - 07:10 PM

Yes, a nested loop is what you want - but the way you've styled your code hides that aspect of the structure. This makes it harder to see what's going on. Programs are complicated, and most of what counts as good programming technique is about reducing that complexity.

Like so:
import java.util.Scanner;
public class exer75 {
        public static void main(String[] args) {
                java.util.Scanner input = new Scanner(System.in); 
 
                System.out.println("");
                System.out.print("Enter Ten Numbers: "); 
                int[] values = new int[10];
 
                int count = 0; 
                 
                for (int i=0; i<10; i++){
                        int number = input.nextInt();
                        boolean distinct = true;
  
                        for (int j=0; j<count; j++){
                                if (number == values[j]){
                                        distinct = false;
                                        break;
                                }
                        }
                        if (distinct) {  
                                values[count++] = number;
                                }
                }
                System.out.println("");
                System.out.print("The Distinct Numbers are: "); 
                for (int i=0; i<count; i++){                
                        System.out.print(values[i]+ " ");
                }
                System.out.println(""); //add another print line
        }
}


Getting the indentation right and removing the excess commentary makes that a lot easier to read. And in doing that, I notice that you're already keeping a counter. It's called count.

Do you see how it works?
Was This Post Helpful? 0
  • +
  • -

#5 njcountrygirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 20-October 14

Re: Printing Distinct Numbers

Posted 08 February 2015 - 07:16 PM

View Postjon.kiparsky, on 08 February 2015 - 07:10 PM, said:

Getting the indentation right and removing the excess commentary makes that a lot easier to read. And in doing that, I notice that you're already keeping a counter. It's called count.

Do you see how it works?


yes I do see it, not so sure how it works though. I know I put count there for a reason. Do I need to make another loop that adds to the count or can I just simply display the count?

This post has been edited by jon.kiparsky: 08 February 2015 - 07:35 PM
Reason for edit:: no need to quote the entire previous post

Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10720
  • View blog
  • Posts: 18,353
  • Joined: 19-March 11

Re: Printing Distinct Numbers

Posted 08 February 2015 - 07:37 PM

Well, work it through - how does the value of count evolve over time? Here's an easy question: what is the value of count before the first iteration of the loop? Why?
Slightly harder one: what's the value of count after the first iteration? Why?
Was This Post Helpful? 0
  • +
  • -

#7 njcountrygirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 20-October 14

Re: Printing Distinct Numbers

Posted 08 February 2015 - 07:39 PM

yay figured it out!!! thanks for the help! can't believe I set it up to count but then never did anything with it. This code stuff really fries my brain! ugh!

This post has been edited by jon.kiparsky: 08 February 2015 - 07:55 PM
Reason for edit:: removed more excessive quoting

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1