Array sum rows and colums of an array - java

import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
int n, m, sumRows= 0, sumColumns= 0, i = 0, j = 0; //rows(n), Columns(m)
n = Integer.parseInt(JOptionPane.showInputDialog(null, "Rows"));
m = Integer.parseInt(JOptionPane.showInputDialog(null, "Columns"));
int[][] a = new int[n][m];
int[] b = new int[n];
int[] c = new int[m];
for(i = 0; i < a.length; i++) {
for(j = 0; j < a[i].length; j++) {
a[i][j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Type"
+ "an int. A[" + i +"]" + "[" + j + "] = "));
sumRows+= a[i][j];
sumColumns+= a[j][i];
if(j == a[i].length-1) {
b[i] = sumRows;
sumRows= 0;
}
if(i == a.length-1) {
c[j] = sumRows;
sumRows= 0;
}
System.out.println("Sum Rows: " + sumRows+ " Vector B" + i + ": " + b[i]);
System.out.println("Sum Columns: " + sumColumns + " Vector C" + j + ": " + c[j]);
}
}
}
}
So i have to sum the rows and colums and store them on two vectors, i have to store the sum of the rows in Vector B, and the sum of the columns in Vector C.
The sum of the rows works perfectly, but i can't get to work the sum of the columns.

Try this:
public class Main {
public static void main(String[] args) {
int n = Integer.parseInt(JOptionPane.showInputDialog(null, "Rows"));
int m = Integer.parseInt(JOptionPane.showInputDialog(null, "Columns"));
int[][] a = new int[n][m];
int[] b = new int[n];
int[] c = new int[m];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = Integer.parseInt( JOptionPane.showInputDialog(null, "Type" + "an int. A[" + i + "]" + "[" + j + "] = "));
b[i] += a[i][j];
c[j] += a[i][j];
}
}
// USED FOR PRINTING
// -------------------------
for (int i = 0; i < b.length; i++) {
System.out.println("Sum Row " + (i + 1) + " is " + b[i]);
}
for (int i = 0; i < c.length; i++) {
System.out.println("Sum Column " + (i + 1) + " is " + c[i]);
}
// -------------------------
}
}
INPUT:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
OUTPUT:
Sum Row 1 is 10
Sum Row 2 is 35
Sum Row 3 is 60
Sum Row 4 is 85
Sum Column 1 is 30
Sum Column 2 is 34
Sum Column 3 is 38
Sum Column 4 is 42
Sum Column 5 is 46

Related

Java Matrices Dot Product ArrayIndexOutOfBoundsException

I'm getting an error on line 66 c[rowA][colB] = c[rowA][colB] + a[rowA][colA]*b[colA][colB];. I went through the indices by hand, just can't figure out where the index went wrong. Help is greatly appreciated.
package arrayproducts;
import javax.swing.JOptionPane;
public class ArrayProducts {
public static void main(String[] args) {
String output = "";
int rowA = Integer.parseInt(JOptionPane.showInputDialog("\nEnter the number of rows for MatrixA."));
int colA = Integer.parseInt(JOptionPane.showInputDialog("\nEnter the number of columns for MatrixA."));
int rowB = Integer.parseInt(JOptionPane.showInputDialog("\nEnter the number of rows for MatrixB."));
int colB = Integer.parseInt(JOptionPane.showInputDialog("\nEnter the number of columns for MatrixB."));
if( colA != rowB){
output += "Cannot perform matrix operation: Inner matrix dimensions must agree.";
output += "\nMatrixA has a dimension of "+ rowA + " x " + colA + ".";
output += "\nMatrixB has a dimension of "+ rowB + " x " + colB + ".";
JOptionPane.showMessageDialog(null, output);
return;
} else {
output += "\nDot Product Begin:";
int [][] a = new int[rowA][colA];
output += "\nMatrixA has a dimension of "+ rowA + " x " + colA + ".";
int [][] b = new int[rowB][colB];
output += "\nMatrixA has a dimension of "+ rowB + " x " + colB + ".";
JOptionPane.showMessageDialog(null, output);
int [][] c = new int[rowA][colB];
////
// enter first matrix
for(int i = 0; i < rowA; i++){
for(int j = 0; j < colA; j++){
a[i][j] = Integer.parseInt(
JOptionPane.showInputDialog("\nEnter an integer for MatrixA, row " + (i+1) + " and column " + (j+1) + "."));
}
}
// add first matrix to output
output += "\nThe first matrix is: \n";
for(int i=0; i < rowA; i++){
for(int j=0; j < colA; j++){
output += " " + a[i][j];
}
output += "\n";
}
JOptionPane.showMessageDialog(null, output);
////
// enter second matrix
for(int i = 0; i < rowB; i++){
for(int j = 0; j < colB; j++){
b[i][j] = Integer.parseInt(
JOptionPane.showInputDialog("\nEnter an integer for MatrixB, row " + (i+1) + " and column " + (j+1) + "."));
}
}
// add second matrix to output
output += "\nThe second matrix is: \n";
for(int i=0; i < rowB; i++){
for(int j=0; j < colB; j++){
output += " " + b[i][j];
}
output += "\n";
}
JOptionPane.showMessageDialog(null, output);
////
// compute the product
for(int i = 0; i < rowA; i++){
for(int j = 0; j < colB; j++){
for(int k = 0; k < colA ; k++){ // either colA or rowB will work here
c[rowA][colB] = c[rowA][colB] + a[rowA][colA]*b[colA][colB];
}
}
}
output += "\nThe product of MatrixA and MatriB is:\n";
for(int i=0; i < rowA; i++){
for(int j=0; j < colB; j++){
output += " " + c[rowA][colB];
}
output += "\n";
}
JOptionPane.showMessageDialog(null, output);
}
}
}
I guess you meant to use the indices i,j,k instead of rowA, colB etc in the following code.
c[rowA][colB] = c[rowA][colB] + a[rowA][colA]*b[colA][colB];
I will show you a simple example.
int[] a = new int[3];
That mean a can have only 3 values.
a[0], a[1] and a[2]
If you try a[3] it will be out of bound.
So. You have
int [][] c = new int[rowA][colB];
And try to access c[rowA][colB] which is out of bound.
In your three for loops, I think, you want to use i,j and k.

how do i get the code to print the integers i have entered instead of these numbers?

When I enter my 7 integers "1 2 3 4 2 6 2" it will print the correct number of occurrences. However it will print the numbers at "49 50 51 52 53 54 55"... What in my code is missing or what do I need to change in order to get it print the correct numbers?
public static void main(String[] args)
{
final int maxEntryCount = 7;
int [][] numbers = new int [maxEntryCount][2];
System.out.print("Enter " + maxEntryCount + " integers separated by spaces: ");
Scanner input = new Scanner(System.in);
for (int i = 0; i < maxEntryCount; i++) {
numbers[i][0] = (input.next().charAt(0));
}
for (int i = 0; i < maxEntryCount; i++) {
for (int j = 0; j < maxEntryCount; j++) {
if (numbers[i][0] > 0 && numbers [j][0] == numbers[i][0]) {
numbers[i][1]++;
if (j > i) {
numbers[j][0] = 0;
}
}
}
}
for (int i = 0; i < maxEntryCount; i++) {
if (numbers[i][0] > 0) {
System.out.println("Number " + numbers[i][0] + " occurs " + (int) numbers[i][1] + " times");
if (numbers[i][0] == 1) {
System.out.println("Number " + numbers[i][0] + " occurs " + (int) numbers[i][1] + " time");
}
}
}
}
Just change your loop where you are getting the user input to:
for (int i = 0; i < maxEntryCount; i++) {
numbers[i][0] = (input.nextInt());
}
In your code you were reading the input as character. When you type 1 then it will take its ASCII value instead of 1.
I changed it to input.nextInt() which is used to take int as input.

How can i position the colums in my matrix evenly?

package SinemaSalonu;
public class SinemaSalonu {
public static void main(String args[]) {
int[][] matrix = new int[10][20];
for (int k = 0; k < 10; k++) {
matrix[k][0] = k + 1;
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(" " + matrix[i][j] + " ");
}
System.out.println();
}
System.out.println("\n\n" + " " + "P E R D E");
}
}
when i run this code the resulting matrix' last row is not centered with others cause of the number "10" in the last row. it caused the row to slightly shift to the right. i want to fix this and center each row.
In order to align the lines with a single digit with the lines that start with two digit replace your printing:
System.out.print(" " + matrix[i][j] + " ");
with:
int n = matrix[i][j];
System.out.print(" " + (n < 10 ? " " + n : n) );
It will add an extra space before each line that starts with a single digit.
It will look like this:

How to make the for inner loop more efficient?

I am new to Java and there's this one question that makes me wonder. How to make the for inner loop more efficient in this code?
for (int i = 2; i <= 100; i++) {
System.out.print("Factors of " + i + " is: ");
for (int j = 2; j < i; j++) {
if (i % j == 0) System.out.print(j + " ");
}
System.out.println();
}
I was just trying to get the factors of numbers from 2 to 100 but how can i make the inner loop more efficient?
It's a little bit number theory involved here but if you do this it would be efficient specially when the 100 is replaced with something much bigger:
for (int i = 2; i <= 100; i++) {
System.out.print("Factors of " + i + " is: ");
for (int j = 2; j <= (int) Math.sqrt(i); j++) {
if (i % j == 0) System.out.print(j + " " + i / j + " ");
}
System.out.println();
}
You could use the fact that for every divisor a of i there is a number b such that a * b = i.
Find all divisors a <= sqrt(i) and save b = i/a and print these values later.
final int num = 100;
int[] divisors = new int[(int) Math.sqrt(num)];
for (int i = 2; i <= num; i++) {
System.out.print("Factors of " + i + " is: ");
int j = 2;
int index = 0;
for (; j * j < i; j++) {
if (i % j == 0) {
System.out.print(j + " ");
divisors[index++] = i / j;
}
}
if (j * j == i) {
// print sqrt(i) only once, if it's integral
System.out.print(j + " ");
}
while (--index >= 0) {
System.out.print(divisors[index] + " ");
}
System.out.println();
}
This way your inner loop needs only O(sqrt(i)) instead of O(i) operations.
This code time complexity is O(N2).
public static void main(String[] args) {
for (int i = 2; i <= 100; i++) {
System.out.print("Factors of " + i + " is: ");
for (int j = i/2; j > 1; j--) {
if (i % j == 0) System.out.print(j + " ");
}
System.out.println();
}
}
Try this,as your code output will be displayed as follows (ascending order)
Factors of 24 is: 2 3 4 6 8 12
please be noticed, but this given code will be displayed output as follows (descending order )
Factors of 24 is: 12 8 6 4 3 2

Arithmetic Sequences program

I want to write a program Sequence that reads an arbitrary number of command­line ints and stores them inside an array.
The program then looks for 3 numbers in the array that form an arithmetic sequece of length 3.
For example:
% java Sequence 20 8 27 19 10 56 7 12 98
The numbers 8, 10, 12 located at indices 1, 4, 7 form an arithmetic sequence
This is my code until now but it doesn't work:
public class Sequence {
public static void main(String[] args){
int[] arr = new int[args.length];
for(int i = 0; i < arr.length; i++){
arr[i] = Integer.parseInt(args[i]);
}
// Process
for(int a = 0; a < arr.length; a++){
for(int b = 1; b < arr.length; b++){
for(int c = 2; c < arr.length; c++){
if (arr[b] - arr[a] == arr[c] - arr[b]){
System.out.println(a + " " + b + " " + c);
}
}
}
}
}
}
When I run this it shows me this:
1 4 7
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
You have two major issues:
You are not printing the numbers that you find, but their indexes instead. You need to change System.out.println(a + " " + b + " " + c); to System.out.println(arr[a] + " " + arr[b] + " " + arr[c]);
Your indexing is not correct. For each index from the second on, you end up getting the same number three times, which always builds a sequence.
Here is the correct code:
public class Sequence {
public static void main(String[] args){
// 20 8 27 19 10 56 7 12 98
int[] arr = new int[args.length];
for(int i = 0; i < arr.length; i++){
arr[i] = Integer.parseInt(args[i]);
}
for(int a = 0; a < arr.length; a++){
for(int b = a + 1; b < arr.length; b++){
for(int c = b + 1; c < arr.length; c++){
if (arr[b] - arr[a] == arr[c] - arr[b]){
System.out.println(arr[a] + " " + arr[b] + " " + arr[c]);
}
}
}
}
}
}
What you want is tot start index b at a+1 and index c at b+1 in order to avoid using certain numbers double. Although I must say, the sequences you printed are in fact correct as "5 5 5" is an arithmetic sequence. Adding this little change leads to:
public class Sequence {
public static void main(String[] args){
int[] arr = new int[args.length];
for(int i = 0; i < arr.length; i++){
arr[i] = Integer.parseInt(args[i]);
}
// Process
for(int a = 0; a < arr.length; a++){
for(int b = a+1; b < arr.length; b++){
for(int c = b+1; c < arr.length; c++){
if (arr[b] - arr[a] == arr[c] - arr[b]){
System.out.println(arr[a] + " " + arr[b] + " " + arr[c]);
//print the sequence and not the index
}
}
}
}
}
}

Categories

Resources