Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27
  1. #21
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    I'm hardly the one to say, but go ahead; I agree with you and we would all be interested at this point.

    Besides, wtd nielson has already posted a working solution that is simple and elegant. Just ran it. Should have done that much sooner. My bad and apologies.

    Note that that any duplicates will be adjacent in a sorted list. This is the key to his solution which works in one pass.

    Any differences would be of interest, whether as a correct model, or one to avoid.
    Last edited by rdtindsm; 12-22-2009 at 04:22 AM.

  2. #22
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Okay then. Here's my solution. I tried to keep it simple and there's much room for improvement. ;)
    Java Code:
    import java.util.*;
    
    public class Main {
        public static void main(String[] args) {
            // program parameters
            boolean loop = true;
            Scanner scanner = new Scanner(System.in);
            // an array based map
            int[] numbers = new int[100];
            int[] counters = new int[100];                
            int count = 0;
            // get the input
            while (loop) {
                System.out.println("Enter a number or enter -999 to continue: ");
                int next = scanner.nextInt();
                loop = next != -999;
                if (loop) {
                    int pos = 0;
                    boolean found = false;
                    // search for number and increment count
                    for (int i = 0; i < count; i++) {
                        if (numbers[i] == next) {
                            counters[i]++;
                            found = true;
                            break;
                        }
                    }
                    // if not found, add it to the back
                    if (!found) {
                        numbers[count] = next;
                        counters[count] = 1;
                        count++;
                    }
                }
            }        
            // sort the numbers with the bubble sort
            boolean swapped = true;
            while (swapped) {
                swapped = false;
                for (int i = 0; i < count - 1; i++)  {
                    if (numbers[i + 1] < numbers[i]) {                    
                        swapped = true;
                        int temp = 0;
                        // swap the number
                        temp = numbers[i + 1];
                        numbers[i + 1] = numbers[i];
                        numbers[i] = temp;
                        // swap the counter
                        temp = counters[i + 1];
                        counters[i + 1] = counters[i];
                        counters[i] = temp;
                    }
                }
            }
            // output the map
            for (int i = 0; i < count; i++) {
                System.out.println(numbers[i] + "_____" + counters[i]);
            }
        }
    }
    Hope this adds to the discussion. :)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #23
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by tim View Post
    Hope this adds to the discussion. :)
    The OP will be very grateful, copies and pastes your code and turns it in as if it were his own.

    kind regards,

    Jos

  4. #24
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Quote Originally Posted by tim View Post
    Can I post the solution? Others do not agree with this, but I feel that students learn best by example. ;)
    Quote Originally Posted by JosAH View Post
    The OP will be very grateful, copies and pastes your code and turns it in as if it were his own.
    If he plagiarizes, then he steals from himself. :rolleyes:
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  5. #25
    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Cool Re: inputs numbers then outputs how many time a particular number appears

    [QUOTE=koji_kun;96119]Ok, this is what my teacher wants me to do:

    Write a program that reads a set of positive integers and outputs how many times a particular number appears in the list. You may assume that the data set has at most 100 numbers and -999 marks the end of the input data. The numbers must be output in increasing order. For example, for the data:

    15 40 28 62 95 15 29 13 62 65 48 95 62 65 95 95 -999

    The output is:
    Number Count
    13_____1
    15_____2
    28_____2
    40_____1
    48_____1
    62_____3
    65_____3
    95_____4
    import java.io.BufferedReader;
    import java.io.InputStreamReader;


    public class NumberRepetition {

    public static void main(String[] args) throws Exception {
    int size;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("enter size of array");
    size=Integer.parseInt(br.readLine());
    int el;
    int[] a=new int[size];
    for(int i=0;i<size;i++)
    {
    System.out.println("enter a number");
    el=Integer.parseInt(br.readLine());
    a[i]=el;
    }
    for(int i=0;i<size;i++)
    {
    for(int j=0;j<size-1;j++)
    {
    if(a[j]>a[j+1])
    {
    int temp=a[j];
    a[j]=a[j+1];
    a[j+1]=temp;

    }
    }
    }
    int count=0;
    for(int i=0;i<size;i++)
    {
    for(int j=0;j<size;j++)
    {
    if(a[i]==a[j])
    {
    count++;

    }



    }
    System.out.println(a[i]+" \t " +"occurence time is"+"\t"+count);
    if(count!=0)
    {
    i=i+count-1;
    }
    count=0;
    }

    }

    }

  6. #26
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: inputs numbers then outputs how many time a particular number appears

    Quote Originally Posted by tim View Post
    Can I post the solution? Others do not agree with this, but I feel that students learn best by example. ;)
    I feel that students are piss-poor at actually learning something and look for "examples" to work around that.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #27
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: inputs numbers then outputs how many time a particular number appears

    The post before yours seem to have resurrected a 4 year old thread.
    I think I will put it to sleep.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 2 of 2 FirstFirst 12

Similar Threads

  1. printing number of pos/neg numbers input?
    By shroomiin in forum New To Java
    Replies: 12
    Last Post: 09-25-2009, 03:15 AM
  2. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 02:50 AM
  3. Compare two lists of number - what numbers arent there
    By Bishop609 in forum New To Java
    Replies: 5
    Last Post: 02-18-2009, 02:22 AM
  4. rounding a double in a number of significant numbers
    By rikribbers in forum Advanced Java
    Replies: 2
    Last Post: 10-27-2008, 04:35 PM
  5. trying to add up random numbers into one number
    By pjr5043 in forum New To Java
    Replies: 4
    Last Post: 09-15-2008, 03:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •