Advertisement
Guest User

Stupid Question

a guest
Jan 8th, 2015
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. // precondition: 0 <= pos < A.length
  2. // postcondition: returns the smallest index k such that
  3. //                        (pos <= k A.Length) and (A[k] == 0),
  4. //                        or -1 if there is no such index
  5.  
  6. public int findZero(int[] A, int pos){
  7.  
  8. //initalize stuff
  9. int length = A.length;
  10. boolean zero = false;
  11. //check array for zero(s)
  12. for(int x = 0; x <= length; x++){
  13. if(A[x] == 0){
  14. zero = true;
  15. }
  16. }
  17. //if zero(s) are present, then continue
  18. //otherwise, return -1
  19. if(zero == false){
  20. return -1;
  21. }
  22. //start at pos, search array for zeros
  23. for(int x = pos; x <= length; x++){
  24. if(A[x] == 0){
  25. //when it finds the first zero after pos, return the index
  26. return x;
  27. }
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement