#code

1 messages Β· Page 1 of 1 (latest)

finite mulchBOT
#

<@&987246399047479336> please have a look, thanks.

weak plume
#

fix what?

#

what issue are u facing

#

and please share the code as text not image, thanks

finite mulchBOT
#

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.

last mica
#
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("");
        }
          
    }
}
last mica
weak plume
#

u used the wrong format for posting the code, its backticks not "

#

please reread or copy pasta it, thanks

weak plume
#

please make it easier to help u

last mica
last mica
weak plume
#

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

delicate vault
#

You never use your i, so you don't apply your progress.

last mica
last mica
finite mulchBOT
# last mica ``` java import java.util.*; public class Pattern1 { public static void main...

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]
last mica
finite mulchBOT
weak plume
#

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

weak plume
#

almost. now it prints 0 to n-1 instead of 1 to n πŸ™‚

#

u need to adjust the logic slightly

#

i.e. that print

last mica
#

What are the tips to merge those two code?

weak plume
#

i was waiting for u to share ur progress

last mica
weak plume
#

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

last mica
#
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(); 
        }
    }
}
finite mulchBOT
last mica
#
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(); 
        }
    }
}
finite mulchBOT
weak plume
#

ur second table is incorrect

#

it will print 0 1 2 ...

#

but it should print 1 2 3 ...

last mica
#
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(); 
        }
    }
}
finite mulchBOT
last mica
weak plume
#

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

last mica
last mica
weak plume
#

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)

last mica
#

Hmmm this condition?

if (j == 3) {
        System.out.print(" | ");
weak plume
#

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

last mica
#
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
        }
    }
}
finite mulchBOT
last mica
#
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");
        }
    }
}
finite mulchBOT
weak plume
#

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

last mica
#
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
last mica
weak plume
#

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

last mica
#

okayh

weak plume
#

yup

last mica
#

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

}
finite mulchBOT
weak plume
#

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"

#

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

last mica
#
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(); 
            }
      
      }
}
finite mulchBOT
last mica
#
+ - + - + 
+ - + - + 
+ - + - + 
+ - + - + 
+ - + - +
finite mulchBOT
last mica
weak plume
#

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?

last mica
#
if ( (i =0 || i=1 || i=2) && (j>0 && j<3) ) {
 System.out.print("+ ");
} else {
System.out.print("- ");
}
finite mulchBOT
weak plume
#

great!

#

u can simplify this a bit

#

i < 3 && j < 3

last mica
weak plume
#

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

last mica
#

(1,5)
(2, 4)
(3, 3)
(4, 2)
(5,1)

weak plume
#

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)
weak plume
#

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

last mica
#

(1, 7)
(2, 6)
(3, 5)
(4, 4)
(5, 3)
(6, 2)
(7, 1)

weak plume
#

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)
last mica
#

oh sorry

#

I forgot

weak plume
#

no worries

last mica
#

have to minus 1

weak plume
#

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

last mica
#

"-"

weak plume
#

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

last mica
#

yes

weak plume
#

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?

last mica
#

Yeah it took a while for me to understandπŸ’€

last mica
weak plume
#

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

last mica
weak plume
#

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

last mica
#

(9,5) is boundary?

#

By subtracting 14 with given row 9

weak plume
#

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

weak plume
#

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

last mica
#

int boundaryCol = j - currentRow

weak plume
#

what is the current row in our code?

#

which variable

#

this was the code u posted earlier:

#

u need to use variables that exist

last mica
#

oh

#

int boundaryCol = cols - j

weak plume
#

j is the column πŸ˜›

#

u took the row 9 and subtracted 14, the total col count from it

last mica
#

oh sorry it's int boundaryCol = cols - i

weak plume
#

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

last mica
#

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(); 
            }
      
      }
}
finite mulchBOT
last mica
#
Input:
Enter rows:
10
Enter columns:
7

Output:
+ + + + + + + 
+ + + + + + - 
+ + + + + - - 
+ + + + - - - 
+ + + - - - - 
+ + - - - - - 
+ - - - - - - 
- - - - - - - 
- - - - - - - 
- - - - - - - 
finite mulchBOT
weak plume
#

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

last mica
#

Okay wait

weak plume
#

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

last mica
#
Enter rows:
10
Enter columns:
20
+ + + + + + + + + + + + + + + + + + + + 
+ + + + + + + + + + + + + + + + + + + - 
+ + + + + + + + + + + + + + + + + + - - 
+ + + + + + + + + + + + + + + + + - - - 
+ + + + + + + + + + + + + + + + - - - - 
+ + + + + + + + + + + + + + + - - - - - 
+ + + + + + + + + + + + + + - - - - - - 
+ + + + + + + + + + + + + - - - - - - - 
+ + + + + + + + + + + + - - - - - - - - 
+ + + + + + + + + + + - - - - - - - - - 
finite mulchBOT
weak plume
#

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) + " ");
#

πŸ‘Œ

last mica
#
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(); 
            }
      
      }
}
finite mulchBOT
last mica
#
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 
finite mulchBOT
weak plume
#

πŸ‘

#

congrats

last mica
#

Yes2 I finally figured it out

#

Thank you so muchhh I really appreciate your help!

finite mulchBOT
#

Closed the thread.

finite mulchBOT
#

@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 πŸ‘

finite mulchBOT
#

@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
#

code