Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Everything is good, but i just cant get the addition to show up? When I run the program it is blank when it comes to the addition of the matrixes part. Thanks in advance. BtW does anyone know how I would make this display right column justified?
public static void displayMatrixes(int[][] matrix1, int[][] matrix2, int[][] resultsMatrix) {
System.out.println("This is how i want it to output");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(matrix1[i][j] + " ");
}
System.out.print("+ ");
for (int j = 0; j < 3; j++) {
System.out.print(matrix2[i][j]+ " ");
}
System.out.print("= ");
for (int j = 0; j < 3; j++) {
System.out.print(resultsMatrix[i][j]+ " ");
}
System.out.println();
}
)
This is my code
import java.util.Scanner;
public class MatrixAdd
{
public static void main(String arg[])
{
Scanner input = new Scanner(System.in);
int a[][]= new int[3][3];
int b[][] = new int[3][3];
int row, column;
System.out.println("\nEnter Matrix A: \n");
for (int i = 0 ; i < 3 ; i++){
for (int j = 0; j<3 ; j++){
a[i][j] = input.nextInt();
}
}
System.out.println("\nEnter Matrix B: \n");
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
b[i][j] = input.nextInt();
}
}
System.out.println("\nMatrix A + Matrix B = Matrix C: \n");
int[][] resultingMatrix = addMatrix(a, b);
}
public static int[][] addMatrix(int[][] a, int[][] b){
int[][] result = new int[a.length][a[0].length];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++){
result[i][j]=a[i][j] + b[i][j];
}
}
for (int i = 0; i < a.length; i++) {
char plus = '+';
for (int j = 0; j < a[0].length; j++) {
System.out.print(" " + a[i][j]);
}
if (i == a.length / 2)
System.out.print(" " + plus + " ");
else {
System.out.print(" ");
}
for (int j = 0; j < b[0].length; j++) {
System.out.print(" " + b[i][j]);
}
if (i == a.length / 2)
System.out.print(" = ");
else {
System.out.print(" ");
}
for (int j = 0; j < result[0].length; j++) {
System.out.print(" " + " " + result[i][j]);
}
System.out.println();
}
return result;
}//end of add matrices
}//end of class
I doubt that the given program compiles. In this line: int[][] resultsMatrix = displayMatrixes(a, b); you are expecting an int[][], but in your method displayMatrixes you are not returning anything. You are also expecting a 3rd parameter which you are not passing.
Also, the displayMatrixes method has no return value, which since you are returning something at the end, you must have. Try it again like so:
public static void main(String arg[]) {
Scanner input = new Scanner(System.in);
int a[][] = new int[3][3];
int b[][] = new int[3][3];
int row, column;
System.out.println("\nEnter Matrix A: \n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
a[i][j] = input.nextInt();
}
}
System.out.println("\nEnter Matrix B: \n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
b[i][j] = input.nextInt();
}
}
System.out.println("\nMatrix A + Matrix B = Matrix C: \n");
displayMatrixes(a, b);
}
public static void displayMatrixes(int[][] a, int[][] b) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a[i][j] + " ");
}
System.out.print("+ ");
for (int j = 0; j < 3; j++) {
System.out.print(b[i][j] + " ");
}
System.out.print("= ");
for (int j = 0; j < 3; j++) {
System.out.print((a[i][j] + b[i][j]) + " ");
}
System.out.println();
}
}
}
printf with The "%3d" specifier means a minimum width of three spaces, which, by default, will be right-justified.
for your right alignment question you can at http://alvinalexander.com/programming/printf-format-cheat-sheet
Related
I am trying to populate my 2-D array with random intergers. The only issue I am having is the last system.out.println(); is highlighted in red. It says error can't resolve symbol 'println'
int[][] array = new int[row][col];
for (int i = 0; i <= rows; i++) {
System.out.println(i);
}
System.out.println();
for (int j = 0; j <= col; j++) {
System.out.println(j);
}
array[row][col] = ((int)(Math.random() * 10));
System.out.println(array[row][col]);
}
// this last line is highlighted in red
System.out.println();
}
}
You can try this
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row;
int col;
System.out.println("row?");
row = sc.nextInt ();
System.out.println("column?");
col = sc.nextInt ();
int[][] array = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
array[i][j] = ((int)(Math.random() * 10));
}
}
for(int i = 0 ;i < row ;i++) {
for(int j = 0 ; j < col ; j++) {
System.out.print(array[i][j] );
}
System.out.println();
}
}
}
You need to take care not to go beyond the end of the arrays, so the <= need to be <. You also want to use the for loops within each other to get the indices right. Thus:
import java.util.*;
public class Array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row;
int col;
System.out.println("row?");
row = sc.nextInt();
System.out.println("column?");
col = sc.nextInt();
int[][] array = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
array[i][j] = ((int)(Math.random() * 10));
System.out.println("row " + i + " column " + j + " " + array[i][j]);
}
}
// this last line is highlighted in red
System.out.println();
}
}
REVISION: now with pretty print of table:
import java.util.*;
public class Array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row;
int col;
System.out.println("row?");
row = sc.nextInt();
System.out.println("column?");
col = sc.nextInt();
System.out.print("```|");
for (int j = 0; j < col; j++) {
System.out.print(" " + j);
}
System.out.println();
for (int j = 0; j <= col; j++) {
System.out.print("----");
}
System.out.println();
//if you want to print any header inforation or the values of row and column, do it here outside of the loops
int[][] array = new int[row][col];
for (int i = 0; i < row; i++) {
System.out.print(i + " |");
for (int j = 0; j < col; j++) {
array[i][j] = ((int)(Math.random() * 10));
System.out.print(" " + array[i][j]); //feel free to add vertical bars here if you wish
}
System.out.println();
}
}
}
guys!
First of all, I'm from Brazil, so sorry if I make some grammar error.
I'm having problems to solve an exercise in whitch I have to a program that generates a matix in java with user-informed dimensions. Then, it has to fill the matrix with values which are also entered by the user. My code stops of running in my second for, passing by the columns. I get a ArrayIndexOutOfBoundException. Can you help me to see what I'm doing wrong?
import java.util.Scanner;
public class DiagonalsSum {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int[][] matrix;
int[] sizes = new int[2];
int diagonalsSum = 0, i, j, n, m;
for(i = 0; i < 2; i++){
n = i + 1;
System.out.println("Inform the " + n + " dimension of the matrix");
sizes[i] = s.nextInt();
}
matrix = new int[sizes[0]][sizes[1]];
for(i = 0; i < matriz.length; i++){
n = i + 1;
System.out.println(n);
for(j = 0; j < matrix[sizes[0]].length; j++){
m = j = 1;
System.out.println("Inform the value of " + n + "." + m +
" in the matrix:");
matrix[i][j] = s.nextInt();
}
}
s.close();
i = 0;
j = 0;
while(i < matrix.length && j < matrix[sizes[1]].length){
diagonalsSum += matrix[i][j];
i++;
j++;
}
i = 0;
j = (matrix[sizes[i]].length - 1);
while(i < matrix.length && j > 0){
diagonalsSum += matrix[i][j];
i++;
j--;
}
System.out.println("The sum of the primary and secondary diagonals is " + diagonalsSum);
}
Thanks in advance, guys!
Try this:
for(i = 0; i < matrix.length; i++){
matrix[i] = new int[sizes[1]];
n = i + 1;
System.out.println(n);
for(j = 0; j < matrix[sizes[0]].length; j++){
m = j = 1;
System.out.println("Inform the value of " + n + "." + m +
" in the matrix:");
matrix[i][j] = s.nextInt();
}
}
Java's an object-oriented language. You'll do better if you encapsulate the behavior you need in a proper Matrix class.
I think there are a couple of errors in here, but I'll address the one you've asked about.
I believe
for(j = 0; j < matrix[sizes[0]].length; j++)
will always result in going out of bounds because you've declared:
matrix = new int[sizes[0]][sizes[1]];
Note that Java has 0 based indexing, meaning that for any array, array[array.length] will be out of bounds. This type of access is effectively what your for loop is doing.
for(j = 0; j < matrix[sizes[0]-1].length; j++)
should fix the column loop issue.
guys!
I changed some things and it worked!
Thanks for all the help!
import java.util.Scanner;
public class DiagonalsSum {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int[][] matrix;
int[] sizes = new int[2];
int diagonalsSum = 0, i, j, n, m;
for(i = 0; i < 2; i++){
n = i + 1;
System.out.println("Inform the " + n + " dimension of the matrix");
sizes[i] = s.nextInt();
}
matrix = new int[sizes[0]][sizes[1]];
for(i = 0; i < sizes[0]; i++){
n = i + 1;
System.out.println(n);
for(j = 0; j < sizes[1]; j++){
m = j = 1;
System.out.println("Inform the value of " + n + "." + m +
" in the matrix:");
matrix[i][j] = s.nextInt();
}
}
s.close();
i = 0;
j = 0;
while(i < sizes[0] && j sizes[1]){
diagonalsSum += matrix[i][j];
i++;
j++;
}
i = 0;
j = (sizes[1] - 1);
while(i < sizes[0] && j > -1){
diagonalsSum += matrix[i][j];
i++;
j--;
}
System.out.println("The sum of the primary and secondary diagonals is " + diagonalsSum);
}
I am a beginner programmer in APCS, and I am getting this error message on my code (below). How do I fix this? I am not sure what is wrong with it because I believe all of the called variables are within the scope of the code and I declared them as public... Let me know what I should be looking for, and what is wrong please! Thank you very much.
Error:
java.lang.ArrayIndexOutOfBoundsException: 4
at Shuffler.perfectShuffle(Shuffler.java:49)
at Shuffler.main(Shuffler.java:16)
Code:
import java.util.ArrayList;
public class Shuffler
{
private static final int SHUFFLE_COUNT = 1;
private static final int VALUE_COUNT = 4;
public static void main (String[] args)
{
System.out.println ("Results of " + SHUFFLE_COUNT + " consecutive perfect shuffles:");
int[] values1 = new int[VALUE_COUNT];
for (int i = 0; i < values1.length; i++)
{
values1 [i] = i;
}
for (int j = 1; j <= SHUFFLE_COUNT; j++)
{
perfectShuffle (values1);
System.out.print(" " + j + ":");
for (int k = 0; k < values1.length; k++)
{
System.out.println (" " + values1 [k]);
}
System.out.println();
}
System.out.println();
System.out.println("Results of " + SHUFFLE_COUNT + " consecutive efficient selection shuffles:");
int[] values2 = new int[VALUE_COUNT];
for (int i = 0; i < values2.length; i++)
{
values2[i] = i;
}
for (int j = 1; j <= SHUFFLE_COUNT; j++)
{
selectionShuffle(values2);
System.out.print(" " + j + ":");
for (int k = 0; k < values2.length; k++)
{
System.out.print(" " + values2[k]);
}
System.out.println();
}
System.out.println();
}
public static void perfectShuffle (int[] values)
{
int[] temp = new int [52];
int k = 0;
for (int j = 0; j < 25; j++)
{
temp [k] = values [j];
k+=2;
}
k=1;
for (int j=26; j<values.length; j++)
{
temp [k] = values [j];
k+=2;
}
for (int j=0; j<values.length; j++)
{
values [j] = temp [j];
}
}
public static void selectionShuffle (int[] values)
{
ArrayList<Integer> temp=new ArrayList<Integer>(52);
int rando=(int) Math.random()*52;
for (int counter=0; counter<temp.size(); counter++)
{
temp.set (counter,rando);
}
}
}
This is a 2d array which is generated at random, and stores 20 integers from 100-1000. I got that part right, I'm trying to switch the rows of this 2d array and not having much luck, i tried the temp but for some reason it comes out as column array...
public static void main (String args[]){
int [][] randArray = new int[2][10];
Random rnd = new Random();
for(int i=0; i<randArray.length; i++)
for(int j=0; j<randArray[i].length; j++)
randArray[i][j] = 100 + rnd.nextInt(900);
for (int i=0; i < randArray.length; i++){
System.out.println("Row " + i);
for (int j=0; j < randArray[i].length; j++)
System.out.print(randArray[i][j]+ " ");
System.out.println();
}
System.out.println("Switch row ");
int temp = 0;
for (int j=0; j < randArray[0].length; j++){
temp = randArray[0][j];
randArray[0][j] = randArray[1][j];
randArray[1][j] = temp;
//System.out.println(randArray[i][j]+ " ");
}
}
}
Switch the rows as in row1 [1,2,3,4,5] row2 [6,7,8,9,10] to row1[6,7,8,9,10] row2[1,2,3,4,5]
You can use Arrays.deepToString(Object[]) to print the array. Display the array after you perform your swap. Something like,
int[][] randArray = new int[2][10];
Random rnd = new Random();
for (int i = 0; i < randArray.length; i++) {
for (int j = 0; j < randArray[i].length; j++) {
randArray[i][j] = 100 + rnd.nextInt(900);
}
}
System.out.println(Arrays.deepToString(randArray));
System.out.println("Switch row ");
for (int j = 0; j < randArray[0].length; j++) {
int temp = randArray[0][j];
randArray[0][j] = randArray[1][j];
randArray[1][j] = temp;
}
System.out.println(Arrays.deepToString(randArray));
Your code is working actually I fixed only the compile error;
public static void main(String args[]) {
int[][] randArray = new int[2][10];
Random rnd = new Random();
for (int i = 0; i < randArray.length; i++)
for (int j = 0; j < randArray[i].length; j++)
randArray[i][j] = 100 + rnd.nextInt(900);
for (int i = 0; i < randArray.length; i++) {
System.out.println("Row " + i);
for (int j = 0; j < randArray[i].length; j++)
System.out.print(randArray[i][j] + " ");
System.out.println();
}
System.out.println("Switch row ");
int temp = 0;
for (int j = 0; j < randArray[0].length; j++) {
temp = randArray[0][j];
randArray[0][j] = randArray[1][j];
randArray[1][j] = temp;
//System.out.println(randArray[i][j]+ " "); no i variable
}
//I added only below lines to print your result
for (int i = 0; i < randArray.length; i++) {
System.out.println("Row " + i);
for (int j = 0; j < randArray[i].length; j++)
System.out.print(randArray[i][j] + " ");
System.out.println();
}
}
How can I make a program that prompts the size of an array and fill it with random numbers, and then add all the numbers that are not at the edge?
Heres the code:
import javax.swing.*;
public class array {
int matrizNN[][];
public void setMatrizNN(int n){
matrizNN = new int[n][n];
for (int i = 0; i < matrizNN.length; i++) {
for (int j = 0; j < matrizNN[i].length; j++) {
matrizNN[i][j]= (int)(Math.random()*10);
System.out.print(" "+matrizNN[i][j]);
}
System.out.println(" ");
}
}
import javax.swing.*;
public class array {
int matrizNN[][];
public void setMatrizNN(int n){
matrizNN = new int[n][n];
for (int i = 0; i < matrizNN.length; i++) {
for (int j = 0; j < matrizNN[i].length; j++) {
matrizNN[i][j]= (int)(Math.random()*10);
System.out.print(" "+matrizNN[i][j]);
}
System.out.println(" ");
System.out.println("Size "+matrizNN.Length);
}
}
public static void setMatrizNN(int n){
matrizNN = new int[n][n];
for (int i = 0; i < matrizNN.length; i++) {
for (int j = 0; j < matrizNN[i].length; j++) {
matrizNN[i][j]= (int)(Math.random()*10);
System.out.print(" "+matrizNN[i][j]);
}
System.out.println(" ");
}
int sum=0;
System.out.println("=========================");
for (int i = 1; i < matrizNN.length-1; i++) {
for (int j = 1; j < matrizNN[i].length-1; j++) {
//System.out.print(matrizNN[i][j]);
sum+=matrizNN[i][j];
}
}
System.out.println("sum is :"+sum);
}
If you havent find the solution yet here is the code. Hope this is what you want.
You seem to have an idea of how to fill matrizNN[][]. To add the non-edge values up, you can use a similar set of for loops but with the first and last values omitted. Here's the basic idea:
int centerTotal = 0;
for (int i = 1; i < matrizNN.length - 1; i++) {
for (int j = 1; j < matrizNN[i].length - 1; j++) {
centerTotal += matrizNN[i][j];
}
}
System.out.println(centerTotal);