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;...
3. Write a program which divides a given array (int array []) into two parts: -...
3. Write a program which divides a given array (int array []) into two parts: - Left part with all elements with values <= (less than or equal) pivot (int pivot) and the - Right part with all elements with values > (larger than) pivot. You should ask the user to enter the values of: int pivot, int size and the array
C Practice 1: 1) Write a forward declaration for the following C function int triple_it (int...
C Practice 1: 1) Write a forward declaration for the following C function int triple_it (int x) { return (x * 3); } 2) What is C syntax to declare two variables, one called num of integer type and another called farray which is an array of 10 floating point numbers? 3) Write a C function array_max(int a[], int len) that takes an integer array & its length as its parameters and returns the largest value in the array. 4)...
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++)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT