- 1
Matrix - Multidimensional Arrays (Practice 24.2)
You are given a 3x3 matrix with numbers: int[][] matrix = { {8, 1, 6}, {3, 5, 7}, {4, 9, 0}, }; Output the numbers of the array, each on a new line. *Hint: You need to use two nested for loops to iterate over the array.
6 Answers
+ 6
This is Solution : Don't forget to Upvote
...................................................
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int[,] num = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
//your code goes here
for(int i=0; i<3;i++)
{
for(int j=0;j<3;j++)
{
Console.WriteLine(num[i,j]);
}
}
}
}
}
+ 5
The method these people use is correct, but I wouldn't get into the habit of just using i<3 try using .length as it might help in future uses such as this. It might seem confusing but the matrix.length is calling the entire length of the array, so intead of having to change i<3 to i<4 if you have another array added it will just use the length of the array that is given if that makes any sense. :)
int[][] matrix = {
{8, 1, 6},
{3, 5, 7},
{4, 9, 0},
};
//output the numbers
for (int i=0; i<matrix.length; i++) {
for(int j=0; j<matrix.length; j++ ) {
System.out.println(matrix[i][j]);
}
}
+ 1
Martin Taylor, thanks. I found the mistake of my code. I put double printIn (h).
I completed the tutorial a year before joining pro account, so now only I have access to the Practice.
And Iâm not learning coding full time. Just trying to do a lesson a day and hopefully can gain momentum and get familiar with coding.
+ 1
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{8, 1, 6},
{3, 5, 7},
{4, 9, 0},
};
//Please Subscribe to My Youtube Channel
//Channel Name: Fazal Tuts4U
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
System.out.println(matrix[i][j]);
}
}
}
}
0
My code as below:
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{8, 1, 6},
{3, 5, 7},
{4, 9, 0},
};
//output the numbers
int a = matrix[0][0];
int b = matrix[0][1];
int c = matrix[0][2];
int d = matrix[1][0];
int e = matrix[1][1];
int f = matrix[1][2];
int g = matrix[2][0];
int h = matrix[2][1];
int i = matrix[2][2];
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(h);
System.out.println(h);
System.out.println(i);
}
}
The output seems correct but it says no input.
Iâm doing the long method as Iâm not sure how to do two nested for loops.
0
Chin Eu please post the question