Arrays

  1. Write a fragment of code that creates an array named array of length 10 that holds 10 integers and initializes the array with random numbers between 1 and 10 (inclusively).
  2. Assume there is an array named array of length 10 that holds 10 integers. Write a fragment of code that sets the elements in the array with even indices to 0.
  3. Assume there is an array named array of length 10 that holds 10 integers. Write a fragment of code that sets the elements in the array with values greater than 5 to 0.
  4. Assume there is an array named array of length 10 that holds 10 integers. Write a fragment of code that creates a second array named array2 of length 10 and initializes array2 with the same values that are in array and in the same order.
  5. Assume there is an array named array of length 10 that holds 10 integers. Write a fragment of code that prints the values that are in array in reverse order.
  6. Write a program that creates an array named array of size 10 and initializes it with values read in from the keyboard. The program then passes the array to a function named sumArray and prints the value returned by the function. The sumArray function has one parameter and returns the sum of the values in the array that is passed into the function.
  7. Write a program that asks the user for an integer, passes the integer to a method named createArray, and prints the contents of the array that is returned by createArray. The function named createArray has one integer parameter and creates an array with a length equal to the value of the argument passed into the function.  The function should initializes the array with the values {1, 2, …, n} and return the array.
  8. Write a program that asks the user to enter an integer. The program then reads in the integer and creates an uninitialized array of size equal to the value read.  The program passes the uninitialized array to a method named initArray then prints the contents of the array after initArray returns.  The method initArray takes an array of integers as an argument and has a void return type.  The function populates the array that is passed into the function with random values between 0 and 9.
  9. Write a function named initArray that has a variable number of integers as its parameter.  The method creates an array of length equal to the number of integers passed into the function and populates the array with the values passed into the method.  The function should return a reference to the array that was created.
  10. Write a function named initArray2 that takes an array of integers as the first parameter and a variable number of integers as the second parameter. The function copies the values in the second parameter into the array, so long as there is room in the array (starting at index 0). The function returns the number of elements that were copied into the array.
  11. Write a fragment of code that creates a 3 x 3 2D array of characters named matrix with the following values:
c a t
a t e
t e a
  1. Write a fragment of code that creates a 3 x 3 2D array of integers named matrix3 and that using nested for-loops initializes the 2D array with following values:
1 0 0
0 1 0
0 0 1
  1. Assume there is a 3 x 3 2D array of characters name matrix. Write a fragment of code that creates a 3 x 3 2D array named matrix2 and populates matrix2 with the values in matrix (in the same order).
  2. Assume there is an n x m 2D array of integers name matrix3. Write a fragment of code that creates an n x m 2D array named matrix4 and populates matrix4 with the values in matrix3 (in the same order).

© 2017 – 2020, Eric. All rights reserved.