Array exercises Download

Charlie Lunetta

basic math, arrays Student approved


Introduction

Complete each of these tasks to increase your comfort level and skill in using arrays. The tasks below tell you what your code should do.


Task

  1. Write a method that starts with an array A = [2, 4, 6, 8, 10] and computes an array B that holds the squares of the elements in array A (so B = [4, 16, .. ].)
  2. Write a method that starts with an array A = [-2, 12, 0, 34, 43] and an array B that holds values such that the sum of indexed elements is 25. Your program should compute these values and place them in B.
  3. Write a method that uses three arrays, A = [-1, 5, 0, 31, 12]; B = [0, 2, -4, 21, 4]; and C such that C holds the difference between indexed elements of A and B. Your program should calculate these values and place them in C.


Starter code

class Arrays{
	static void squareArray(){
		int[] A = {2, 4, 6, 8, 10};
		int[] B;
	}

	static void sumArary(){
		int[] A = {-2, 12, 0, 34, 43};
		int[] B;
	}

	static void diffArray(){
		int[] A = {-1, 5, 0, 31, 12};
		int[] B = {0, 2, -4, 21, 4};
		int[] C;
	}
}

Useful tools to solve the problem