So I was tasked to write a program that can create two matrices and then add them both together. I must ask the length and height of matrix 1, and then ask the length and height of matrix 2. Which I did here:
Scanner keyboard = new Scanner(System.in);
int length1, height1, length2, height2;
System.out.println("Welcome to the matrix adder program");
System.out.println(" ");
System.out.println("Please enter the length of the first matrix");
length1 = keyboard.nextInt();
System.out.println("Please enter the height of the first matrix");
height1 = keyboard.nextInt();
System.out.println("Please enter the length of the second matrix");
length2 = keyboard.nextInt();
System.out.println("Please enter the height of the second matrix");
height2 = keyboard.nextInt();
int[][] a = new int[length1][height2];
int[][] b = new int[length2][height2];
Then I need to enter a value for matrix 1 row 1, column 1, row 1, column 2, and so on until it meets the dimensions of the array. So I put
for (int i = 1; i < length1 + 1; ++i)
{
for (int j = 1; j < height1 + 1; ++j)
{
System.out.println("Please enter a value for matrix 1 space "+ i + "," + j);
a[i][j] = keyboard.nextInt();
}
}
And this is where I get the error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at MaxtrixAddition.main(MaxtrixAddition.java:32)
It would need to output something like this:
Please enter a value for matrix 1 space 1, 1
4
Please enter a value for matrix 1 space 1, 2
1
Please enter a value for matrix 1 space 2, 1
8
Please enter a value for matrix 1 space 2, 2
7
Please enter a value for matrix 2 space 1, 1
1
Please enter a value for matrix 2 space 1, 2
3
Please enter a value for matrix 2 space 2, 1
2
Please enter a value for matrix 2 space 2, 2
4
What would I need to do to get rid of that error?
I hope that I understood what you want to do.
Arrays start at position 0.
So your code snip should be:
for (int i = 0; i < length1; ++i)
{
for (int j = 0; j < height1 ; ++j)
{
System.out.println("Please enter a value for matrix 1 space "+ i + "," + j);
a[i][j] = keyboard.nextInt();
}
}
Related
We have a High School activity where we utilize ArrayList methods to create a mean and median program. However, I encounter a problem when I inputted 20 data as shown below. All answers and suggestions are appreciated.
THE PROBLEM:
Create a MeanMedian2 class so that the user can enter any number of values up to 20. If the list has an even number of values, the median is the numeric average of the values in the two middle positions. Allow the user to enter 9999 to quit entering numbers.
//https://www.softwaretestinghelp.com/java-arraylist-tutorial/
//https://stackoverflow.com/questions/16242733/sum-all-the-elements-java-arraylist
//https://stackoverflow.com/questions/16242733/sum-all-the-elements-java-arraylist
//https://stackoverflow.com/questions/22023053/how-do-i-print-a-single-item-on-a-array-list
import java.util.*;
class MeanMedian2{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<>();
int reference = 0;
int count = 1;
int ender = 9999;
while(true){
System.out.println("Enter number " + count + ":" );
int num = input.nextInt();
if (num == ender){
break;
}else{
numbers.add(num);
reference += 1;
count += 1;
}
}
//Mean
double sum = 0;
for (int i = 0; i < numbers.size(); i++)
sum += numbers.get(i);
double mean = sum/numbers.size();
//Median
int median;
Collections.sort(numbers);
int length = numbers.size();
int oddOrEven = length % 2;
int Odd = (length + 1)/2;
if (oddOrEven != 0 ){
median = numbers.get(Odd - 1);
}else{
int a = numbers.get((length/2)-1);
int b = numbers.get(length/2);
median = (a+b)/2;
}
//Final
System.out.print("You entered: ");
for(int i = 0; i < numbers.size() - 1; i++){
System.out.print(numbers.get(i) + ", ");
}
System.out.println(numbers.get(length-1));
System.out.println("The mean is " + mean + " and the median is " + median );
}
}
Instance 1:
/**
Enter number 1:
25
Enter number 2:
50
Enter number 3:
500
Enter number 4:
550
Enter number 5:
450
Enter number 6:
600
Enter number 7:
200
Enter number 8:
10
Enter number 9:
700
Enter number 10:
9999
You entered: 10, 25, 50, 200, 450, 500, 550, 600, 700
The mean is 342.77777777777777 and the median is 450**/
Instance 2. I inputted 20 data and 1 9999 to stop (21 in all)
/**
javac MeanMedian2.java
java MeanMedian2Exception in thread "main"
java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at MeanMedian2.main(MeanMedian2.java:19)**/
Change the while loop condition.
while(reference != 20){
I want to create a matrix like this way:
Enter the number of rows and columns of the array: 4 5
Enter the array:
3 4 5.5 4 5 (enter)
1 3.5 1.5 4 5 (enter)
6.5 1.5 2.5 5 1 (enter)
4 3 1 4 4.4 (enter)
then local the smallest value's location in this matrix, but I already stuck in the first step
java.util.Scanner input = new java.util.Scanner(System.in);
int row = 0, col = 0;
double numList[][] = new double[row][col];
System.out.print("Rnter the number if rows and columns of the array: ");
row = input.nextInt();
col = input.nextInt();
System.out.println("Enter the array:");
what I have already tried is here,
Is there any method? THANK YOU GUYS
You can use Scanner.nextDouble() to read the next double value.
import java.util.Scanner;
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of rows and columns of the array: ");
int row = input.nextInt();
int col = input.nextInt();
double numList[][] = new double[row][col];
System.out.println("Enter the array:");
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
numList[i][j] = input.nextDouble();
}
}
I want to generate an output like this :-
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
However, i am trying in this way to achieve , but some piece of logic is missing which i am unable to decipher.
Here's what i am trying :-
import java.util.Scanner;
class Fibonacci
{
public static void main(String arr[])
{
System.out.println("Enter a no.");
Scanner input=new Scanner(System.in);
int num=input.nextInt();
int x=0,y=1;
for(int i=0;i<=num;i++)
{
for(int j=0;j<i;j++)
{
System.out.print(j);
}
System.out.println("");
}
}
}
And it generates the output like this (consider num=6)
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
What logic is required to get the desired output ? Would be thankful if anyone can explain me this :)
Thanks in advance !!
You need to change logic of inner loop like this by adding two previous number to current number and swap them like this.
import java.util.Scanner;
class Fibonacci
{
public static void main(String arr[])
{
int x = 0, y = 0, c = 0;
System.out.println("Enter a no.");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
for (int count = 0; count < num; count++) {
System.out.print(0);
x = 0;
y = 1;
c = 0;
for (int i = 1; i <= count; i++) {
c = x + y;
y = x;
x = c;
System.out.print(" " + c);
}
System.out.println();
}
}
}
First two numbers 0 and 1 are given , no need to caculate .
Use a String to save previous line string .
Caculate next numbers , add it into your previous line string .
Here is an example :
int x = 0 , y = 1;
int num = 6;
System.out.println("0");
System.out.println("0 1");
String str = "0 1";
for(int i = 2 ; i < num ; i ++){
int amt = x + y ;
x = y;
y = amt;
str += " " + amt;
System.out.println(str);
}
In a Fibonacci series only the first two numbers are provided which are 0 and 1. The next number of the series are calculated by adding the last two numbers. The series is limited by the user by providing the number of integers it wants in the series.
Logic: The logic behind creating a Fibonacci series is to add the two integers and save them in a new variable z = x+y and then replace the first integer value by the second integer and second integer value by their sum to move one step ahead in the series x=y adn y=z.
In your problem you want the series to be printed in a right angled triangle so you need to save the series that is already printed in a string
int n = 10;
System.out.println("0\n");
System.out.println("0 1\n");
int x = 0, y=1;
int i=2, z=0;
String str = "0 1";
while(i!=10)
{
z = x+y;
str += " " + z;
x=y;
y=z;
i++;
System.out.println(str);
}
Hope this helps
The code seems to run except what I am getting is not a matrix of a specified (by the user) size but what I think is a heap address
Here's what it returns when the user inputs 2 for the size and then 4 numbers:
Enter matrix size:
2
Enter a 2 by 2 matrix row by row: 2 3 4 5
The row-sort matrix is...[[D#3c954549BUILD SUCCESSFUL (total time: 8 seconds)
here is the code....thank you in advance.
import java.util.Scanner;
public class Exercise7_26M {
public static void main (String[]args)
{
//Prompt user for input of matrix size
System.out.println("Enter matrix size: ");
Scanner input = new Scanner(System.in);
int size = input.nextInt();
double[][] m = new double [size][size];
//prompt user for input of array
System.out.print("Enter a " + size + " by " + size + " matrix row by row: ");
for (int row = 0; row < 2; row++)
for (int column = 0; column < 2; column++)
m[row][column] = input.nextDouble();
System.out.print("The row-sort matrix is..." + m);
}
Java arrays do not override toString() so you are getting the default implementation from Object. Instead, you can use Arrays.deepToString(Object[]) like
System.out.println("The row-sort matrix is..." + Arrays.deepToString(m));
Pasted below is a program to print Pascal's triangle. The way to compute any given position's value is to add up the numbers to the position's right and left in the preceding row. For instance, to compute the middle number in the third row, you add 1 and 1. the sides of the triangle are always 1 because you only add the number to the upper left or the upper right (there being no second number on the other side).
int pascal[][]=new int[50][50]; int j;
for(int i=0;i<m;i++)
{
pascal[i][i]=1;
for(j=1;j<i;j++)
{
pascal[i][j]=pascal[i-1][j-1]+pascal[i-1][j];
}
for(int n=1;n<=m-i;n++)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
System.out.print(pascal[i][k]);
System.out.print(" ");
}
System.out.println(" ");
}
Is there any way to accomplish this without using arrays?
I'm trying this combination without arrays:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
You can easily compute it using Combination.
You can compute combination as:
|n| = (n!) / ((n-k)!*k!)
|k|
So if you want to print the image above you would start as:
int size = 5;
for(int i = 0; i < size; i++){
for(int k = 0; k < (size - i)/2; k++)
System.out.print(" "); // print the intendation
for(int j = 0; j <= i; j++){
System.out.print(combination(i,j));
}
System.out.println("");
}