943,516 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3323
  • Java RSS
Aug 11th, 2004
0

How would i recode as a function?

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
srthomso is offline Offline
11 posts
since Aug 2004
Aug 11th, 2004
0

Re: How would i recode as a function?

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]
Java Syntax (Toggle Plain Text)
  1. boolean contains(int[] a, int numToFind) {
  2. for (int i = 0; i < a.length; i++) {
  3. if (a[i] == numToFind)
  4. return true;
  5. }
  6. return false;
  7. }


Ed
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004
Aug 11th, 2004
0

Re: How would i recode as a function?

Oh I left this out.

If you are calling this function from within main(...),
you'd best label the function static.

Java Syntax (Toggle Plain Text)
  1. static boolean contains(int[] a, int numToFind) {
  2. ....

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("");
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004
Aug 11th, 2004
0

Re: How would i recode as a function?

I'm not sure I quite understand what you have done here:-S
Reputation Points: 10
Solved Threads: 0
Newbie Poster
srthomso is offline Offline
11 posts
since Aug 2004
Aug 12th, 2004
0

Re: How would i recode as a function?

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?

Java Syntax (Toggle Plain Text)
  1. for (int i = 0; i < N; i++) {
  2. //... loop contents here
  3. // first loop iteration i = 0
  4. // second loop iteration i = 1
  5. // ...
  6. // ends with i = N-1
  7. // for a total of N times.
  8. }

a for loop like this will execute for N times. This is equivalent to
Java Syntax (Toggle Plain Text)
  1. int i = 0;
  2. while (i < N) {
  3. //.... loop contents here
  4. i++;
  5. }

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)
  1. public static boolean function(int[] A) {
  2.  
  3. for (int i=0; i<A.length; i++) {
  4. // loop will execute A.length times
  5. System.out.println(A[i]);
  6. }
  7. }


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)
  1. public static boolean contents(int[] A, int valueToFind) {
  2.  
  3. for (int i=0; i<A.length; i++) {
  4. // loop will execute A.length times
  5. if (A[i] == valueToFind) {
  6. // Hark! Mission accomplished we have found our value.
  7. // therefore, we return true to tell the function that called
  8. // this one that YES we have found a value
  9. // The function immediately returns to the place you called it.
  10. return true;
  11. }
  12. }
  13.  
  14. // We ONLY get to this spot in the code if we have gone through all
  15. // the elements of array A, and none of them were the value
  16. // we wanted. Mission failed. Tell caller that we did not find value.
  17. return false;
  18. }


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
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004
Aug 12th, 2004
0

Re: How would i recode as a function?

Cheers m8 u're a diamond! that certainly has helped! i'm thinkin of doin a java course, so any little helpful hinters are great!
Thanks again.
Steve
Reputation Points: 10
Solved Threads: 0
Newbie Poster
srthomso is offline Offline
11 posts
since Aug 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Mock exam help?
Next Thread in Java Forum Timeline: changing colours





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC