JAVA: ACTUAL QUESTION AT THE BOTTOM Consider a two-by-three integer array t, usi
ID: 3917052 • Letter: J
Question
JAVA: ACTUAL QUESTION AT THE BOTTOM
Consider a two-by-three integer array t, using java code answer the following questions, include screenshots.
a) Write a statement that declares and creates t.
b) How many rows does t have?
c) How many columns does t have?
d) How many elements does t have?
e) Write access expressions for all the elements in row 1 of t.
f) Write access expressions for all the elements in column 2 of t.
g) Write a single statement that sets the element of t in row 0 and column 1 to zero.
h) Write individual statements to initialize each element of t to zero.
i) Write a nested for statement that initializes each element of t to zero.
j) Write a nested for statement that inputs the values for the elements of t from the user.
k) Write a series of statements that determines and displays the smallest value in t.
l) Write a single printf statement that displays the elements of the first row of t.
m) Write a statement that totals the elements of the third column of t. Do not use repetition.
Here is what I have so far,
Explanation / Answer
import java.util.Scanner;
public class Q4 {
public static void main(String args[])
{
int smallestVal;
//task a
int t[][] = new int[2][3];
//B it has 2 rows
//C it has three columns
//D it has 2*3=6 elements
//E
System.out.println("Elements of first row are "+t[1][0] + t[1][1]+ t[1][2]);//hare I am printing to show how to access if not needed then you can these to any variables by accesing those like below
/*int a = t[1][0];
int b = t[1][1];
int c = t[1][2];*/
//F
System.out.println("Elements of first row are "+t[0][1] + t[1][1]);//hare I am printing to show how to access if not needed then you can these to any variables by accesing those like below
/*int a = t[0][1];
int b = t[0][2];*/
//G
t[0][1]=0;
//H
t[0][0]=0;
t[0][1]=0;
t[0][2]=0;
t[1][0]=0;
t[1][1]=0;
t[1][2]=0;
//I
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
t[i][j]=0;
}
//J
Scanner input = new Scanner(System.in);
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
t[i][j]=input.nextInt();
}
//K
smallestVal=t[0][0];
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
if(smallestVal>t[i][j])
smallestVal=t[i][j];
}
}
System.out.println("Smallest Value is "+smallestVal);
//l
System.out.println("Elements of first row of t are "+ t[1][0] + t[1][1] + t[1][2]);
//m
int sum = t[0][2]+t[1][2];//here third column is the column with index 2 since in arrays indexing starts from 0
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.