Assume you are given a boolean variable named isSquare and a 2-dimensional array
ID: 3815548 • Letter: A
Question
Assume you are given a boolean variable named isSquare and a 2-dimensional array that has has been created and assigned to a2d. Write some statements that assign true to isSquare if the entire 2-dimensional array is square meaning every row and column has the same number of elements as every other row and column.
Assume you are given an int variable named nPositive and a 2-dimensional array of ints that has been created and assigned to a2d. Write some statements that compute the number of all the elements in the entire 2-dimensional array that are greater than zero and assign the value to nPositive.
Declare a multidimensional array of ints , westboundHollandTunnelTraffic that can store the number of vehicles going westbound through the Holland Tunnel on a particular hour on a particular day on a particular week on a particular year. The innermost dimension should be years, with the next being weeks, and so on.
Explanation / Answer
Question 1:
// consider matrix to be square first.
// if it will not be.. we will make this false.
boolean isSquare = true;
// first take the number of rows in n
// they should be equal to number of columns in each row
int n = a2d.length;
for(int i=0; i<a2d.length; i++){
// if number of columns are not same as number of rows
// then make variable false, and no need to compute further
if(n != a2d[i].length) {
isSquare = false;
break;
}
}
Question 2:
for(int i=0; i<a2d.length; i++){
int nPositive;
for(int j=0; j<a2d[i].length; j++) {
if(a2d[i][j] > 0) {
nPositive += 1;
}
}
}
Question 3:
int westboundHollandTunnelTraffic[year_val][week_val][day_val][hour_val][num_of_vehicles]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.