change array logic in java - java

How can I change the following code so it prints out:
123
123
123
instead?
public class A4SXYY {
public static void main(String[] args) {
int[][] a2 = new int[3][3];
for (int i = 0; i < a2.length; i++) {
for (int j = 0; j < a2[i].length; j++) {
a2[i][j] = i + 1;
System.out.print(" " + a2[i][j]);
}
System.out.println("");
}
}
}

public static void main(String[] args) {
int[][] a2 = new int[3][3];
for (int i = 0; i < a2.length; i++) {
for (int j = 0; j < a2[i].length; j++) {
a2[i][j] = j + 1;
System.out.print("" + a2[i][j]);
}
System.out.println("");
}
}
123
public static void main(String[] args) {
int[][] a2 = new int[3][3];
for (int i = 0; i < a2.length; i++) {
for (int j = 0; j < a2[i].length; j++) {
a2[i][j] = 3 - j;
System.out.print("" + a2[i][j]);
}
System.out.println("");
}
}
321

The only issue you have is in the line where you assign a value to the array. You use the are using i+1 and should be using j+1. See corrected code below:
public class A4SXYY{
public static void main(String[] args){
int[][] a2 = new int[3][3];
for (int i=0; i<a2.length; i++){
for (int j=0; j<a2[i].length; j++){
a2[i][j] = j+1; //Use j instead of i
System.out.print(" " + a2[i][j]);
}
System.out.println("");
}
}
}

Related

why is system.out.println using 2-d arrays giveing error?

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();
}
}
}

Ragged array not showing all averages

It only prints out the average to the first column but doesn't do anything for the next.
it only prints out 717, for the first column average. It is a ragged array. Everything else compiles fine.
import java.util.Scanner;
import java.io.*;
public class Fitness
{
public static void main(String [] args)
{
int [][] week = {{800,1000,100},{450,100,845,20,1200,200},{1800,250,400},{0,1500,800,120},{600,500},{700,1400,1700,100},{675}};
System.out.println("Average over 7 days");
avgCalb(week);
static void avgCalb(int [][] x)
{
for(int j = 0; j < x[0].length; j++)
{
int colTotal = 0;
for(int i = 0; i < x.length; i++)
{
colTotal = colTotal + x[i][j];
}
System.out.println(colTotal/7);
break;
}
}
}
summing columns
static void avgCalb(int [][] x)
{
int[] colTotal = new int[x.length];
for(int j = 0; j < x.length; j++)
{
for(int i = 0; i < x[j].length; i++)
{
colTotal[i] += x[j][i]
}
}
for(int i = 0;i<colTotal.length;i++) {
System.out.println((double)colTotal[i]/colTotal.length);
}
}
summing rows
static void avgCalb(int [][] x)
{
for(int j = 0; j < x.length; j++)
{
int colTotal = 0;
for(int i = 0; i < x[j].length; i++)
{
colTotal += x[j][i];
}
System.out.println(colTotal/x[j].length);
}
}

I want to print the following format in java program for the pryamid

1**
2**
3***
4****
Till then I have got this snippet of code
public class triangles {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
}
You can print your index before the loop :
for (int i = 1; i <= 4; i++) {
System.out.print(i);//<<----------Print the index i
for (int j = 0; j < i; j++) {
System.out.print(i == 1 ? "**" : "*");//check if i == 1 then print 2 stars else 1
}
System.out.println("");
}
In case you mean 1* you can replace System.out.print(i == 1 ? "**" : "*"); with System.out.print("*");
you just need to add i index to print
here you go
public class triangles {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
system.out.println(i);
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
}
Java 8 simplifies it:
public class triangles {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
System.out.print(i);
System.out.println(String.join("", Collections.nCopies(i, "*")));
}
}
}

Can not get precise results for the hourglass algorithm

Here is the link for definition of the hourglass problem:
https://www.hackerrank.com/challenges/30-2d-arrays
I wrote the following program:
package day11;
import java.util.Scanner;
public class Solution {
public static void main(String ... args){
Scanner scan = new Scanner(System.in);
int[][] arr = new int[6][6];
int maxHourGlassValue = 0;
int temp = 0;
int currMax = 0;
int k = 0, l = 0;
for(int i = 0 ; i < 6 ; i++){
for(int j = 0 ; j < 6 ; j++){
arr[i][j] = scan.nextInt();
}
}
for(int i = 1 ; i < 5 ; i++){
for(int j = 1 ; j < 5 ; j++){
if(maxHourGlassValue < currMax){
maxHourGlassValue = currMax;
}
}
}
System.out.println(maxHourGlassValue);
}
}
I could only run 6 out of 8 given test cases. What could possibly go wrong ????
Try this code, i did not write that code, i just copy it and modified it from here
import java.util.Scanner;
public class Test {
public static void main(String ... args){
Scanner scan = new Scanner(System.in);
int size = 6;
int[][] m = new int[size][size];
//numbers input
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
m[i][j] = scan.nextInt();
}
}
int temp = 0, MaxSum = -99999;
for (int i=0; i<size; ++i) {
for (int j=0; j<size; ++j) {
if (j+2 < size && i+2 < size) {
temp = m[i][j] + m[i][j+1] + m[i][j+2] + m[i+1][j+1] + m[i+2][j] + m[i+2][j+1] + m[i+2][j+2];
if (temp >= MaxSum) {
MaxSum = temp;
}
}
}
}
System.out.println(MaxSum);
}
}
Below is the code which will run successfully for all the test cases of hourglass problem.
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
int[][] arr = new int[6][6];
int maxHourGlassValue = -63;//Assigning (-9*7=)-63 which is the minimum possible value of "hourglass sum".
//Reading inputs.
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
arr[i][j] = scan.nextInt();
}
}
//Logic.
/**
* Index of both i and j will run from 1 to 4 (one less than n-1 where n = 6)
* So for each i and j iteration calculating the sum of hourglass.
*/
int iHGValueTemp = 0;
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
iHGValueTemp = arr[i][j] + /*Main element*/
arr[i - 1][j - 1] + arr[i - 1][j] + arr[i - 1][j + 1]+ /*Top three elements of main element.*/
arr[i + 1][j - 1] + arr[i + 1][j] + arr[i + 1][j + 1]; /*Bottom three elements of main element.*/
if (iHGValueTemp > maxHourGlassValue) {
maxHourGlassValue = iHGValueTemp;
}
}
}
//Output.
System.out.println(maxHourGlassValue);
}
}
I have written description within code in the comments only. Please refer to that and discuss with me if any doubt.

Elevens Activity 4 APCS debugging

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);
}
}
}

Categories

Resources