Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

about array..

Last post 05-14-2008 6:52 PM by Lwilliams. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 03-09-2008 2:17 AM

    • xaimei
    • Not Ranked
    • Joined on 03-08-2008
    • Philippines
    • New Member
    • Points 45

    about array..

    how will i do this one: Create a program that will 1. allow user to input 5 numbers using array. 2. Determine if the list is UNIQUE or NOT UNIQUE NOTE: UNIQUE - if there are no similar elements in the list NOT-UNIQUE - if there are similar elements in the list EXAMPLE: 1 2 3 4 4 It's NOT UNIQUE! please help me..

    • Post Points: 20
  • 03-10-2008 2:18 PM In reply to

    • Uncle
    • Top 75 Contributor
    • Joined on 01-10-2002
    • Guru
    • Points 1,470

    Re: about array..

     

    Here's some pseudocode for one method of solving your problem:

    Create the array.

    Get the five inputs and store them in the array.

    Sort the array into ascending order using your favourite sort algorithm.

    Create a Boolean and set it to False.

    Loop through the five variables; if one is the same as the next one, set the Boolean to True.

    If the Boolean is True, the list is Not Unique, otherwise the list is Unique.

    • Post Points: 10
  • 03-23-2008 12:34 AM In reply to

    • Lars_TP
    • Not Ranked
    • Joined on 03-22-2008
    • Denmark
    • New Member
    • Points 5

    Re: about array..

     Hi

    Here's a console program that solves the problem.

    Input is evaluated to check if the user has typed in an integers and it it's unique.

    The unique check is done by comparing the new integers with the previous accepted integers.

    /* Input5uniquenumbers.java
     * Created on 22. marts 2008, 23:56
     */
    import java.io.*;
    public class Input5uniquenumbers {  
        public static void main(String[] args) {
           
            int numberOfInputs = 5;
            System.out.println( "\nPlease type in " + numberOfInputs + " integers: " );
           
            InputStreamReader isr = new InputStreamReader( System.in );
            BufferedReader stdin = new BufferedReader( isr );

            String rawInput;
            int inputAsInteger = -1;
            int[] goodInteger = new int[numberOfInputs +1];
            goodInteger[0] = -1;    // index 0 is ignored
           
            boolean inputOk;
            boolean isUnique;
            //
            for ( int intNum = 1; intNum <= numberOfInputs; intNum++) {
            
                inputOk = true;
                rawInput = null;
                System.out.print( "\n" + intNum + ". integer ..");
               
                try {
                     rawInput = stdin.readLine();
                     inputAsInteger = Integer.parseInt( rawInput.trim() );
                } catch (IOException ioError) {
                     inputOk = false;
                     System.err.println( ioError );
                } catch (NumberFormatException ioError ) {
                     inputOk = false;
                     System.err.println( "Not an integer" );
                }
               
                // Evaluation
                if ( inputOk )  {
                    // is the number unique ?
                    isUnique = true;
                    // compares the previous accepted integers with the new one
                    for(int ix = 1; ix < intNum; ix++) {
                        if ( goodInteger[ix] == inputAsInteger ) {
                            isUnique = false;
                            System.err.println( "Not unique" );
                        }
                    }
                    if ( isUnique ) {
                        goodInteger[intNum] = inputAsInteger;
                    } else {
                        intNum--;           // eliminate the for loops intNum++
                    }
                } else {
                    intNum--;
                }
           
            }//ends for loop
           
            System.out.print( "\nYou typed in the following unique integers: " + goodInteger[1]);
            for(int ix = 2; ix <= numberOfInputs; ix++) {
                System.out.print( ", " + goodInteger[ix]  );
            }
        }//ends main()
    }

    • Post Points: 5
  • 04-22-2008 9:45 AM In reply to

    • xaimei
    • Not Ranked
    • Joined on 03-08-2008
    • Philippines
    • New Member
    • Points 45

    Re: about array..

    thanks for the help 

    • Post Points: 5
  • 05-14-2008 6:52 PM In reply to

    • Lwilliams
    • Not Ranked
    • Joined on 05-14-2008
    • Canada
    • New Member
    • Points 5

    Re: about array..

    Hello everyone 

    Seems you have your problem solved. I came into the thread to see if I could help, but was beaten to the punch.

    Best of luck!! .... Les ..... data recovery software 

    • Post Points: 5
Page 1 of 1 (5 items)