Results 1 to 3 of 3
Thread: MyProgrammingLab Questions
- 07-24-2013, 10:58 PM #1
Member
- Join Date
- Jun 2013
- Posts
- 17
- Rep Power
- 0
MyProgrammingLab Questions
I only have two problems from the MyProgrammingLab that I cannot get...I've tried different things 5 times and just can't get it.
The first is:
Suppose* you have two arrays* of ints*, arr1 and arr2, each containing ints* that are sorted in ascending order.
Write a static* method* named* merge that receives these two arrays* as parameters* and returns a reference* to a new, sorted array* of ints* that is the result of merging the contents of the two arrays*, arr1 and arr2.
This was my answer:
Java Code:public static int[] merge(int[] arr1, int[] arr2) { int[] merged = new int[arr1.length + arr2.length]; for (int k = 0; k < arr1.length; k++) merged[k] = arr1[k]; for (int j = 0; j < arr2.length; j++) merged[arr1.length j] = arr2[j];
CTest.java:9: error: ']' expected
•• We think you might want to consider using: if
•• We think you might want to consider using: else
•• We think you might want to consider using: while
The 2nd one is:
Assume* the existence of a Widget class* that implements the Comparable interface and thus has a compareTo method* that accepts an Object* parameter* and returns an int*. Write an efficient static* method*, getWidgetMatch, that has two parameters*. The first parameter* is a reference* to a Widget object*. The second parameter* is a potentially very large array* of Widget objects* that has been sorted in ascending order based on the Widget compareTo method*. The getWidgetMatch searches for an element* in the array* that matches the first parameter* on the basis of the equals* method* and returns true* if found and false* otherwise.
My answer:
Java Code:public static boolean getWidgetMatch(Widget a, Widget[] b){ int lo=0; int hi=b.length-1; int m; int r; while (lo!=hi){ m=(hi-lo)/2; r=a.compareTo(b[m]); if (r==0) return true; if (r>0) hi=m; else lo=m; } return return a.equals(b[m]); }
CTest.java:14: error: illegal start of expression
***
**⇒*****You almost certainly should be using: +
*****⇒*****We think you might want to consider using: <
*****⇒*****Solutions like yours don't usually use: !=
Thank you!
- 07-25-2013, 01:33 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: MyProgrammingLab Questions
First, your syntax looks off since it appears you are missing some closing braces. Better formatting will help us help you.
Second, it's time to play computer. Grab some paper and pencil and write down two sorted lists and a third one. The third one's size is the combined size of the other two. Assign a starting index to each one and start interating thru each sorted list and compare the current elements. Put the smaller one the third list and increment its pointer. Then continue incrementing, comparing, and merging, adjusting the various indices until you are done.
See if that helps you visualize the problem.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-25-2013, 08:20 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 29
- Rep Power
- 0
Re: MyProgrammingLab Questions
On your first issue I think you are missing an addition sign on line 8.
merged[arr1.length j] = arr2[j]; -- instead should look like...
merged[arr1.length + j] = arr2[j]; -- the compiler wanted you to close the bracket because it didnt understand why j was there.
Also... for your second issue look at line 13 in your code.Last edited by slider57; 07-25-2013 at 08:24 PM.
Similar Threads
-
Two questions!
By eranmc in forum New To JavaReplies: 2Last Post: 11-09-2012, 04:35 PM -
questions
By amaliutz in forum New To JavaReplies: 13Last Post: 02-02-2011, 07:13 PM -
questions regarding XML
By Sparky in forum New To JavaReplies: 4Last Post: 02-01-2011, 08:10 PM -
I have 2 questions =P
By santa in forum New To JavaReplies: 4Last Post: 01-19-2011, 06:35 PM -
Questions About JSP?
By mtz1406 in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 08-19-2008, 08:56 PM
Bookmarks