Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32

Thread: Array Merger

  1. #21
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default Re: Array Merger

    thats the same thing but my goal is to show the list of the merged arrays without showing the repeating numbers like:
    1,2,3,4,4,5,6,7,8,9
    it should only be;
    1,2,3,4,5,6,7,8,9

  2. #22
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: Array Merger

    Sorry, it is not the same thing.
    With the first there are three arrays used: two input, one merged & made unique
    With the second there are four arrays used: two input, one merged and one with unique elements.
    If you don't understand my response, don't ignore it, ask a question.

  3. #23
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default Re: Array Merger

    ok id go with the first one...

  4. #24
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default Re: Array Merger

    What is wrong with one of the Set impementations? (also see my reply#2); arrays are so Fortranesque and soooo clumsy for the job ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #25
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default Re: Array Merger

    it is what my teacher wants us to do ..

  6. #26
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: Array Merger

    Again make the list of steps to go through the array and remove duplicates.
    A way to visualize what needs to be done would be to write on a piece of paper a line of numbers with duplicates. Start at the left and move to the right. If the current number is already to the left of the current position, it's a duplicate and needs to be overwritten by copying a unique number over it from those numbers to the right. Continue to the end of the array.

    When you get done there will be unused slots at the end of the array, one for each duplicate number.
    If you don't understand my response, don't ignore it, ask a question.

  7. #27
    hardcoder is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Array Merger

    hey Yaki,
    I should not write down the code for removing duplicates from the array, because you wiil not get to learn and will loose that joy when you ll finish it, so here are the steps I can suuges.
    == take your merged array== iterate through it using for loops== get 1-1 element from that and compare it to all next to it== if it is equal then remove the another element with which you are comparing.
    Now if you want to create fourth array of unique elements without disturbing third array:
    iterate from third array== pick an int == iterate through fourth array == check if this int is there == if yes == break == if no add it in fourth array.

    Hope this will help!

  8. #28
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default Re: Array Merger

    Quote Originally Posted by hardcoder View Post
    hey Yaki,
    I should not write down the code for removing duplicates from the array, because you wiil not get to learn and will loose that joy when you ll finish it, so here are the steps I can suuges.
    == take your merged array== iterate through it using for loops== get 1-1 element from that and compare it to all next to it== if it is equal then remove the another element with which you are comparing.
    Now if you want to create fourth array of unique elements without disturbing third array:
    iterate from third array== pick an int == iterate through fourth array == check if this int is there == if yes == break == if no add it in fourth array.

    Hope this will help!


    heres my code i still cant get it right!

    Java Code:
    package app.runner;
    
    import java.util.Scanner;
    import java.util.TreeSet;
    
    public class Runner {
    
    	public static void main(String[] args)  {
    		int firstArray[] = {1,1,1,1,1};
    		int secondArray[] = {6,7,8,9,10};
    		int result[]= new int[10];
    		int talagangResult[] = new int [10];
    		int x=0;
    		int r=0;
    		
    		//this code is for the merging of two arrays..
    		for( x=0;x<firstArray.length;x++ ){
    
    			result[x] = firstArray[x];
    		}
    		for(int y=0;y<secondArray.length;y++ ){
    			result[y+5] = secondArray[y];
    		}
    
    		
    		//this is the start of the code that will be incharge of  
    		//putting the number on an array wthout the duplicates:::
    		for(r=0;r<result.length;r++ ){
    			boolean isMapupunta = false;	
    			for(int fLoop = 0 ;fLoop<result.length ; fLoop++ ){
    				
    					
    			
    				
    				//this code tests if result[r] is equal to
    				//result[fLoop] this tests if a number is a duplicate of another number
    				// then it will set isMapupunta "true" and
    				//if it is set as true then
    				//it will not go on the second if statement
    				// the second if statement is
    				//putting the non duplicate numbers
    				// to array " talagangResult[x]
    				System.out.println("   result: "+ result[r]+ " fLoop: "+result[fLoop]);
    				if(result[r] == result[fLoop]){
    					isMapupunta =true;
    					break;
    				}
    				
    				if(!isMapupunta){
    					talagangResult[x] = result[x];
    					
    				}
    
    			}
    		}
    		//this here prints the numbers
    		//that is contained by talagangResult through the process
    	
    			for(int s=0;s<talagangResult.length;s++ ){
    				System.out.println("result: "+talagangResult[s]);
    			}
    		
    	}
    }

  9. #29
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: Array Merger

    Can you post the program's output so we can see what it does?
    If you don't understand my response, don't ignore it, ask a question.

  10. #30
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default Re: Array Merger

    this is the output :

    this is for checking the loop:
    result: 1 fLoop: 1
    result: 1 fLoop: 1
    result: 1 fLoop: 1
    result: 1 fLoop: 1
    result: 1 fLoop: 1
    result: 6 fLoop: 1
    result: 6 fLoop: 1
    result: 6 fLoop: 1
    result: 6 fLoop: 1
    result: 6 fLoop: 1
    result: 6 fLoop: 6
    result: 7 fLoop: 1
    result: 7 fLoop: 1
    result: 7 fLoop: 1
    result: 7 fLoop: 1
    result: 7 fLoop: 1
    result: 7 fLoop: 6
    result: 7 fLoop: 7
    result: 8 fLoop: 1
    result: 8 fLoop: 1
    result: 8 fLoop: 1
    result: 8 fLoop: 1
    result: 8 fLoop: 1
    result: 8 fLoop: 6
    result: 8 fLoop: 7
    result: 8 fLoop: 8
    result: 9 fLoop: 1
    result: 9 fLoop: 1
    result: 9 fLoop: 1
    result: 9 fLoop: 1
    result: 9 fLoop: 1
    result: 9 fLoop: 6
    result: 9 fLoop: 7
    result: 9 fLoop: 8
    result: 9 fLoop: 9
    result: 10 fLoop: 1
    result: 10 fLoop: 1
    result: 10 fLoop: 1
    result: 10 fLoop: 1
    result: 10 fLoop: 1
    result: 10 fLoop: 6
    result: 10 fLoop: 7
    result: 10 fLoop: 8
    result: 10 fLoop: 9
    result: 10 fLoop: 10

    and this are the output that should be 1,6,7,8,9,10

    result: 0
    result: 0
    result: 0
    result: 0
    result: 0
    result: 6
    result: 0
    result: 0
    result: 0
    result: 0

  11. #31
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: Array Merger

    A better way to show the contents of an array is to used the Arrays class's toString() methd:
    Java Code:
            System.out.println("res="+java.util.Arrays.toString(result)); // res=[1, 1, 1, 1, 1, 6, 7, 8, 9, 10]
    
    
            System.out.println("talres="+java.util.Arrays.toString(talagangResult)); //talres=[0, 0, 0, 0, 0, 6, 0, 0, 0, 0]
    Go back and do the logic by using a piece of paper to see how to advance the array pointers to make sure you advance each one at the correct time.
    Also check when isMapupunta is true and when it will be set false as the code loops and the break is executed. Print out its value in the println that's there so you can see when tis true or false.
    Last edited by Norm; 09-09-2012 at 02:24 PM.
    If you don't understand my response, don't ignore it, ask a question.

  12. #32
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: Array Merger

    You need more debug print out to show what is happening. Print out the values of the from/source (result) and to/target()talagangResult) indexes for when the code copies an element and the vaue of the element that is being copied.
    If you don't understand my response, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  2. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

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