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 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}}.
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