My professor wants me to print out the matrices side by side with the "+" between the two matrices and then a "=" sign. In the end he wants us to add the matrices together.
This is the work so far.
So the result would come out as:
1 2 3 9 8 7 10 10 10
4 5 6 + 6 5 4 = 10 10 10
7 8 9 3 2 1 10 10 10
enter code here public static void main(String[] args) {
int matrix1[][] = {{1,2,3},{4,5,6},{6,7,8}};
int matrix2[][] = {{9,8,7},{6,5,4},{3,2,1}};
int result1;
int[][] result2 = new int[2][3];
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
System.out.printf(matrix1[i][j] + " ");
System.out.print("");
}
System.out.println("");
}
for (int i = 0; i < matrix2.length; i++) {
for (int j = 0; j < matrix2[0].length; j++) {
System.out.printf(matrix2[i][j] + " ");
}
System.out.println("");
}
}
My problem is, how could I print it side by side with the solutions?
Consider the two printing loops for your matrices:
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
System.out.printf(matrix1[i][j] + " ");
}
System.out.println("");
}
for (int i = 0; i < matrix2.length; i++) {
for (int j = 0; j < matrix2[0].length; j++) {
System.out.printf(matrix2[i][j] + " ");
}
System.out.println("");
}
They print matrix 1, then 2 - and so the matrices will be below each other.
If you want the matrices side by side, you need to print line 1 of every matrix, then - after a new line - line 2 of every matrix, etc. By re-arranging how the loops go through the matrices, you could have your new layout.
You unfortunately cannot print them one at a time, you need to take it row by row. This solution requires both matrix1 and matrix2 to be of equal height. But here's a template that should get you started.
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[i].length; j++) {
}
if (i == matrix1/2) {
} else { //One part of if handles when "+" is needed, other one doesn't
}
for (int j = 0; j < matrix2[i].length; j++) {
}
if (i == matrix1/2) {
}
for (int j = 0; j < ???; j++) {
}
}
Related
for (i = 1; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
The above code is not able to print numbers which are below the left diagonal. For a 3x3 matrix my code is printing:
1 2 3
4 5 6
7 8 9
OUTPUT :
4 5
7 8
Desired output:
4
7 8
You could add an if statment like this :
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
if (i>j) {
System.out.print(a[i][j] + " ");
}
}
System.out.println();
}
Or better you could do :
for(int i=0;i<a.length;i++) {
for(int j=0;i>j;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
A simple way to achieve this depending on the matrix size is
final int matrixSize = 7; // your matrix size
for (int i = 0; i < matrixSize; ++ i) {
for (int j = 0; j < i; ++ j) System.out.print(a[i][j] + " ");
System.out.print("\n");
}
How can I find the total sum for each two dimensional array row? I'm completely stuck...
public static void main(String[] args) {
int [][] grid = new int [10][10];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j] = (int)(Math.random()*99);
}
}
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
System.out.print("1.");
System.out.printf("%5d ", grid[i][j]);
}
System.out.println();
}
}
My current output is:
How can I show the total sum for each row in the end of the row and show column numbers
For the sum of a row, this should do. In a similar way within the i loop, if you need to count the column as well;
for(int i = 0; i < 10; i++) {
int jSum = 0;
for(int j = 0; j < 10; j++) {
jSum += grid[i][j];
System.out.print("1.");
System.out.printf("%5d ", grid[i][j]);
}
System.out.printf(" %5d", jSum);
System.out.println();
}
On the column numbering:
Either you just pust put a static print in the beginning (like print "1 2 3 4..."), or you put the following with the j loop:
if (i == 0) System.out.printf("%5d ", j); // only prints in first loop / row - print 1,2,3,4,5....
I am quite confused in array loops that do have nested ones to print the Two Dimensional array. /it contains a loop without curly braces and second one has just opposite way of representing the braces for loops ...
Since i am learning I have just typed the code and got output.
public class TwoDimensional {
private int i, j, k = 0;
int[][] twod = new int[4][5];
public void DoubleT() {
for (i = 0; i < 4; i++)
for (j = 0; j < 5; j++) {
twod[i][j] = k;
k++;
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++)
System.out.print(twod[i][j] + " ");
System.out.println();
}
}
}
The result it generates is
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
Try this :
public class TwoDimensional {
private int i, j, k = 0;
int[][] twod = new int[4][5];
public void DoubleT() {
for (i = 0; i < 4; i++)
for (j = 0; j < 5; j++) {
twod[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++){
System.out.print(twod[i][j] + " ");
}
System.out.println();
}
}
To properly use the braces always think about the purpose of the loops you have, when do you want them to finish and when do you want them to continue.
In your case, you'll need nested loops for different tasks so you have to properly delimit each one of those tasks.
Fill the the 2D array:
for (i = 0; i < 4; i++)
for (j = 0; j < 5; j++) {
twod[i][j] = k;
k++;
}
}
Print the 2D array values:
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++){
System.out.print(twod[i][j] + " ");
}
System.out.println();
}
Notice that, either for filling or printing the array, your first loop (iterator i) is responsible for the line. It'll stop at I = 3, line number 3. So you'll be in line 0 until you finish the values of all the columns on that line ( [0][0],[0][1],[0][2],[0][4] ) and you just want to go to the second line when your first line is totally filled or printed, and so on. On the print case, you'll need to change the line before the 'i' increments (new line number) and after you have all `'j' values.
To summarize, you'll just want to increment the line ('i') or go to the next line (println()), when your columns ('j') are finished.
I'm trying to fill a matrix vertically, but 1 row is missing. Can you help me ? There is the code. Maybe there is an easier way to fill a matrix verically, but i cant find it.
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of matrix: ");
int n = input.nextInt();
int [][] matrix = new int [n][n];
for (int i = 1; i < matrix.length; i++) {
matrix[0][i] = matrix[0][i -1] + n;
}
for(int i = 1; i < matrix.length; i++){
for (int j = 0; j < matrix.length; j++){
matrix[i][j] = matrix[i -1][j] + 1;
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
input.close();
}
Output:
Enter the value of matrix: 4
1 5 9 13
2 6 10 14
3 7 11 15
Your row is missing because you never printed it in your first loop (the one that is initializing your first line) - you should have a row of 0 4 10 12 at the beginning. But you could do it much easier with only one nested loop.
Try
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of matrix: ");
int n = input.nextInt();
int [][] matrix = new int [n][n];
matrix[0][0]=0; //you have forgotten the first value
for (int i = 1; i < matrix.length; i++) {
matrix[0][i] = matrix[0][i -1] + n;
//initializing the first line
}
for(int i = 1; i < matrix.length; i++){
for (int j = 0; j < matrix.length; j++){
matrix[i][j] = matrix[i -1][j] + 1;
}
// re-loop to display but this time start with i=0
for(int i = 0; i < matrix.length; i++){
for (int j = 0; j < matrix.length; j++){
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
input.close();
}
To fill a matrix vertically, you must loop through columns in the outer loop and through rows in the inner(nested) loop. For example:
for(int j = 0; j < matrix.length; j++) {
for(int i = 0; i < matrix.length; i++) {
matrix[i][j] = /* The value you want to fill */;
.../* Other stuff you wanna do */
}
}
There is an easier way of doing this:
keep a variable like count and iterate the matrix on columns first then rows:
int count = 1; // or 0 if you start with 0
int[][] a = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
a[j][i] = count; // notice j first then i
count++;
}
After that you can easly print out the values:
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
System.out.println(a[i][j]);
I am trying to write a nested for loop that will print out the values of the following code in a specific order:
public static void main(String[] args) {
int[][] array2d = new int[3][5];
for (int i = 0; i < array2d.length; i++) {
for (int j = 0; j < array2d[0].length; j++) {
array2d[i][j] = (i * array2d[0].length) + j + 1;
}
}
for (int x = 0; x <= 4; x++) {
for (int y = 0; y <= 2; y++) {
System.out.println(array2d[y][x]);
}
}
}
}
The current array prints the way I want it, but each printout on a separate line.
I want the output (on a single line) to be this:
1 6 11 2 7 12 3 8 13 4 9 14 5 10 15
Thanks for the help.
You can use System.out.print instead:
System.out.print(array2d[y][x] + " ");
Replace println with print and it should work
String s = "";
for (int i = 0; i < array2d.length; i++) {
for (int j = 0; j < array2d[i].length; j++) {
s += array2d[i][j] + " ";
}
}
System.out.println(s);
public static void main(String[] args) {
int[][] array2d = new int[3][5];
for (int i = 0; i < array2d.length; i++) {
for (int j = 0; j < array2d[0].length; j++) {
array2d[i][j] = (i * array2d[0].length) + j + 1;
}
}
StringBuilder builder = new StringBuilder();
for (int x = 0; x <= 4; x++) {
for (int y = 0; y <= 2; y++) {
builder.append(array2d[y][x]);
if(!(x == 4 && y == 2)){
builder.append(" ");
}
}
}
System.out.println(builder.toString());
}
You basically had it right, except for changing the println to be print and formatting the string how you want. I changed it a little to show how the StringBuilder works. Whenever possible I use a StringBuilder because it is more convenient.