#code
1 messages Β· Page 1 of 1 (latest)
fix what?
what issue are u facing
and please share the code as text not image, thanks
Please use this format for posting code:
```java
// Example java program
int value = 5;
System.out.println(value);
```
Which results in:
// Example java program
int value = 5;
System.out.println(value);
For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N1 = sc.nextInt();
int N2 = sc.nextInt();
int start = N1;
for (int i = 0; i < N1; i++) {
start = N1 + 1;
for (int j = 0; j < N2; j++) {
int num = start + j;
if (num >= N1 + N2) {
num -=N2;
}
System.out.print(num + " ");
}
System.out.println("");
}
}
}
Like this? This is how I solve, but it didn't show the expected output like in the question want
u used the wrong format for posting the code, its backticks not "
please reread or copy pasta it, thanks
so what output did ur code show?
please make it easier to help u
Yes this is my output
for the input 5, 5, yes?
okay, now we just need ur code in a readable format (see bot post) and then we should be able to help π
to me it looks like u didnt understand the pattern really yet
but started to code "something" nonetheless
u have to step back, grab pen and paper and first solve this "algorithm" on paper
i.e. u need to draw a diagram, write down the indices and understand the pattern
theres no way to code something without first fully understand what u even want to code
the pattern they want from u are two different tables interleaved
u should first write out the 2 individual tables fully
that is:
5 6 7 8 9
6 7 8 9 10
7 8 9 10 11
8 9 10 11 12
9 10 11 12 13
and the second table is
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
u should first write code snippets that create and print these two tables individually
once ur done with that and fully understand how ur code works, u can start trying to mix the two tables together
but step by step
You never use your i, so you don't apply your progress.
Okay I'll try to code these two table individually first
import java.util.*;
public class Pattern1 {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print((n + i + j) + " ");
}
System.out.println();
}
}
}
Detected code, here are some useful tools:
[WARNING] The code couldn't end properly...
Problematic source code:
java
import java.util.*;
public class Pattern1 {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print((n + i + j) + " ");
}
System.out.println();
}
}
}```
Cause:
The code doesn't compile:
';' expected
<identifier> expected
illegal start of expression
illegal start of expression
class, interface, enum, or record expected
cannot find symbol
symbol: variable java
location: class
cannot find symbol
symbol: class util
location: package java
unreachable statement
## System out
[Nothing]
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(j + " ");
}
System.out.println();
}
Detected code, here are some useful tools:
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(j + " ");
}
System.out.println();
}
great. so now u have the logic for both
it will be helpful for later if u can change the second to have the same loop headers
that is, ur first table has this:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ur second currently has this:
for (int i = 0; i < n; i++) {
for (int j = 1; j <= n; j++) {
adjust the code for the second so that it uses the same as the first
when both logics have the exact same loop construct u will easily be able to merge them together in the next step
Owh I see
almost. now it prints 0 to n-1 instead of 1 to n π
u need to adjust the logic slightly
i.e. that print
Oh wait
What are the tips to merge those two code?
have u adjusted the logic of the second already so that it prints 1...n but uses the same loop headers than the first?
i was waiting for u to share ur progress
I editted it here, sorry
that prints the wrong numbers though
both of ur tables should use this loop construct:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
0 to < n
while still printing the right content
Oh
import java.util.*;
public class Pattern1 {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print((n + i + j) + " ");
}
System.out.println();
}
}
}
Detected code, here are some useful tools:
import java.util.*;
public class Pattern2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Detected code, here are some useful tools:
import java.util.*;
public class Pattern2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print((j + 1) + " ");
}
System.out.println();
}
}
}
Detected code, here are some useful tools:
I've fixed it
great!
okay, so lets do a little experiment for merging them
import java.util.Scanner;
public class Pattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter rows:");
int rows = Integer.parseInt(scanner.nextLine());
System.out.println("Enter columns:");
int cols = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// TODO Logic
}
System.out.println();
}
}
}
lets start with this
so far so good?
so. the "logic" for the first pattern is:
System.out.print((n + i + j) + " ");
the "logic" for the second pattern is:
System.out.print((j + 1) + " ");
lets now write code that alternates the two tables each column
just for fun bc its easy and to show u how to put things together
alternating can be done with if (j % 2 == 0) in case u havent seen that yet π
so, in // TODO Logic we now put:
if (j % 2 == 0) {
System.out.print((n + i + j) + " ");
} else {
System.out.print((j + 1) + " ");
}
execute it and see what happens
Okayh I'll try
Should I initialize n=0?
If replace n with rows or cols, it will show:
5 2 7 4 9
6 2 8 4 10
7 2 9 4 11
8 2 10 4 12
9 2 11 4 13
yes, great
do u notice what just happened?
u interleaved both tables
so. if u understand the code we just wrote, u have all u need, namely this construct:
if (/* TODO Put proper condition */) {
System.out.print((n + i + j) + " ");
} else {
System.out.print((j + 1) + " ");
}
now, if u put in the correct condition u can swap from one table to the other at will
now all u need is the right condition that identifies based on i and j whether ur in the top left half or bottom right half of this:
lets make an easier example first
whats the right condition to separate it like this:
(using i and j)
Hmmm this condition?
if (j == 3) {
System.out.print(" | ");
no. im not sure u understood what i said
i dont want u to print a line
i want u to switch from one table logic to the other
like, left half should use the
5 6 7 8 9
6 7 8 9 10
...
logic
while the right half should use the
1 2 3 4 5
1 2 3 4 5
...
logic
import java.util.*;
public class Pattern1 {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < (n-2); i++) { // Loop for rows
for (int j = 0; j < n; j++) { // Loop for columns
System.out.print((n + i + j) + " ");
}
System.out.println(); // Move to the next line after each row
}
}
}
Detected code, here are some useful tools:
import java.util.*;
public class Pattern2 {
public static void main(String[] args) {
// Loop 5 times to print "4 5" on each line
for (int i = 0; i < 5; i++) {
System.out.println("4 5");
}
}
}
Detected code, here are some useful tools:
okay i think u lost me somewhere
lets go back here
#1306883631981002753 message
we are already done experimenting with the two patterns
we are now putting them together into one code already
go back to the code that alternated the two patterns with each odd/even j
#1306883631981002753 message
it seems to me like u didnt really fully understand that code yet
so we will go back there and learn it fully first
it will show
5 2 7 4 9
6 2 8 4 10
7 2 9 4 11
8 2 10 4 12
9 2 11 4 13
Yes I'm sorry, I'm still a beginnerπ
no worries
can u show me ur full code for that output now please?
just to make sure we are both on the same page
okayh
this one?
yup
okayh
import java.util.*;
public class Pattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter rows:");
int rows = Integer.parseInt(scanner.nextLine());
System.out.println("Enter columns:");
int cols = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (j % 2 == 0) {
System.out.print((rows + i + j) + " ");
} else {
System.out.print((j + 1) + " ");
}
}
System.out.println();
}
}
}
Detected code, here are some useful tools:
great. so lets have a look at what does what
this here is the basic loop structure that will touch each cell in ur final table
row by row, col by col
this decides what to print for a given cell at coordinates i, j
this is the logic u need to use if u want to print the contents of a cell from the "first table"
and this for the "second table"
https://i.imgur.com/4UH8sJL.png
consequently this condition here decides when a cell should get the content of the first or the second table
to make it easier to understand, lets modify the prints for now:
if (j % 2 == 0) {
System.out.print("+ ");
} else {
System.out.print("- ");
}
run it with that modification and take a look at the output
import java.util.*;
public class pattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter rows:");
int rows = Integer.parseInt(scanner.nextLine());
System.out.println("Enter columns:");
int cols = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (j % 2 == 0) {
System.out.print("+ ");
} else {
System.out.print("- ");
}
}
System.out.println();
}
}
}
Detected code, here are some useful tools:
+ - + - +
+ - + - +
+ - + - +
+ - + - +
+ - + - +
Detected code, here are some useful tools:
this is the output
great. so now u can visually easily see when a cell is from "table 1" and when its from "table 2"
i want u to understand that we just merged two tables with each other
and u can control which to display per-cell just by modifying the if
so. if u wanted to print
+ + + - -
+ + + - -
+ + + - -
- - - - -
- - - - -
what would u need to input into the if (...) condition?
whats the condition that identifies the plus-cells here?
if ( (i =0 || i=1 || i=2) && (j>0 && j<3) ) {
System.out.print("+ ");
} else {
System.out.print("- ");
}
Detected code, here are some useful tools:
how about this?
oh, that's simpler
so far so good π
so. by just adjusting this condition u can now control which logic to use
plus or minus
table 1 or table 2 logic
etc
so all we have to figure out now is the correct condition
that identifies this pattern:
+ + + + +
+ + + + -
+ + + - -
+ + - - -
+ - - - -
for this, id suggest u first write out the exact i, j coordinates of each of those cells:
once u write down the exact i, j coordinates for those, u might spot the "math" behind it
(1,5)
(2, 4)
(3, 3)
(4, 2)
(5,1)
very good. now, "coordinates" (indices) in programming typically start at 0, so do our i and j values
so u need to subtract 1 of all of those
(0, 4)
(1, 3)
(2, 2)
(3, 1)
(4, 0)
so. what u need to do now is to formulate a condition that says true whenever ur above or to the left of those coordinates
(and false whenever ur below or to the right of them)
also, lets try to make our coordinates dynamic first
since the user can enter rows and cols
that is, it also has to work if the user entered 10 rows and 7 cols
so lets look at another example quickly with 10 rows and 7 cols
+ + + + + + +
+ + + + + + -
+ + + + + - -
+ + + + - - -
+ + + - - - -
+ + - - - - -
+ - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
thats the same pattern applied to 10 rows and 7 cols
write down the coords of those last + again
(1, 7)
(2, 6)
(3, 5)
(4, 4)
(5, 3)
(6, 2)
(7, 1)
one too much but okay, yes
π
lets first look at the example with 5 rows 5 cols again, cause its easier. and then we try to make a condition
(0, 4)
(1, 3)
(2, 2)
(3, 1)
(4, 0)
no worries
have to minus 1
lets look at this
+ + + + +
+ + + + -
+ + + - -
+ + - - -
+ - - - -
ill tell u a random coordinate
and u tell me whether its in the + or - part
coordinate (3, 4)
observe how ur brain tries to figure it out
"-"
yeah, minus
if i change it to (3, 0) its a +
same row but different column and now it changed
so the condition must be based on both, the row and column
i and j
yes
so. how did ur brain figure out whether its + or -?
u looked at the table, first located the row and then located the column
sth like this:
and thats also what ur code has to do
u need to identify the row (which is i), then compute where the boundary would be for this row
i.e. those:
(0, 4)
(1, 3)
(2, 2)
(3, 1)
(4, 0)
and then look if ur j (column) is below or above that
so, the cell we just checked with our brain was (3, 4)
thats how index i = 3
so u compute the boundary for that row
so the boundary for row index 3 is at column index 1
thats when it switches from + to -
our col was 4 ((3, 4))
which is above 1 ((3, 1))
so its a minus
makes sense so far or sth unclear?
Yeah it took a while for me to understandπ
Yes2
great. another example:
lets inspect cell (2, 4)
row 2 has col-boundary 6
4 is below 6
so its a plus
cell (5, 6) would be row 5, has boundary at 3, 6 is above 3, so its a minus
see how all u need are these boundary values that we computed earlier
so our problem can now be reduced from:
if (...) {
print +
} else {
print -
}
to
int boundaryCol = ...
if (j <= boundaryCol) {
print +
} else {
print -
}
now we have the condition down. its "if our current column is below the boundary column"
j <= boundaryCol
and all that is left to do is to compute the boundary column for the given row
Owhh I think I'm getting it noww
great π
its really important before starting to code anything that u first truly study and understand the problem and its solution
analyze how ur brain attempts doing it and try to spot the "algorithm" it executes
this is all pen&paper work, not coding
thats all we did so far
just studying the problem
and now that we are slowly understanding the solution fully, we can start putting it into code piece by piece
the last puzzle piece now is int boundaryCol = ...
given a row i, what is the boundary column
thats what we have to compute now
i.e. the number on the right, given the number on the left here:
total rows = 5
total cols = 5
(0, 4)
(1, 3)
(2, 2)
(3, 1)
(4, 0)
total rows = 10
total cols = 7
(0, 6)
(1, 5)
(2, 4)
(3, 3)
(4, 2)
(5, 1)
(6, 0)
so if i tell u for example
"a table with 30 rows and 14 columns, what is the boundary column for row 9"
u now need to find the math in order to tell me the answer without drawing a picture for this table first
i havent checked it, but it sounds reasonable
lets put the logic u have in ur head into code and then just try it out
so. what logic did u use?
u took the row 9
and subtracted 14, the total col count from it
in code that would be
Yes
int boundaryCol = ...?
what to put there
u took the row 9 and subtracted 14, the total col count from it
translate that into code now
int boundaryCol = j - currentRow
what is the current row in our code?
which variable
this was the code u posted earlier:
u need to use variables that exist
oh sorry it's int boundaryCol = cols - i
okay. lets put that into the code
and then see what it prints
we will easily be able to identify based on the output with + and - whether its correct now or not
if the logic is correct, we get a beautiful diagonal shape
import java.util.*;
public class pattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter rows:");
int rows = Integer.parseInt(scanner.nextLine());
System.out.println("Enter columns:");
int cols = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int boundaryCol = cols - i;
if (j < boundaryCol) {
System.out.print("+ ");
} else {
System.out.print("- ");
}
}
System.out.println();
}
}
}
Detected code, here are some useful tools:
Input:
Enter rows:
10
Enter columns:
7
Output:
+ + + + + + +
+ + + + + + -
+ + + + + - -
+ + + + - - -
+ + + - - - -
+ + - - - - -
+ - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
Detected code, here are some useful tools:
juicy!
now enter the thing we just wanted to try
"a table with 30 rows and 14 columns, what is the boundary column for row 9"
enter 30 rows and 14 columns
lets see if it looks correct as well
look at how the pattern looks like
looks exactly like what we want
perfect diagonal
okay, another edge case to test
what happens if we have more cols than rows
try with rows 10 and cols 20
lets see if it also prints correctly or does sth we dont expect
Enter rows:
10
Enter columns:
20
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + -
+ + + + + + + + + + + + + + + + + + - -
+ + + + + + + + + + + + + + + + + - - -
+ + + + + + + + + + + + + + + + - - - -
+ + + + + + + + + + + + + + + - - - - -
+ + + + + + + + + + + + + + - - - - - -
+ + + + + + + + + + + + + - - - - - - -
+ + + + + + + + + + + + - - - - - - - -
+ + + + + + + + + + + - - - - - - - - -
Detected code, here are some useful tools:
noice
amazing
that means the logic u wrote must be correct now
π
lets substitute back in the real logic instead of + and - and then ur done
System.out.print((rows + i + j) + " ");
and
System.out.print((j + 1) + " ");
π
import java.util.*;
public class pattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter rows:");
int rows = Integer.parseInt(scanner.nextLine());
System.out.println("Enter columns:");
int cols = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int boundaryCol = cols - i;
if (j < boundaryCol) {
System.out.print((rows + i + j) + " ");
} else {
System.out.print((j + 1) + " ");
}
}
System.out.println();
}
}
}
Detected code, here are some useful tools:
Enter rows:
5
Enter columns:
5
5 6 7 8 9
6 7 8 9 5
7 8 9 4 5
8 9 3 4 5
9 2 3 4 5
Detected code, here are some useful tools:
[WARNING] The code couldn't end properly...
Problematic source code:
Enter rows:
5
Enter columns:
5
5 6 7 8 9
6 7 8 9 5
7 8 9 4 5
8 9 3 4 5
9 2 3 4 5```
Cause:
The code doesn't compile:
';' expected
## System out
[Nothing]
Closed the thread.
@last mica
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure π
@last mica
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure π
code