CS 1713 Introduction to Computer Science -- Spring 2001
Practice Problem on Simple for loop

  1. Write a Java code segment that will search for a zero value in the first 100 locations of an array a of doubles. If a zero is found anywhere in the first 100 locations, your segment should print "Zero found". If there are no zeros in the first 100 locations, it should print "No zeros".

  2. Rewrite the above so that it is a method search that will return the location of any desired element in the array, or a -1 if the element is not present. The function should have parameters a the array, size meaning that the seach occurs from position 0 to position size - 1 and a parameter val which is the value to look for. Then the solution to part 1 just becomes:
    
       int loc = search(a, 100, 0.0);
       if (loc == -1) System.out.println("No zeros");
       else System.out.println("Zero found");