Extra Credit This is one program; include the following methods: Write a method
ID: 3535743 • Letter: E
Question
Extra Credit This is one program; include the following methods: Write a method called create Arithmetic Seq that prompts the user to input 2 numbers, first and diff. The method then creates a 1-D array called A of 16 elements ordered in an arithmetic sequence. For example, if first = 21 and diff = 5, the arithmetic sequence is: 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96 Write a method called matricize that takes a 1-D array of 16 elements and a 2-D array of 4 rows and 4 columns as parameters. This method puts the elements of the 1-D array into the 2-D array. Use the 1-D array A created in part (a.) and B is a 2-D array, then after putting the elements of A into B, the array B is:Explanation / Answer
import java.util.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
int[] A = new int[16];
int[][] B = new int[4][4];
createAirthematicSeq(A);
for(int i=0; i<16; i++)
System.out.print( " " + A[i]);
System.out.println();
matricize(A,B);
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
System.out.print( " " + B[i][j]);
}
System.out.println();
}
}
public static void createAirthematicSeq(int[] A)
{
Scanner in = new Scanner(System.in);
System.out.println("enter first element in airthematicsequene");
int first = in.nextInt();
System.out.println("enter difference");
int diff = in.nextInt();
int count = 0;
for(int i=first;count<16; count++)
{
A[count] = i;
i = i + diff;
}
}
public static void matricize(int[] A, int[][] B)
{
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
{
B[i][j] = A[4*i+j];
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.