class OrdArray
   {
   private double[] a;                // ref to array a
   private int nElems;                // number of data items
   //----------------------------------------------------------
      public OrdArray(int max)           // constructor
      {
      a = new double[max];            // create array
      nElems = 0;
      }
   //----------------------------------------------------------

   public int size()
      { return nElems; }
   //----------------------------------------------------------

   public int find(double searchKey)
      {
      int lowerBound = 0;// arrayA is empty,
      int upperBound = nElems-1;
      int curIn;
      while(true)
         {
         curIn = (lowerBound + upperBound   ) / 2;
         if(a[curIn]==searchKey)
            return curIn;                // found it         else
if(lowerBound > upperBound)
            return nElems;               // can't find it
         else                            // divide range
            {
            if(a[curIn] < searchKey)
               lowerBound = curIn + 1; //   it's in upper half
            else
               upperBound = curIn - 1; //   it's in lower half
            } // end else divide range
         } // end while
                                  
     }  // end find()
  //----------------------------------------------------------

  public void insert(double value)  // put element into array
     {
     int j;
     for(j=0; j<nElems; j++)        // find where it goes
        if(a[j] > value)            // (linear search)
           break;
     for(int k=nElems; k>j; k--)    // move higher ones up
        a[k] = a[k-1];
     a[j] = value;                  // insert it
     nElems++;                      // increment size
     } // end insert()
  //----------------------------------------------------------

  public boolean delete(double value)
     {
     int j = find(value);
     if(j==nElems)                  // can't find it
        return false;
     else                           // found it
        {
        for(int k=j; k<nElems; k++) // move higher ones down
           a[k] = a[k+1];
        nElems--;                   // decrement size
        return true;
        }
     } // end delete()
  //----------------------------------------------------------

  public void display()             // displays array contents
     {
     for(int j=0; j<nElems; j++)       // for each element,
        System.out.print(a[j] + " "); // display it
     System.out.println("");
     }
  //----------------------------------------------------------

  }  // end class OrdArray

class OrderedApp 
{
	public static void main (String[] args)
	{
		int maxsize = 100;
		OrdArray arr, arr1, arr2;
		arr = new OrdArray(maxsize);
		arr1 = new OrdArray(maxsize);
		arr2 = new OrdArray(maxsize);
		arr.insert(77);
		arr.insert(99);
		arr.insert(44);
		arr.insert(55);
		arr.insert(22);
		arr1.insert(88);
		arr1.insert(11);
		arr1.insert(00);
		arr1.insert(66);
		arr1.insert(33);
		
		 arr2.merge( arr1 , arr);
	}
		

	 public  void merge(int[] arr, int sizeArr,
			   int[] arr1, int sizearr1,
			   int[] arr2)
			   
			{
				   
			int aDex=0, bDex=0, arr2x=0;
			while(aDex < sizeArr && bDex < sizearr1)  
			if( arr[aDex] < arr1[bDex] )
			arr2[arr2x++] = arr[aDex++];
			else
			arr2[arr2x++] = arr1[bDex++];
			while(aDex < sizeArr)                  
			arr2[arr2x++] = arr[aDex++];  
			while(bDex < sizearr1)                  
			arr2[arr2x++] = arr1[bDex++];  
			} 
	}

The main method cannot be changed. like i am not allowed to. so the call has to be arr2.merge(arr , arr1)
that being said i know my merge method works
i just need to call it
or if my merge method is wrong can somebody help me write a merge method to merge arr1 and arr into arr2 without changing anything the main method
i just need a merge method to be called with those parameters
the book the code is taken form is called java data structures and algorithms
if you need more info please do say so

Recommended Answers

All 7 Replies

as you can see i already have code
i jsut need to make it work correctly
Please help would be greatly appreciated

Well, first of all you created merge method outside of OrdArray class, so no wonder it doesn't work
secondly it has wrong parameters, it should be

public  void merge(OrdArray arr, OrdArray arr1)

you dont need sizeArr parameter you can get the size by

arr.nElems

and instead of giving destination array as a last parameter just call directly to the object's table

a[arr2x++]

instead of

arr2[arr2x++]
commented: Without his code i wouldnt have gotten anywhere. +3

i will try it as soon as i get home, thanks
will reply with results

class OrdArray
   {
   public double[] a;                // ref to array a
   public int nElems;                // number of data items
   //----------------------------------------------------------
      public OrdArray(int max)           // constructor
      {
      a = new double[max];            // create array
      nElems = 0;
      }
   //----------------------------------------------------------
     
	
      public void insert(double value)  // put element into array
      {
      int j;
      for(j=0; j<nElems; j++)        // find where it goes
         if(a[j] > value)            // (linear search)
            break;
      for(int k=nElems; k>j; k--)    // move higher ones up
         a[k] = a[k-1];
      a[j] = value;                  // insert it
      nElems++;                      // increment size
      } // end insert()
	   
			
   public int size()
      { return nElems; }
   //----------------------------------------------------------

   public int find(double searchKey)
      {
      int lowerBound = 0;// arrayA is empty,
      int upperBound = nElems-1;
      int curIn;
      while(true)
         {
         curIn = (lowerBound + upperBound   ) / 2;
         if(a[curIn]==searchKey)
            return curIn;                // found it         else
if(lowerBound > upperBound)
            return nElems;               // can't find it
         else                            // divide range
            {
            if(a[curIn] < searchKey)
               lowerBound = curIn + 1; //   it's in upper half
            else
               upperBound = curIn - 1; //   it's in lower half
            } // end else divide range
         } // end while
                                  
     }  // end find()
  //----------------------------------------------------------

   public  void merge(OrdArray arr, OrdArray arr1)  // put element into array
   
		   
		{
			   
		int aDex=0, bDex=0, arr2x=0;
		while(aDex < arr.nElems && bDex < arr1.nElems)  
		if( a[aDex] < a[bDex] )
		a[arr2x++] = a[aDex++];
		else
		a[arr2x++] = a[bDex++];
		while(aDex < arr.nElems)                  
		a[arr2x++] = a[aDex++];  
		while(bDex < arr1.nElems)                  
		a[arr2x++] = a[bDex++];  
		} 

  //----------------------------------------------------------

  public boolean delete(double value)
     {
     int j = find(value);
     if(j==nElems)                  // can't find it
        return false;
     else                           // found it
        {
        for(int k=j; k<nElems; k++) // move higher ones down
           a[k] = a[k+1];
        nElems--;                   // decrement size
        return true;
        }
     } // end delete()
  //----------------------------------------------------------

  public void display()             // displays array contents
     {
     for(int j=0; j<nElems; j++)       // for each element,
        System.out.print(a[j] + " "); // display it
     System.out.println("");
     }
  //----------------------------------------------------------

  }  // end class OrdArray

class OrderedApp 
{
	public static void main (String[] args)
	{
		int maxsize = 100;
		OrdArray arr, arr1, arr2;
		arr = new OrdArray(maxsize);
		arr1 = new OrdArray(maxsize);
		arr2 = new OrdArray(maxsize);
		arr.insert(77);
		arr.insert(99);
		arr.insert(44);
		arr.insert(55);
		arr.insert(22);
		arr1.insert(88);
		arr1.insert(11);
		arr1.insert(00);
		arr1.insert(66);
		arr1.insert(33);
		
		arr2.merge( arr1 , arr);
		
		arr2.display();
	}
		
	
			}

thats my new code
it doesn't have any compiling errors
but the problem is the merge method doesn't work as i expected it to. :(

Now I am going to make some assumptions here on your merge method, My main assumption is if your method is called as arr2.merge( arr1 , arr); then you want the contents of arr1 and arr merged and in sorted order inside arr2.

Now if thats the case, why don't you parse the contents of "arr1" and "arr" separately and insert them into arr3 by calling arr3's insert() method, and as long as you are sure your insert method works fine, so will the merge method.

What I meant was something like this:-

public  void merge(OrdArray arr, OrdArray arr1)  {
  for(int i=0;i<arr.nElems<i++) {
    insert(arr.a[i]);
  }
 // Repeat similarly for the next object arr1
. .
. .
}

So as long as your insert() method works fine, your merge() method should not have issues.
Also a suggestion is refer to Sun Java Code Conventions, at least more important parts related to conventions for indenting and naming variables and methods.

Also you need to improve your skills in Object Oriented Programming, since in the program you are violating the principle of Encapsulation by making your member variables public. Your member variables should be private and in case you need to access them inside a different class implement the appropriate getter and setter methods that control access to these variables in an organized manner.

commented: Very nice and simple implementation +3

Now I am going to make some assumptions here on your merge method, My main assumption is if your method is called as arr2.merge( arr1 , arr); then you want the contents of arr1 and arr merged and in sorted order inside arr2.

Now if thats the case, why don't you parse the contents of "arr1" and "arr" separately and insert them into arr3 by calling arr3's insert() method, and as long as you are sure your insert method works fine, so will the merge method.

What I meant was something like this:-

public  void merge(OrdArray arr, OrdArray arr1)  {
  for(int i=0;i<arr.nElems<i++) {
    insert(arr.a[i]);
  }
 // Repeat similarly for the next object arr1
. .
. .
}

So as long as your insert() method works fine, your merge() method should not have issues.
Also a suggestion is refer to Sun Java Code Conventions, at least more important parts related to conventions for indenting and naming variables and methods.

Also you need to improve your skills in Object Oriented Programming, since in the program you are violating the principle of Encapsulation by making your member variables public. Your member variables should be private and in case you need to access them inside a different class implement the appropriate getter and setter methods that control access to these variables in an organized manner.

My man, thanks you thats exactly what i had in mind. i just could accomplish it dont know why.
yes my insert method works so all of that code worked out pretty good.
thank you

also im pretty sure you meant

for(int i=0;i<arr.nElems;i++)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.