-3

I would like to let you know that I'm new to this platform, I'm trying to solve this question, could anyone help me?

statement The user must be prompted for the size of the matrix to be created. After user input, a square matrix is ​​created with the information obtained. Example: The user entered the value 3 so we will have it.

[][][]
[][][]
[][][]

However, when printing the matrix on the screen, the diagonal must be filled with the values ​​1 and the value 0 for the other positions, but the diagonal must start on the right side. Example of the expected solution:

[0][0][1]
[0][1][0]
[1][0][0]
  • 2
    What have you tried so far? Please include a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and read [how to ask](https://stackoverflow.com/help/how-to-ask). You're expected to attempt a solution before asking on here. We are willing to help you learn, but we are not going to do your work for you. – Jesse Oct 20 '22 at 23:04

1 Answers1

1

I think this question I would start by figuring out how to create the matrix with input, then I would probably keep some type of pointer that starts at the end of the first row as it is being built then I would decrement the pointer after each row till I am at index 0 of the last row with the pointer value in this case an integer.

I will code it below:

import java.util.*;

public class QuestionOne {

public static int[][] createMatrix(Integer n) {

    int pointer = n - 1;

    int[][] matrix = new int[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (pointer == j) {
                matrix[i][j] = 1;
                pointer--;
                continue;
            }
            matrix[i][j] = 0;

        }
    }

    return matrix;
}

public static void printMatrix(int[][] matrix, int n) {
    for(int i = 0; i < n; i++) {
        for(int k = 0; k< n-1; k++) {
            System.out.print(matrix[i][k] + ",");
        }
        System.out.print(matrix[i][n-1]);
        System.out.println("");
    }
}

public static void main(String[] args) {

    System.out.print("Enter a number for declaring size:");

    Scanner input = new Scanner(System.in);

    int n = input.nextInt();
    int[][] mat = createMatrix(n);
    printMatrix(mat, n);

}

}
devin
  • 368
  • 1
  • 3
  • 19
  • Do not post answers for homework assignments where the poster has made no effort to solve the problem! You are doing their homework for them, which is wrong in multiple ways. – skomisa Oct 21 '22 at 02:30
  • Ok I didn't realize it is an assignment? would you like me to delete it? – devin Oct 21 '22 at 02:48
  • It doesn't matter whether or not it's homework, but it is very obviously an assignment of some kind (school, work, self-assigned, etc.). It doesn't really matter how it was assigned or who assigned it. Point is they have a task to do, and doing it for them isn't going to teach them how to do it. This site is for learning, not spoon feeding. Answering questions like this will simply encourage them to just post on here every time they have something they want to finish but don't want to do themselves, which in turn degrades the quality of the site as a whole. – Jesse Oct 21 '22 at 16:41
  • Questions like this also are not very searchable, and are unlikely to be helpful to future readers that don't have this exact assignment. The people that post them also tend to delete the question as soon as they get an answer that solves their problem which wastes your time as well. If I were you, I would (and I do) just avoid answering questions like this, because they're unlikely to be helpful to anyone besides the OP. Just downvote, add a comment, and move on to a question that's more worth your time. – Jesse Oct 21 '22 at 16:51
  • @Jesse could I post a little bit of help instead of answering the whole question in the future? Try to help them think of a probable solution and not the solution itself? – devin Oct 22 '22 at 03:12