The source code to read and print the two-dimensional array is given below. The given program is compiled and executed successfully.
# Ruby program to read and print
# two-dimensional array
TwoDArr = Array.new(2){Array.new(2)}
printf "Enter elements of MATRIX:\n";
i = 0;
while (i < 2)
j = 0;
while (j < 2)
printf "ELEMENT [%d][%d]: ", i, j;
TwoDArr[i][j] = gets.chomp.to_i;
j = j + 1;
end
i = i + 1;
end
printf "MATRIX:\n";
i = 0;
while (i < 2)
j = 0;
while (j < 2)
print TwoDArr[i][j]," ";
j = j + 1;
end
i = i + 1;
print "\n";
end
Output:
Enter elements of MATRIX:
ELEMENT [0][0]: 1
ELEMENT [0][1]: 2
ELEMENT [1][0]: 3
ELEMENT [1][1]: 4
MATRIX:
1 2
3 4
Explanation:
In the above program, we created a two-dimensional array TwoDArr. Then we read the elements of the 2D array. After that, we printed the 2D array.
Program/Source Code:
The source code to read and print the two-dimensional array is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created a two-dimensional array TwoDArr. Then we read the elements of the 2D array. After that, we printed the 2D array.
need an explanation for this answer? contact us directly to get an explanation for this answer