In: Computer Science
Multi-Dimensional Arrays
Create main class named MultiArray. Create a method which outputs (prints) all the values in the array. Call this method from each of the other methods to illustrate the data in the array. Note: Be sure to populate, and update the values in the multi-array, and then print the contents of the multi-array (Don't just print the design patterns.) The goal is to practice navigating the multi-array.
Declare a multi-dimensional array of integers, which is 10 rows of 10 columns ( 10 by 10).
In your code, set the initial values to ones’ such as
this:
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
Create a method named “patternRow.” Using control loops, (if , then / then / else ) set the data values to 1’s and 0’s, alternating by rows, such as this:
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
Create a method named “patternCheckered.” This method should set the values to alternative 1’s and 0’s, such as this:
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
Create a method named “userChoice.” Prompt the user to enter three numbers. Set the array data to be the sequence of numbers they enter, such as this:
Please enter your choice: 1
Please enter your choice: 7
Please enter your choice: 5
1 7 5 1 7 5 1 7 5 1
7 5 1 7 5 1 7 5 1 7 …
Create a method named “sumArray” which sums all the numbers together. Output totals for each row, and a grand total.
Copyable code:
// import required
packages
import java.util.Scanner;
// define a class MultiArray
public class MultiArray
{
// declare the global array variable and initialize
the values
public static int[][] multary=
{{1,1,1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1,1,1}};
// main method to perfom the operations in array
public static void main(String[] args)
{
System.out.println("Initial values
of MultiArray:");
// call the method and display the
initial values
showValue();
// call the method and perform the
patternRow operation
patternRow();
// call the method and perform the
patternCheckered operation
patternCheckered();
// call the method and perform the
userChoice operation
userChoice();
System.out.println("Sum of each row
and Grand total:");
// call the method and compute sum
of each row and grand total
sumArray();
}
// method definition to display the array values
public static void showValue()
{
int lp1,lp2;
// loop to iterate the array
for(lp1=0;lp1<10;lp1++)
{
for(lp2=0;lp2<10;lp2++)
{
// code to display the values
System.out.print(multary[lp1][lp2]+" ");
}
System.out.println();
}
}
// method to set the data values to 1’s and 0’s in
alternative rows
public static void patternRow()
{
int lp1,lp2;
// loop to iterate the array
for(lp1=0;lp1<10;lp1++)
{
for
(lp2=0;lp2<10;lp2++)
{
// code to fil the array with 1's and 0's
inalternative rows
if(lp1%2 == 0)
{
multary[lp1][lp2] = 0;
}
}
}
System.out.println("Values of
MultiArray after patternRow:");
showValue();
}
// method to set the data values alternatively 1’s and
0’s
public static void patternCheckered()
{
int lp1,lp2,fill=0,fill1=0;
//loop to iterate the array
for(lp1=0;lp1<10;lp1++)
{
// condition
test to fill the array as patternCheckered
if(multary[lp1][0]== 0)
{
fill=0;
fill1=1;
}
// condition
test to fill the array as patternCheckered
else
if(multary[lp1][0] == 1)
{
fill=1;
fill1=0;
}
// code to fill
the array as patternCheckered
for
(lp2=0;lp2<10;lp2++)
{
if(lp2%2 == 0)
{
multary[lp1][lp2] =
fill;
}
else
{
multary[lp1][lp2] =
fill1;
}
}
}
System.out.println("Values of
MultiArray after patternCheckered:");
showValue();
}
// method to get the values from user and fill it in
array
public static void userChoice()
{
Scanner sn = new
Scanner(System.in);
int tmp1, tmp2, tmp3,cnt=1;
// code to get the user choice
values
System.out.println("Please enter
your choice:");
tmp1 = sn.nextInt();
System.out.println("Please enter
your choice:");
tmp2 = sn.nextInt();
System.out.println("Please enter
your choice:");
tmp3 = sn.nextInt();
//loop to fill the user choice
values in array
for(int
lp1=0;lp1<10;lp1++)
{
for(int
lp2=0;lp2<10;lp2++)
{
// check the occurence count and fill the user
choice value1
if(cnt==1)
{
multary[lp1][lp2] =
tmp1;
cnt++;
}
// check the occurence count and fill the user
choice value2
else if(cnt==2)
{
multary[lp1][lp2] =
tmp2;
cnt++;
}
// check the occurence count and fill the user
choice value3
else if(cnt==3)
{
multary[lp1][lp2] =
tmp3;
cnt=1;
}
}
}
System.out.println("Values of
MultiArray after Userchoice:");
showValue();
}
// method to compute sum of row and grand total
public static void sumArray()
{
int grndtot = 0;
int sumrow = 0;
for(int lp1=0;lp1<10
;lp1++)
{
for(int lp2 =
0;lp2<10 ;lp2++)
{
sumrow = sumrow + multary[lp1][lp2];
}
grndtot=grndtot+
sumrow;
System.out.println("Sum of row " + lp1 +" is " + sumrow );
sumrow =
0;
}
System.out.println("Grand total is
"+grndtot);
}
}
Note: please give me a positive rating thank you:)