| | |
How would i recode as a function?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2004
Posts: 11
Reputation:
Solved Threads: 0
I have some code which examines an array to see if it contains a particular value:
boolean found = false;
int []a = new int[100];
populate(a); // populates the array a with a set of random values
int valueToFind = 123; // or whatever we want to find
while((n < 100) && (! found)){
if(a[n] == valueToFind){
found = true;
}
else {
n = n + 1;
}
}
However i'm struggling with re-coding this as a function with parameters and/or a return value to be able to work with any size array.
Then how could I create a second function to give me a value stored in a particular location on the array?
Why is programmin so difficult to learn?! lol
Hope some1 can help, thanks
Steve
boolean found = false;
int []a = new int[100];
populate(a); // populates the array a with a set of random values
int valueToFind = 123; // or whatever we want to find
while((n < 100) && (! found)){
if(a[n] == valueToFind){
found = true;
}
else {
n = n + 1;
}
}
However i'm struggling with re-coding this as a function with parameters and/or a return value to be able to work with any size array.
Then how could I create a second function to give me a value stored in a particular location on the array?
Why is programmin so difficult to learn?! lol
Hope some1 can help, thanks
Steve
Hey srthomso,
Programming isn't difficult, you just have to be exposed to code. I'd recommend going to the library or bookstore and obtaining a good book.
That said, let me explain how you can create a function for the job you require. Every array has a length parameter. So a.length in this example is 100. I would recommend using a for loop to iterate this array. You can simply return from the function when you have found the item. I hope you'll find this code neater. Don't fret; after you see a couple code patterns like this you'll get a hang of programming.
[/QUOTE]
Ed
Programming isn't difficult, you just have to be exposed to code. I'd recommend going to the library or bookstore and obtaining a good book.
That said, let me explain how you can create a function for the job you require. Every array has a length parameter. So a.length in this example is 100. I would recommend using a for loop to iterate this array. You can simply return from the function when you have found the item. I hope you'll find this code neater. Don't fret; after you see a couple code patterns like this you'll get a hang of programming.

[/QUOTE]
Java Syntax (Toggle Plain Text)
boolean contains(int[] a, int numToFind) { for (int i = 0; i < a.length; i++) { if (a[i] == numToFind) return true; } return false; }
Ed
Oh I left this out.
If you are calling this function from within main(...),
you'd best label the function static.
call the function like this:
contains(a, 123);
You can even do this:
if (contains(a, 123))
System.out.println("Blah!");
else
System.out.println("
");
If you are calling this function from within main(...),
you'd best label the function static.
Java Syntax (Toggle Plain Text)
static boolean contains(int[] a, int numToFind) { ....
call the function like this:
contains(a, 123);
You can even do this:
if (contains(a, 123))
System.out.println("Blah!");
else
System.out.println("
"); hey srthomso,
I would like to help more, but I need to understand how much you know and go from there. Have you learned about the for construct?
a for loop like this will execute for N times. This is equivalent to
Did you understand what I did in the function?
public static boolean function(int a) // <-- function that takes an integer and returns a boolean (true / false)
So put a loop in the funtion to iterate through all of array A. This function will print out all the values in array A.
Now you want to search for an object in the array right? So a bit more logic to add... we'll add another function parameter that contains the value of the number to find in the array. We'll call this function parameter valueToFind.
Let me know what bit of knowledge you're missing. My advice to you is to simply try playing around with the code. Step through the code in a debugger if you have to. And I strongly encourage you to buy a good book and read up on the language.
Ed
I would like to help more, but I need to understand how much you know and go from there. Have you learned about the for construct?
Java Syntax (Toggle Plain Text)
for (int i = 0; i < N; i++) { //... loop contents here // first loop iteration i = 0 // second loop iteration i = 1 // ... // ends with i = N-1 // for a total of N times. }
a for loop like this will execute for N times. This is equivalent to
Java Syntax (Toggle Plain Text)
int i = 0; while (i < N) { //.... loop contents here i++; }
Did you understand what I did in the function?
public static boolean function(int a) // <-- function that takes an integer and returns a boolean (true / false)
So put a loop in the funtion to iterate through all of array A. This function will print out all the values in array A.
Java Syntax (Toggle Plain Text)
public static boolean function(int[] A) { for (int i=0; i<A.length; i++) { // loop will execute A.length times System.out.println(A[i]); } }
Now you want to search for an object in the array right? So a bit more logic to add... we'll add another function parameter that contains the value of the number to find in the array. We'll call this function parameter valueToFind.
Java Syntax (Toggle Plain Text)
public static boolean contents(int[] A, int valueToFind) { for (int i=0; i<A.length; i++) { // loop will execute A.length times if (A[i] == valueToFind) { // Hark! Mission accomplished we have found our value. // therefore, we return true to tell the function that called // this one that YES we have found a value // The function immediately returns to the place you called it. return true; } } // We ONLY get to this spot in the code if we have gone through all // the elements of array A, and none of them were the value // we wanted. Mission failed. Tell caller that we did not find value. return false; }
Let me know what bit of knowledge you're missing. My advice to you is to simply try playing around with the code. Step through the code in a debugger if you have to. And I strongly encourage you to buy a good book and read up on the language.
Ed
![]() |
Similar Threads
- Trouble with a function (C++)
- Linked List Class - Operator Overloading (C++)
- Function that returns void (C++)
Other Threads in the Java Forum
- Previous Thread: Mock exam help?
- Next Thread: Java project
| Thread Tools | Search this Thread |
3d @param affinetransform android api applet application arc arguments array arrays automation binary bluetooth byte capture chat class classes click client code color compare component count database design detection eclipse eclipsedevelopment encryption error event exception fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer j2me java java.xls javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object os pong print problem producer program programming project projectideas read recursion replaysolutions rim scanner screen server set size sms sort sql string swing terminal threads time transforms tree ui web windows





