- Write starting curly braces of main method.
- declare an array of size 5 lets say int[] a; a=new int[5];
- declare an integer variable b.
- Write a caller statement which pass the array a to the user defined method. first choose an appropriate name of method like searcharray.
- b=searcharray(a); when the value will be return by method then b will recieve it.
- write conditional funtions like if and else. if(b==-1) then print "number not found" in body of if and in body of else print "position of number is b".
- write closing curly braces of method main .
- write method havind name searcharray and return type int and should recieve array public static int searcharray(int[] x). x array will recieve the contents of array a.
- write starting curly braces of method searcharray.
- Take input in array x using loop having variable name lets say q which will start from 0 to x.length and increment by 1.
- inside the body of loop write x[q]=EasyIn.getInt();
- Now take a number from user and then we will have to give its position in the array
- declare variable of int type lets say e and w=0;
- start a loop having variable name i start from 0 to x.length and increment by 1.
- Write starting curly braces of this loop
- Write if(x[i]==e){
- within the body of if write w=i+1; and then break;
- write closing curly braces of if
- write closing curly braces of loop
- write return statement which return w . It will return the value to b written before caller.
- write closing curly braces of method searcharray.
This blog is made for my dear class fellows to provide algorithms of different problem statements of JAVA.
Tuesday, 31 January 2012
Searching using method
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment