Java - Introduction To Programming: 2D Arrays in Java
Java - Introduction To Programming: 2D Arrays in Java
Java - Introduction To Programming: 2D Arrays in Java
Lecture 11
2D Arrays In Java
//input
//rows
for(int i=0; i<rows; i++) {
//columns
for(int j=0; j<cols; j++) {
numbers[i][j] = sc.nextInt();
}
}
Apna College
}
//input
//rows
for(int i=0; i<rows; i++) {
//columns
for(int j=0; j<cols; j++) {
numbers[i][j] = sc.nextInt();
}
}
int x = sc.nextInt();
}
}
Apna College
Homework Problems
1. Print the spiral order matrix as output for a given matrix of numbers.
[Difficult for Beginners]
APPROACH :
Apna College
to column_end and we will increase the row_start with 1 as we have
import java.util.*;
int n = sc.nextInt();
int m = sc.nextInt();
matrix[i][j] = sc.nextInt();
int rowStart = 0;
Apna College
int rowEnd = n-1;
int colStart = 0;
//1
rowStart++;
//2
colEnd--;
//3
rowEnd--;
//4
Apna College
System.out.print(matrix[row][colStart] + " ");
colStart++;
System.out.println();
int n = sc.nextInt();
int m = sc.nextInt();
matrix[i][j] = sc.nextInt();
Apna College
System.out.println("The transpose is : ");
System.out.print(matrix[i][j]+" ");
System.out.println();
Apna College