Question

In: Computer Science

Given the following processing array declaration with initialisation: int[][] foo = {{1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7},{4,5,6,7,8}}; Write a loop that...

Given the following processing array declaration with initialisation:

int[][] foo = {{1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7},{4,5,6,7,8}};

Write a loop that will modify  foo so it instead contains the values {{1,4,9,16,25},{4,9,16,25,36},{9,16,25,32,49},{16,25,32,49,64}}.

Solutions

Expert Solution

Solution:

public class Main
{
   public static void main(String[] args) {
   int[][] foo = {{1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7},{4,5,6,7,8}};
int [][]zoo={{1,4,9,16,25},{4,9,16,25,36},{9,16,25,32,49},{16,25,32,49,64}};
//updating the values of the array foo
for(int i=0;i<4;i++)
{
for(int j=0;j<5;j++)
{
foo[i][j]=zoo[i][j];
}
}
//printing the values of the array
for(int i=0;i<4;i++)
{
for(int j=0;j<5;j++)
{
System.out.print(foo[i][j]);
System.out.print(" ");
  
}
System.out.println();
}

      
   }
}

Note: If you find improper indentation while copying the code then please provide proper indentation with the help of attached image. Also please save your file with same name as class name

If you find my answer helpful,please give thumbs up,Thank you


Related Solutions

Given the following variable declarations: const size_t n = 50; Write the declaration of an array...
Given the following variable declarations: const size_t n = 50; Write the declaration of an array of pointers to n memory blocks containing 16-bit signed integer values. Use 'x' for the name of the variable. (Don't forget the semi-colon at the end of the declaration!)
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
Given the following Java code: class C { public int foo(C p) { return 1; }...
Given the following Java code: class C { public int foo(C p) { return 1; } } class D extends C { public int foo(C p) { return 2; } public int foo(D p) { return 3; } } C p = new C(); C q = new D(); D r = new D(); int i = p.foo(r); int j = q.foo(q); int k = q.foo(r); (Remember that in Java every object is accessed through a pointer and that methods...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int length, int width)
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Write a declaration to store the following values in an array rates : 12.9, 28.6, 11.4,...
Write a declaration to store the following values in an array rates : 12.9, 28.6, 11.4, 13.7, 9.5, 15.2, and 17.6. Include the declaration in a program that displays the values in the array by using pointer offset notation
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions:...
class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions: Print to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //code here } public static void (int[] array) { int count = 0; for(int i = 0; i < array.length; i++) { for(int j = i; j < array.length; j++) { for(int k = j; k < array.length; k++)...
Write a static method remove(int v, int[] in) that will return a new array of the...
Write a static method remove(int v, int[] in) that will return a new array of the integers in the given array, but with the value v removed. For example, if v is 3 and in contains 0, 1, 3, 2, 3, 0, 3, and 1, the method will return an array containing 0, 1, 2, 0, and 1. Hint: You can follow two steps to solve this problem: Create an array in the method, let say you called it result....
Given a 2D array a, sum up ALL the edges of the array. Ex. int a[...
Given a 2D array a, sum up ALL the edges of the array. Ex. int a[ ][ ] = { {1, 2, 3, 4},                        {5, 6, 7, 8},                        {9, 10, 11, 12} }; OUTPUT: Sum of the edges = 65
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT