Title is slightly confusing... Sorry.
I have to answer a question, and I started it, but I'm not 100% sure of what is being asked. Perhaps one of you understand it.
Here is the question:
Write the code for populating a 2-D array of ints that models a multiplication table. The array should have 12 rows and 12 columns. Each entry in the ‘table’ should be the product of the row*col -- e.g. The element at arr[2][3] = 6 and the element at arr[0][11] = 0.
Here is what I have so far, but I don't know how to continue:
int arr[][] = new int[12][12];
int mult;
for(int row = 0; row < arr.length; row++){
for(int col = 0; col < arr[1].length; col++){
}
}
basically a 2d array can store value (for a tutorial click here). What you want to do is save the value in the block it self.
int arr[][] = new int[12][12];
for(int row = 0; row < arr.length; row++){
for(int col = 0; col < arr[1].length; col++){
arr[row][col]=row*col;
}
}
Then use a double for loop to see the output like:
for(int row = 0; row < arr.length; row++){
for(int col = 0; col < arr[1].length; col++){
System.out.print(row +" * " col + " = "+arr[row][col]+" ");
}
System.out.println();
}
Related
So i have to make this program and I was able to make the program display the array with the row averages but I am finding it difficult to also display the column averages. My problems are that: 1. I cannot figure out how to add the column value similar to how I did with the row values. 2. I am also having trouble displaying the average column values below the table. Can anybody help?
public class ColRowAvg
{
public static void main (String args[])
{
int avg = 0;
int[][] arr = {{6,3,4,2}, {3,4,5,7}, {3,6,7,2}, {3, 5, 2, 7}};
for (int row = 0; row < arr.length; row++)//Cycles through rows
{
for (int col = 0; col < arr[row].length; col++)//Cycles through columns
{
System.out.printf("%5d", arr[row][col]);
avg += arr[row][col];
}
System.out.printf(" |%2d", avg/arr.length);
System.out.println(); //Makes a new row
avg = 0;
}
System.out.println();
for (int col = 0; col < arr.length; col++)//Cycles through rows
{
for (int row = 0; row < arr[col].length; row++)//Cycles through columns
{
System.out.printf("%5d", arr[row][col]);
avg += arr[row][col];
}
System.out.printf(" |%2d", avg/arr.length);
System.out.println(); //Makes a new row
avg = 0;
}
}
}
This is what I have right now.
Just swap row and col in nested for loop
I am using java and I would like to print a 2d array horizontally multiple time based on user input. However, my array prints vertically, can anyone help?
n=3; //user input
char[][] board = new char[2][3];
char[][] f = new char[board.length][n * board[0].length];
for (int i = 1; i < n + 1; i++) {
int Start = (i * board[0].length) - board[0].length;
int End = i * board[0].length;
for (int row = 0; row < f.length; row++) {
for (int col = nStart; col < nEnd; col++) {
f[row][col] = board[row][col - nStart];
System.out.print(f[row][col]);
}
System.out.println();
}
}
For example board array =
xx
xx
I would like
xxxxxx
xxxxxx
If you want to print 2d array horizontally, you have to repeat printing row n times before next row:
int n = 3; // user input
char[][] board = new char[][] { { 'x', 'x', 'x' }, { '0', '0', '0' } }; //example board
for (int row = 0; row < board.length; row++)
{
for (int i = 0; i < n; i++)
{
for (int col = 0; col < board[row].length; col++)
{
System.out.print(board[row][col]);
}
System.out.print("\t"); //arrays separated by tab
}
System.out.println();
}
Output:
xxx xxx xxx
000 000 000
I hope this help.
Your solution works fine except that you made a little mistake in the last lines of your code. I think you want to print a space between every entry by using System.out.println() but println prints a line-break at the end. So your code should look like this:
n=3; //user input
char[][] board = new char[2][3];
char[][] f = new char[board.length][n * board[0].length];
for (int i = 1; i < n + 1; i++) {
int Start = (i * board[0].length) - board[0].length;
int End = i * board[0].length;
for (int row = 0; row < f.length; row++) {
for (int col = nStart; col < nEnd; col++) {
f[row][col] = board[row][col - nStart];
System.out.print(f[row][col]);
}
System.out.print(" "); // Print a space between every single output
}
}
Or if you dont wan't a space at all remove the line completely. Or change the space to a comma, point or whatever you need.
Removing the copied array f.
What you need is exchanging row,col in loop.
n=3; //user input
char[][] board = new char[2][3];
for (int j = 0; j < board[0].length; j++) {
for (int i = 0; i < n ; i++) {
System.out.print(board[i][j]);
System.out.print(" ");
}
System.out.println();
}
I'm having a strange issue trying to print a 9x9 grid of integers. When I try this code in the main method:
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 0; col++) {
System.out.format( "%d ", entries[row][col] );
}
System.out.print("\n");
}
The output is just a bunch of spaces and newlines, without the actual integers. I've tested 'entries' to make sure it actually contains the correct values (and it does). The weird thing is, when I try the following code also in the main method:
System.out.format("%d ", entries[0][0]);
It works. For some reason the for loop is messing up the output. Any ideas?
You did a mistake in the inner for loop:
for (int col = 0; col < 0; col++)
This wont do any iteration because zero is equals zero.
I think this is what you want:
for (int col = 0; col < 9; col++)
Problem is with the condition in second for loop
for (int col = 0; col < 0; col++) {
It is always false and hence never gets executed.
You should instead use:
for (int col = 0; col < 9; col++) {
This condition is never met:
for (int col = 0; col < 0; col++) {
so you can just simplify it by doing:
for (int row = 0; row < 9; row++) {
System.out.format( "%d ", entries[row][0] );
System.out.print("\n");
}
To print a two-dimensional array , you have to print each element in the array using a loop like the following:
int[][] matrix = new int[9][9];//9*9 grid container
for(int row=0 ; row < matrix.length ;row++){
for(int column= 0 ; column < matrix[row].length;column++){
System.out.print(matrix[row][column] + "");
}
System.out.println();
}
i was wondering how i could change my current code which at the moment will sum up only complete columns and the rest it would catch the exception and not print it. I want it to add up the columns no matter how many numbers are there. For example
1 2 3 4
1 2 3
1 2
1
I would want it to add those up meaning 4, 6, 6, 4 does anyone know how to do so?
public static void sumByCol(int[][] matrix){
try {
for(int col = 0; col < matrix[0].length; col++){
int total = 0;
// hol a column constant and loop over each row
for(int row = 0; row < matrix.length; row++){
total += matrix[row][col];
}
System.out.println("Sum for col " + col + " is " + total);
}
}
catch (ArrayIndexOutOfBoundsException e){
}
}
}
To make it work with try-catch, you'd have to put it inside the nested loop. But it's not a very nice solution.
Much better would be to simply check if the array contains the index you're going to access, that is
for(int row = 0; row < matrix.length; row++)
if (col < matrix[row].length)
total += matrix[row][col];
To fix the problem when your first row doesn't contain the maximum number of columns, you need to find the max number first, e.g.
int maxColumns = 0;
for (int i = 0; i < matrix.length; i++)
if (matrix[i].length > maxColumns)
maxColumns = matrix[i].length;
And then use maxColumns in your second loop to iterate through columns 0..maxColumns, i.e.
for(int col = 0; col < maxColumns ; col++){
Here is the code to use:
int[][] matrix = new int[][]{{1,2,3,4},{1,2,3},{1,2},{1}};
String end = "";
boolean check = false;
int j = 0;
do{
check = false;
int total = 0;
for (int i = 0; i < matrix.length; i++){
if (j+1 < matrix[i].length){
check = true;
}
if (j < matrix[i].length){
total += matrix[i][j];
}
}
end += total + " ";
j++;
}while (check);
What this does is it takes the value of matrix[0][0], matrix[1][0], matrix[2][0]... etc. and adds them, stores it in a String 'end' with a space, and then moves on to the next column.
No 'try-catch' is needed, as it will never go outside of the array. The first 'if' statement will keep the 'do-while' loop running as long as there is another column.
The idea is similar to finding the sum of each row. However to find the sum of each column, we need to know the maximum number of columns in your jagged 2D array:
int maxCol = 0;
for(int x=0; x<nums.length; x++)
if(nums[x].length > maxCol)
maxCol = nums[x].length;
After knowing the maximum number of columns, a pair of nested loops will be enough to sum the columns. I used an if statement to ensure I stay within array bounds:
for(int y=0; y<maxCol; y++){
int sum = 0;
for(int x=0; x<nums.length; x++)
if(y < nums[x].length)
sum += nums[x][y];
System.out.println("Sum of col " + y + ": " + sum);
}
Code Testing 1:
int[][] nums = {{1,2,3,4},
{1,2,3},
{1,2},
{1}};
Output:
Sum of col 0: 4
Sum of col 1: 6
Sum of col 2: 6
Sum of col 3: 4
Code Testing 2:
int[][] nums = {{1},
{1,2},
{1,2,3},
{1,2,3,4,5,6,7}};
Output:
Sum of col 0: 4
Sum of col 1: 6
Sum of col 2: 6
Sum of col 3: 4
Sum of col 4: 5
Sum of col 5: 6
Sum of col 6: 7
I have a 2D Array (inputMatrix with x rows and y columns) and need to subtract all vectors(rows) with eachother.
Here's the input:
1,5
3,7
2,3
6,5
4,7
The output(a distance matrix) should look like:
subtraction[0][0] = {0,0} // first row - first row
subtraction[0][1] = {-2,-2} // first row - 2nd row -> {1,5}-{3,7}=-2,2
subtraction[0][2] = {-1,2} // first row - 3rd row
...
subtraction[4][2] = {2,4}
subtraction[4][3] = {-2,2}
subtraction[4][4] = {0,0}
However i'm having a problem on storing the values, since subtraction[row][col] values are being overwritten on the "col for-loop". Also a note, each subtraction index is getting as output another array.
for(int row = 0; row < inputMatrix.length; row++){
for(int col = 0; col < inputMatrix[0].length; col++){
subtraction[row][col] = inputMatrix[0][row] - inputMatrix[row][col];
System.out.print(subtraction[row][col] + " ");
}
System.out.print("\n");
}
Based on your description of the output, you need a 3-dimensional array to store the output, since for each pair of input rows, you are producing an output row.
for(int row1 = 0; row1 < inputMatrix.length; row1++){
for(int row2 = 0; row2 < inputMatrix.length; row2++){
for(int col = 0; col < inputMatrix[0].length; col++){
subtraction[row1][row2][col] = inputMatrix[row1][col] - inputMatrix[row2][col];
}
}
}
If you must have a two dimensional output, you can flatten the output :
int outputRow = 0;
for(int row1 = 0; row1 < inputMatrix.length; row1++){
for(int row2 = 0; row2 < inputMatrix.length; row2++){
for(int col = 0; col < inputMatrix[0].length; col++){
subtraction[outputRow][col] = inputMatrix[row1][col] - inputMatrix[row2][col];
}
outputRow++;
}
}
subtraction[row][col] = inputMatrix[0][row] - inputMatrix[**row**][**col**];
You've mixed up your indices here.