4 – Arrays (Level 3)

1 Sample Code

2 Exercises

  1. Print out the elements of the following array: int [] numbers = {1,2,3,4,5};
  2. Declare and initialize a String array containing the days of the week. Print out a random day.
  3. Output the sum of the elements in this array: int [] values = {3,5,4,7,2,3};
  4. Output the average of the elements in this array: int [] values = {3,4,5,6};
  5. Declare an int array of length 5. Use a for loop to prompt for 5 numbers and enter them into the array. Print out the array.
  6. Declare an int array of length 4. Use a for loop to prompt for 4 numbers and enter them into the array. Print out the average of the four numbers
  7. The following code will convert a String s to an array of characters c. Print out the characters: String s = “This is a string”; char [] c = s.toCharArray();
  8. Use what you learned in the last question to count the number of times the letter e occurs in the String “I never saw a purple cow”

Leave a Reply

Your email address will not be published. Required fields are marked *