University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NegativeTall.java 702B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. class NegativeTall {
  2. public static void main(String[] args) {
  3. int[] ints = {1, 4, 5, -2, -4, 6, 10, 3, -2};
  4. int i;
  5. /*
  6. * A
  7. */
  8. //Count the number of negative ints
  9. i = 0;
  10. int numNegative = 0;
  11. while (i < ints.length) {
  12. if (ints[i] < 0)
  13. numNegative += 1;
  14. i += 1;
  15. }
  16. System.out.println("Number of negative ints: "+numNegative);
  17. /*
  18. * B
  19. */
  20. //Replace negative ints with their index
  21. i = 0;
  22. while (i < ints.length) {
  23. if (ints[i] < 0)
  24. ints[i] = i;
  25. i += 1;
  26. }
  27. /*
  28. * C
  29. */
  30. //Output the ints in the array
  31. System.out.println("Elements in the array:");
  32. i = 0;
  33. while (i < ints.length) {
  34. System.out.println(ints[i]);
  35. i += 1;
  36. }
  37. }
  38. }