I am learning Java programming and made a program which uses Binary Search to find a symbol in an array of char. However, I have a problem when I try to search for the symbol which is not in an array, my program becomes an endless cycle. I have no idea how to make an error sign if there is no such symbol in an array. Here is the code of my program
import java.lang.reflect.Array;
import java.util.Arrays;
public class Main {
public static void main(String[] args){
char[]arr = {'a','d','f','l','o','z'};
find(arr,'m');
}
public static void find(char[]arr,char ch){
int last = arr.length-1;
int mid=last;
while (arr[mid] != ch){
if (arr[mid]<ch){
mid = (last+mid)/2;
}
else{
last=mid;
mid=last/2;
}
}
System.out.print(mid);
}
}
Thank you in advance.
You have no case to break out of the while loop and the changed values assigned to the variables during the 2 if cases are incorrect. Plus, you should also check for when the element is found in the if-else ladder and use a variable (found) to indicate whether the element is found. Moreover, it'll be better if you use 3 variables for the lower bound (first), mid and upper bound (last). The code below is a revision of the find() method.
public static void find(char[]arr,char ch){
int first=0,mid=0,last=arr.length-1,found=0;
while (first<=last){
mid=(last+first)/2;
if(arr[mid]==ch){
System.out.print(ch+" found at index "+mid);
found=1;
break;
}
else if(arr[mid]<ch){
first=mid+1;
}
else if(arr[mid]>ch){
last=mid-1;
}
}
if(found==0)
System.out.print(ch+" was not found ");
}
This works fine and maybe you should use this method.
You have to make some changes in your find() method.First check that the value of mid should never exceed the value of last,so you have to change your while loop condition accordingly. Secondly you have to give a terminating condition if(arr[mid]==ch) to come out of the loop.
This can also be done by using two variables lo & hi ,easy to understand & implement.See the implementation here Binary Search.
Below is the solution using only only last & mid variables,as per your code requirement above:
public static void find(char[]arr,char ch){
int last = arr.length-1;
int mid=last/2;
while (mid<=last){
if(arr[mid] == ch) //if found, Print and exit the loop
{
System.out.println("found at:"+mid);
return;
}
if (arr[mid]<ch){
mid=((last+mid)/2)+1;
}
else{
last=mid-1;
mid=last/2;
}
}
System.out.println("Not found!!");
}
Related
I was trying to implement the coin change problem using recursion. I have written the following code and am facing a problem with the static class variable. 'answer' is a class variable and i am trying to add the return value to it in the loop. This works fine within the while loop but after the while loop ends the answer is reset to 0;
while (i * currentCoin <= sum) {
System.out.println("inside while; answer is " + answer);
answer = answer
+ findCombinations(
sum - i * currentCoin,
new ArrayList<Integer>(denominations.subList(1,
denominations.size())));
i++;
}
Below is all the code that I have written. You can copy and run it to check.
import java.util.ArrayList;
import java.util.Collections;
public class CoinChangeHashMap {
static int answer = 0;
public static void main(String[] args) {
int[] array = new int[] { 7, 3, 2 };
ArrayList<Integer> input = new ArrayList<Integer>();
getList(array, input);
findCombinations(12, input);
System.out.println(answer);
}
private static void getList(int[] array, ArrayList<Integer> input) {
for (int i : array) {
input.add(i);
}
}
public static int findCombinations(int sum, ArrayList<Integer> denominations) {
if (denominations.size() == 1) {
if (sum % denominations.get(0) == 0) {
return 1;
}
return 0;
}
int i = 0;
int currentCoin = denominations.get(0);
while (i * currentCoin <= sum) {
System.out.println("inside while; answer is " + answer);
answer = answer
+ findCombinations(
sum - i * currentCoin,
new ArrayList<Integer>(denominations.subList(1,
denominations.size())));
i++;
}
return 0;
}}
**The output that I get is 0. but the expected output is 4. While debugging the output that I got is **
inside while; answer is 0
inside while; answer is 0
inside while; answer is 1
inside while; answer is 1
inside while; answer is 2
inside while; answer is 2
inside while; answer is 0
inside while; answer is 0
inside while; answer is 0
0
Any Help is appreciated.
The problem is related to your odd code structure, in which you convey the outcome of your recursive call sometimes by modifying static variable answer, and sometimes via the method's return value.
If you analyzed the problem more closely, you would discover that it is not upon exit from the loop that the partial results are lost, but rather some time after return from the method. Therefore, consider carefully the way you update the answer:
answer = answer + findCombinations( /* ... */ );
At the top-most level of your recursion, answer is initially 0. When Java evaluates the above expression, it evaluates first the left operand and then the right operand, then it adds them. That is, it evaluates answer, getting the result 0, before it performs the recursive call. The value of answer may be updated in the course of the recursive call, but those changes come too late. Only the bottom-most level of the recursion ever returns a value different from zero, so if the recursive call itself recurses at least one level deeper then it will return zero. In that case, the sum is computed as 0 + 0, and assigned to answer, clobbering any update the method performed.
You could resolve the problem by swapping the order of the operands in your sum, but it would be better, and not much harder, to get rid of the static variable altogether. Use a local variable within the method to accumulate results, and in all cases convey the total back to the caller via the method's return value.
For the past few months, I switched to programming in a functional language (Racket), and recently restarted coding in Java, so I'm a bit confused regarding a few concepts.
The following (simplified version) code is an implementation of euclid's algorithm. It works just fine. My problem with it is the return statement. Is it possible in java to store the results of a method in a variable? For example,in my code, I initialized the variable result to store the gcd of two numbers. But that returns an incorrect value. However, if I remove the variable result, I get the correct value for the gcd, which brings me to my 2nd question: return statements. I don't quite understand what the return statement is doing here. The only reason I have it in the 1st place was because I was aiming to store the result of the method Recursion in a variable. But as far as I've tried it, and seems to be only messing up my code.
Primary objective: To store the result of the gcd of two numbers in a variable, so I can re-use it elsewhere.
Is there is a way to make this possible?
Any suggestions would be appreciated!
import java.util.*;
public class StoringResults
{
public static void main(String[]args)
{
int big,small,remainder,gcd; //Variables declared.
Scanner sc=new Scanner(System.in);
/* Use enters input */
//Big is the larger number.
//Small is the smaller of the two.
remainder=big%small;
int result=recursion(big,small,remainder);
System.out.println("FINAL RESULT:"+result);
}
//recursive method.
public static int recursion(int big,int small,int remainder)
{
remainder=big%small;
if(remainder==0)
{
System.out.println(small);
}
else
{
int dummyvar=remainder;
big=small;
small=dummyvar;
recursion(big,small,remainder);
}
return remainder;
}
}
As my comment already stated your logic is faulty.
And your statement if I remove the variable result,I get the correct value for the gcd is plain wrong. You get the correct result printed but not returned. And that is caused by the fact that you return the wrong value.
remove the remainder from the method signature since your first statement is assigning something to it
return the correct value: smaller instead of remained
return in the else branch
That will result in the following code:
public static int recursion(int big,int small)
{
int remainder=big%small;
if(remainder==0)
{
System.out.println(small);
}
else
{
big=small;
small=remainder;
return recursion(big,small);
}
return small;
}
Shortening results in
public static int recursion(int big, int small) {
int remainder = big % small;
if(remainder == 0) {
return small;
} else {
return recursion(small,remainder);
}
}
Adding to TDG's answer, your code should be more like this:
//recursive method.
public static int recursion(int big, int small, int remainder) {
remainder = big%small
if (remainder==0) {
System.out.println(small);
return small;
} else {
Int dummyvar = remainder;
big = small;
small = dummyvar;
return recursion(big, small, remainder);
}
}
I am new to programming and wrote this code for recursive binary search, but the output is wrong.
I tried debugging it many times but could not know where I am going wrong.
public class Number {
public static void main (String[] args){
int []a = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
int key = 7;
int mid,low,high;
low=0;
high=a.length-1;
int pos=binarySearch(a,key,low,high);
System.out.println(key +" is found at "+pos+" position");
}
public static int binarySearch(int[]a,int key,int low, int high) {
int mid=(low+high)/2;
if(key<a[mid]){
high=mid-1;
binarySearch(a,key,low,high);
}
else if(key >a[mid]){
low=mid+1;
binarySearch(a,key,low,high);
}
else if(low>high){
return -1;
}
return mid;
}
}
During recursive calls execution of caller is interrupted and his execution frame is pushed onto stack. When callee finishes execution callers frame is retrieved from the stack and his execution proceeds. You assigned 9 to mid and you returned mid without reassigning it. If you try different size arrays you will see that always initial mid is returned and all recursive calls are made for no reason. To debug place one System.out.println("returning "+mid); in front of return statement.
//ignores found value
binarySearch(a,key,low,high);
should be
//returns value
return binarySearch(a,key,low,high);
in both "if" and "else if" clause
You should first check if mid equals key. If it does, escape.
Im a beginner.
Here is a binary search code.Its showing array out of bounds error for main method.
please look into the program and kindly tell me my mistake.ill be grateful for ur service.
i have to write all this crap cause i cant post it as its asking for more details.
public class BinaryS
{
int n;
public BinaryS(int z)
{
n=z;
}
static int pos;
static boolean flag=false;
public void disp()
{
int arr[]={0,1,2,3,4};
int len=arr.length;
int first=0;
int last=len;
int mid=(int)(first+last/2);
//boolean flag=false;
while(mid>=0 && mid<=len)
{
if(n<arr[mid])
{
last=mid;
}
if(n>arr[mid])
{
first=mid;
}
if(n==arr[mid])
{
flag=true;
pos=mid+1;
}
}
if(flag==true){
System.out.println("the no."+n+"is found at"+pos);
}
else{
System.out.println("the no."+n+"is not found ");
}
}
public static void main(String args[])
{
BinaryS obj=new BinaryS(2);
obj.disp();
}
}
Currently your code does compile, and runs forever - because of this loop:
while(mid>=0 && mid<=len)
{
// Code which doesn't modify mid or len
}
Assuming it gets into that loop at all (which it does), the condition is never going to become false - so unless you return or break from within the loop (you don't) or an exception is thrown (it isn't) you're just going to keep going round the loop.
This is where you should:
Use a debugger to observe what's happening
Think about what the condition should actually be and how you want it to become false
Adjust your code to either change the condition, or change the loop body so that it modifies mid or len
This is a question in the book "Cracking the Coding Interview". Here is Java code, but why does it cause the ArrayIndexOutOfBoundsException? I just copied from the book.
class Q1_3{
public static void removeDuplicates(char[] str){
if(str==null) return;
int len=str.length;
if(len<2) return;
int t=1;
for(int i=1;i<len;++i){
int j;
for(j=0;j<t;++j){
if(str[i]==str[j])
break;
}
if(j==t){
str[t]=str[i];
++t;
}
}
str[t]=0; //why ?
}
public static void main(String[] args){
char ss1[] = {'a','b','c','d'};
char ss2[] = {'a','a','a','a'};
char ss3[] = {};
char ss4[] = {'a','a','b','b'};
removeDuplicates(ss1);
removeDuplicates(ss2);
removeDuplicates(ss3);
removeDuplicates(ss4);
System.out.println(ss1);
System.out.println(ss2);
System.out.println(ss3);
System.out.println(ss4);
}
}
It's really weird code, the naming and the use of control structures its... questionable...
The code breaks when there is no duplicated chars, t increases his value in all iterations and at the end is 4, this is what causes de exception.
in the example the code only crash with ss1, and "works" with the others.
If you debug the code closely, you will find that after comparing the value for the last element in the inner for loop you increment the value of t, thus the value of t will be str.length. But, array index start from 0 till str.length-1. So eventually, when you try to insert value at index str.length you will get the exception.