Iterating over a 2D array
If we want to visit or process every element of a 2D array, we need a nested for loop to do it. Generally, when you iterate over a 2D array, you want to do it in row major order, because this is more memory efficient. The reason why is that adjacent elements in a row are stored contiguously (one after another) in memory. Another reason, for an English speaker, is that this is the same order your read a book—top to bottom, left to right.
The following code iterates over arr2D, changing each value to 100:
for (int r=0; r < arr2D.length; r++)
{
for (int c=0; c < arr2D[r].length; c++) {
arr2D[r][c] = 100;
}
}
I'm using the variables "r" and "c" because they iterate of the rows and columns of the array, respectively. The outer loop iterates over all the rows, and the inner loop iterates over all the columns in a single row. Notice how the row index (r) comes before the column index (c). Also, notice how the loop bounds are handled in the interior loop. While the outer loop goes from 0 to arr2D.length, the inner loop goes from 0 to arr2D[r].length, which is then length of row r.
Q3) What code would you use to add 1 to all the elements of arr2D?
Answer:
for (int r=0; r < arr2D.length; r++)
{
for (int c=0; c
< arr2D[r].length; c++)
{
arr2D[r][c] = arr2D[r][c] + 1;
}
}
Explanation:
Add 1 to each of the elements of arr2D and store back to arr2D. When all the elements are initialized by 100 then after add 1 to each of the elements of arr2D, the elements of the array becomes 101.
Full Program:
class Arr2D
{
public static void main (String[] args)
{
int arr2D[][] = new
int[3][3];
for (int r=0; r < arr2D.length;
r++)
{
for (int c=0; c
< arr2D[r].length; c++)
{
arr2D[r][c] = 100;
}
}
for (int r=0; r < arr2D.length;
r++)
{
for (int c=0; c
< arr2D[r].length; c++)
{
arr2D[r][c] = arr2D[r][c] + 1;
}
}
for (int r=0; r < arr2D.length;
r++)
{
for (int c=0; c
< arr2D[r].length; c++)
{
System.out.print(arr2D[r][c] + " ");
}
System.out.println();
}
}
}
Output:
101 101 101
101 101 101
101 101 101
Iterating over a 2D array If we want to visit or process every element of a...
Write a Java program that does the following. a. Declare an integer 2D array with 5 rows and 5 columns. b. Initialize the array's elements to random integers between 1 and 10 (inclusive). c. Display all the elements in the 2D array as a table of rows and columns. d. Display the row index and column index of all the even integers in the 2D array. e. Display the sum of first row's elements.
Write a Java program which implements both the while and for loop, as needed, to draw the illustrations given below. Shape 1 123456789 12345678 1234567 123456 12345 1234 123 12 1 Shape 2 ************* ************* ************* ************* ************* Shape 3 ============ * * * * * * * * * * ============ Hint: To draw the shapes above, you can use nested for loops to accomplish the task. The outer for loop iterates on each row, and the inner for...
The last element in each array in a 2D array is incorrect. It’s your job to fix each array so that the value 0 is changed to include the correct value. In the first array, the final value should be the length of the first array. In the second array, the final value should be the sum of the first value, and the second to last value in the array. In the third array, the final value should be the...
Write a C program that uses mallloc to create a 2D array. of c columns and r rows. Elements of array must be initialized to 0 and you must use malloc and free allocated memory fo array.
please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class Chapter9a_FillInTheCode { public static void main(String[] args) { String [][] geo = {{"MD", "NY", "NJ", "MA", "CA", "MI", "OR"}, {"Detroit", "Newark", "Boston", "Seattle"}}; // ------> exercise 9a1 // this code prints the element at row at index 1 and column at index 2 // your code goes here System.out.println("Done exercise 9a1.\n"); // ------> exercise 9a2 // this code prints all the states in the...
Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){ int rows = 5; int cols = 5; int x; int** arr = new int[rows][cols] cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x]; return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){ int rows; int cols; int x;...
Write loop(s) that print ALL the content of the following 2D array #include <iostream> using namespace std; int main(){ int rows; int cols; int offset; cin >> rows; cin >> cols; cin >> offset; int** arr = new int*[rows]; for(int i = 0; i < rows; ++i) arr[i] = new int[cols]; for(int i = 0; i < rows; i++) // initialization for(int j = 0; j < cols; j++) arr[i][j] = offset + i; // printing, your code goes here ...
USING C++: Write a function that given a 2D dynamic array of integers, will return the number of elements whose absolute value is smaller than a given value x. The function should take as arguments (in this order): a pointer to the array (i.e., 2D dynamic array) the number of rows the number of columns the given value The function should return the number of elements smaller in absolute value than epsilon E.g. if A={{0.2, -0.1, 3.4},{4.4, 0, -2}} and...
whats the answers
How many total integers elements are in an array with 4 rows and 7 columns? 07 28 11 What is the resulting array contents, assuming that itemList is an array of size 4 having contents -55, -1, 0, 9? for (i = 0; i < 4; ++i) { itemsList[i] - i; -54,0, 1, 10 O 0, 1, 2, 3 O 1, 2, 3, 4 O-55,-1, 0,9 Given an integer array myVals of size N_SIZE (i.e. int[] myVals...
Refer to the following array definition for questions 1 & 2 int numberArray[9)[11] 1. Write a statement that assigns 145 to the first column of the first row of this array. 2. Write a statement that assigns 18 to the last column of the last row of this array. values is a two-dimensional array of floats, with 10 rows and 20 columns. Write code that sums all of the elements in the array and stores the sum in the variable...