4 Replies - 390 Views - Last Post: 13 September 2011 - 12:54 AM Rate Topic: -----

#1 Rayden150  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 13-July 11

Array processing detail (help)

Posted 11 September 2011 - 10:08 AM

The program I need help with is supposed to
print out the numbers that are NOTrepeated
int other words if the user inputs uni[] = {1,2,4,2,5}
the 2 will not be printed out because its repeated
here is what I got so far:

import java.util.*;
public class Ejer4 {
	
	public static void main(String args[]){
		Scanner reader = new Scanner(System.in);
      int[] uni = new int[5];
      int [] apples = new int[5];
      int var = 0;
      
      
     for(int i = 0; i<5; i++){
      System.out.println("Input a number: ");
     uni[i] = reader.nextInt();}
        
    	   for(int y = 0; y<uni.length-1;y++){
    		     if(uni[y] != uni[y+1]){
    		    	 apples[y] = uni[y];
    		     }
    	   }
         for(int j=0; j<apples.length;j++)
    	  System.out.println("The non-repeating numbers are: " + apples[j]);
    	 }
    	 
     }
     

It works kind of well but it still prints out one of the repeated numbers
which isnt good, and also it prints out 0 at the end and
0 in place of the repeated number its understandable given that
the array has a fixed size.. so I hope I get some help.. thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Array processing detail (help)

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8009
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: Array processing detail (help)

Posted 11 September 2011 - 10:21 AM

And it also doeesn't detect repeats that aren't adjacent in sequence:

The sequence 1, 2, 3, 1, 2 outputs
The non-repeating numbers are: 1
The non-repeating numbers are: 2
The non-repeating numbers are: 3
The non-repeating numbers are: 1
The non-repeating numbers are: 0



Quote

also it prints out 0 at the end and 0 in place of the repeated number its understandable given that the array has a fixed size


That is because the loop that you use to assign the numbers to the second array never sees the last item in the loop.
Was This Post Helpful? 0
  • +
  • -

#3 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Array processing detail (help)

Posted 11 September 2011 - 12:59 PM

Like jon.kiparsky said this only detects for numbers that are repeated adjacent to each other. The way I would solve this problem is by first grabbing the first number and comparing it with each other number in the array, if you find another number that matches remove it from the list along with any other number that matches (along with the original number once you went through the whole array). If you don't find a number that matches it then simply leave it in the array and move to the next index.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8009
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: Array processing detail (help)

Posted 11 September 2011 - 01:10 PM

That's a reasonable approach. Two things I'd add:

1) keep track of how many items you currently have in the array, and only look at those. Otherwise, this won't allow you to enter zero.
2) what happens when you find a number that matches a previous entry? Rather than answer the question, I'll give you this to think about

I've entered 1, 2, 3, so the array contents are {1,2,3,0,0} and my counter is at 3.

I enter 2. What should the program do to make sure that my next entry goes correctly?

(limiting ourselves to arrays, and not a more reasonable data structure, there are at least two solutions that you could reasonably pursue - probably more)
Was This Post Helpful? 0
  • +
  • -

#5 jdwilliams51  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 15
  • Joined: 12-September 11

Re: Array processing detail (help)

Posted 13 September 2011 - 12:54 AM

So, you use Yahoo Answers too! The code, including the class name, is identical to the problem that I solved over there recently. My approach is to define a boolean[] array of the same size. Then once you get your input array you go through and set values in the boolean array. You use this array in your printing loop to decide which values to print.

Here are the relevant pieces of that code. Your code assumed 5 inputs, that's what this code handles.


        int[] uni = new int[5];
        boolean[] dup = new boolean[5];
        for (int i = 0; i < 5; i++) {
            System.out.println("Input a number: ");
            uni[i] = reader.nextInt();
            dup[i]=false;
        }
        for (int y = 0; y < uni.length-1; y++) {
            for (int x = y+1; x < uni.length; x++) {
                if (uni[y] == uni[x]) {
                    dup[y] = dup[x] = true;
                }
            }
        }
        for (int j = 0; j < uni.length; j++) {
            if (!dup[j]) {
                System.out.println("The non-repeating numbers are: " + uni[j]);
            }
        }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1