code :
class test1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt(); // input number rows & colums
int twoD[][] = new int[a][];
int z;
for (z = 0 ; z < a ; z++) {
twoD[z] = new int[z + 1];
}
int i,j,k = 0;
for (i = 0 ; i < a ; i++) {
for (j = 0 ; j <= i ; j++){
twoD[i][j] = k;
k++;
}
for (i = 0 ; i < a ; i++ ) {
for (j = 0 ; j <= i ; j++){
System.out.print(twoD[i][j] + " ");
}
System.out.println();
}
}
}
my expected output is ( for a = 4) :
0
1 2
3 4 5
6 7 8 9
my output is (for a = 4):
0
0 0
0 0 0
0 0 0 0
please help me fix my problem. according to me the lopping is correct. there might be mistake somewhere else...
The loop that prints the contents of the array is contained within the loop that is supposed to fill the 2D array with values. Since it uses the same variables, it interferes with the execution of the first loop. Move it out:
int i,j,k = 0;
for (i = 0 ; i < a ; i++) {
for (j = 0 ; j <= i ; j++){
twoD[i][j] = k;
k++;
}
}
for (i = 0 ; i < a ; i++ ) {
for (j = 0 ; j <= i ; j++){
System.out.print(twoD[i][j] + " ");
}
System.out.println();
}
You could have avoided this by
using and editor or IDE that automatically formats your code, so that is shows you how the control structures are nested
using common idioms like declaring the loop variables with the smallest necessary scope:
for (int i = 0 ; i < a ; i++)
Related
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 2 years ago.
Improve this question
The question of the problem is given in the following link: https://www.codechef.com/problems/PRICECON
I have written the program and it also satisfies the sample test case but still shows wrong answer. Any sort of help would be great.
I have taken care of all test cases and I believe that my program is missing out on some corner cases.
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class sample
{
public static void main (String[] args) throws IOException
{
String s = "";
char ch = '\u0000';
char ch1 = '\u0000';
int i =0;
int n = 0;
int k =0;
int arr_ref = 0;
int decrease = 0;
int p[] = new int[10000];
//int p1[] = new int[10000];
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int test_cases = Integer.parseInt(in.readLine() );
if(test_cases > 100 || test_cases < 1 )
{
System.exit(0);
}
for(int z = 0 ; z<test_cases ; z++)
{
s = "";
ch = '\u0000';
ch1 = '\u0000';
i =0;
n = 0;
k =0;
arr_ref = 0;
decrease = 0;
s = in.readLine();
for(i = 0 ; i < s.length() ; i++)
{
if(s.charAt(i) == ' ' )
break;
else
ch += s.charAt(i);
}
//System.out.println("ch : "+ch+"|");
for(k = i+1 ; k<s.length() ; k++ )
{
ch1+= s.charAt(k);
}
n = (int)(ch)-48;
k = (int)(ch1)-48;
//System.out.println("n : "+n);System.out.println("k : "+k);
if(n > 10000 || n < 1 )
{
System.exit(0);
}
if(k > 1000 || k < 1 )
{
System.exit(0);
}
s = in.readLine();
/*for( i = 0 ; i < n ; i++)
{
p[i] = Integer.parseInt(in.readLine() );
if(p[i] > k)
decrease += p[i] - k;
}*/
arr_ref = 0;
for(i = 0 ; i < 10000 ; i++ )
{
p[i]= 0;
}
/*for(i = 0 ; i < n ; i++ )
{
System.out.println("p["+(i+1)+"] : "+p[i]);
}*/
for( i = 0 ; i < s.length() ; i++)
{
if(s.charAt(i) == ' ')
arr_ref++;
else
p[arr_ref] = (p[arr_ref]*10)+((int)(s.charAt(i))-48);
}
for( i = 0 ; i <= arr_ref ; i++)
{
//System.out.println("p["+(i+1)+"] : "+p[i]);
if(p[i] > 1000 || p[i] < 1)
{
System.exit(0);
}
if(p[i] > k)
{
decrease += p[i] - k;
}
}
System.out.println(decrease);
}
}
}
Your code does not work when inputs are given like this
3
5
4
10
2
3
4
5
But works for
3
5 4
10 2 3 4 5
I suggest you to use java.util.Scanner class which will work for both formats..
Other than that, Your logic is correct.
A sample solution for your reference is mentioned.
Store each answer in array and print at the end.
I was trying out a sample problem statement and correct code for which is something like this -
import java.util.Scanner;
public class Sample {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int m = 5, n = 5;
int dist = 0;
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
int value = scanner.nextInt();
if(value == 1) {
dist = Math.abs(i - 2) + Math.abs(j - 2);
}
}
scanner.nextLine();
}
System.out.println(dist);
}
}
This runs perfectly giving the correct answer. But when I write the code -
import java.util.Scanner;
public class Sample {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int m = 5, n = 5;
int i = 0, j = 0;
int dist = 0;
for(; i < m; ++i) {
for(; j < n; ++j) {
int value = scanner.nextInt();
if(value == 1) {
dist = Math.abs(i - 2) + Math.abs(j - 2);
}
}
scanner.nextLine();
}
System.out.println(dist);
}
}
The answer is 0, always.
For the sample input -
0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
The correct answer is 3 and I am getting 3 while running the 1st piece of code. But not while running the 2nd piece of code.
Another approach could be the following to reset the variable j as mentioned previously
public class Sample {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int m = 5, n = 5;
int i = 0, j = 0;
int dist = 0;
for(; i < m; ++i, j = 0) {
for(; j < n; ++j) {
int value = scanner.nextInt();
if(value == 1) {
dist = Math.abs(i - 2) + Math.abs(j - 2);
}
}
scanner.nextLine();
}
System.out.println(dist);
}
}
The problem is that the variable j is not reset to 0.
The correct code would be:
import java.util.Scanner;
public class Sample {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int m = 5, n = 5;
int i = 0;
int dist = 0;
for(; i < m; ++i) {
int j = 0; // <== this line was missing
for(; j < n; ++j) {
int value = scanner.nextInt();
if(value == 1) {
dist = Math.abs(i - 2) + Math.abs(j - 2);
}
}
scanner.nextLine();
}
System.out.println(dist);
}
}
If you don't know, see how the for statement works in https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
In the first piece of code, the variable j is reset back to 0 each time the code exits and restarts that inner loop. In the second piece of code, that does not happen. A step by step look at what the variables are storing might be helpful. I added the following line inside the inner loop and commented out the user input section for debugging purposes:
System.out.println("( " + i + " " + j + ")");
With the first example, this prints:
( 0 0)
( 0 1)
( 0 2)
( 0 3)
( 0 4)
( 1 0)
( 1 1)
( 1 2)
( 1 3)
( 1 4)
( 2 0)
( 2 1)
( 2 2)
( 2 3)
( 2 4)
( 3 0)
( 3 1)
( 3 2)
( 3 3)
( 3 4)
( 4 0)
( 4 1)
( 4 2)
( 4 3)
( 4 4)
However for the second example it prints:
( 0 0)
( 0 1)
( 0 2)
( 0 3)
( 0 4)
Essentially in the second example, j is not reset for each iteration of the outer loop. If you add more print statements in other places, for instance inside the outer loop but not the inner one, the scoping will become even more clear. It's important to note that the outer loop is still running 5 times, we just have no evidence of that as the print statement becomes unreachable after the first pass through the inner loop.
I want to create a 2d int array thats getting bigger from the outside to the inside, the first layer should start with 1 and the next layer should be 1 higher.
With n = 3 it should look like this :
1 1 1 1 1 1
1 2 2 2 2 1
1 2 3 3 2 1
1 2 3 3 2 1
1 2 2 2 2 1
1 1 1 1 1 1
thats what i already have
int n = 3;
int[][] feld = new int[2*n][2*n];
int c = 1 ;
for (int i = 0; i < 2*n; i++) {
for (int j = 0; j < 2*n; j++) {
feld[i][j] = c-i+j;
}
c++;
}
for (int i = 0; i < feld.length; i++) { //this to printing the matrix
for (int j = 0; j < feld[i].length; j++) {
System.out.print(feld[i][j] + " ");
}
System.out.println();
}
This should work.
int n = 3;
int[][] feld = new int[2*n][2*n];
int c = 0;
while(c<n) {
for(int i = c; i < 2*n-c; i++) {
feld[c][i] = c+1;
feld[i][c] = c+1;
feld[2*n-c-1][i] = c+1;
feld[i][2*n-c-1] = c+1;
}
c++;
}
for (int i = 0; i < feld.length; i++) { //print the matrix
for (int j = 0; j < feld[i].length; j++) {
System.out.print(feld[i][j] + " ");
}
System.out.println();
}
You can change n to whatever value and it should still work
I have been trying different variations of for loops and have no clue how to make these patterns:
Pattern
1
121
12321
1234321
My code is the following but doesn't work like the example above.
for (int i = 1 ; i <= rows ; i++) {
for (int j = (rows + 1 - i) ; j > 0 ; j-- ) {
System.out.print(j);
}
System.out.print("\n");
}
Your code prints only the suffix of each line, you are missing to write 12....i for each line.
In addition, the loop should start from i, not from rows-i+1.
for (int i = 1 ; i <= rows ; i++) {
//add an inner loop that prints the numbers 12..i
for (int j = 1 ; j < i ; j++ ) {
System.out.print(j);
}
//change where j starts from
for (int j = i ; j > 0 ; j-- ) {
System.out.print(j);
}
System.out.println(""); //to avoid inconsistency between different OS
}
First note that 11*11 = 121, 111*111=12321, etcetera.
Then that 10n - 1 is a number that consists of n 9's, so (10n - 1)/9 consists of n 1's.
So we get:
int powerOfTen = 1;
for (int len = 0; len < 5; len++)
{
powerOfTen = powerOfTen*10;
int ones = (powerOfTen-1)/9;
System.out.println(ones*ones);
}
Code explains everything!
public static void main(String[] args) {
String front = "";
String back = "";
int rows = 5;
for (int i = 1; i <= rows; i++) {
System.out.println(front+i+back);
front += i;
back = i + back;
}
}
Try this one: it may seems too much looping, but yet easy to understand and effective.
public static void main(String[] args) {
int rows=5;
int i,j;
for(i=1;i<=rows;i++)
{
/*print left side numbers form 1 to ...*/
for(j=1;j<i;j++)
{
System.out.printf("%d", j);
}
/*Print the middle number*/
System.out.printf("%d", i);
/*print right numbers form ... to 1*/
for(j=i-1;j>0;j--)
{
System.out.printf("%d", j);
}
System.out.println("");
}
}
int n=0;
for(int m =0; m<=5; m++){
for(n= 1;n<=m;n++){
System.out.print(n);
}
for(int u=n;u>=1;u--){
System.out.print(u);
}
System.out.print("");
}
I'm creating a program to create an Identity Matrix - which is pretty easy. But now I need to create the Identity Matrix, but backwards. The result needs to be like so:
0 0 1
0 1 0
1 0 0
Here is the program I'm using that is creating the Identity Matrix:
import java.util.*;
class Lab19Part2 {
public static int[][] create(int size) {
int[][] matrix = new int[size][size];
for(int i = 0; i < size; i++)
for(int j = 0; j < size; j++)
matrix[i][j] = (i == j) ? 1 : 0;
return matrix;
} public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter size of matrix: ");
int size=input.nextInt();
int matrix[][]=create(size);
for (int i=0 ; i < matrix.length ; i++) {
System.out.println();
for (int j=0 ; j < matrix[i].length ; j++){
System.out.print(matrix[i][j]+" ");
}
}
}
}
Though it prints out the Identity Matrix like so:
1 0 0
0 1 0
0 0 1
Question is, how do I make it so it prints out like the first Identity Matrix? I know it has something to do with the for loops but I can't pinpoint it.
Thanks!
You would need to change your condition that controls whether the value is 1 or 0:
matrix[i][j] = (i + j == size - 1) ? 1 : 0;
So that if size is 3, positions [0][2], [1][1], and [2][0] get 1's.
Change
matrix[i][j] = (i == j) ? 1 : 0;
to
matrix[i][j] = (i == size - j - 1) ? 1 : 0;
Why not do this for your identity case:
for(int i = 0; i < size; i++) {
matrix[i][i] = 1;
}
Then for the other case use:
for(int i = 0; i < size; i++) {
matrix[i][size - (i+1)] = 1;
}
Here is the brief example how to create it with la4j (Linear Algebra for Java):
Matrix a = new Basic2DMatrix(Matrices.asIdentitySource(3)).transpose();
// will create a 3x3 matrix
//
// 0 0 1
// 0 1 0
// 1 0 0