0
try
{
    System.out.println("Enter the number of vertices");
    number_of_vertices = scan.nextInt();
    adjacency_matrix = new int[number_of_vertices + 1][number_of_vertices + 1];
 
    System.out.println("Enter the Weighted Matrix for the graph");
    for (int i = 1; i <= number_of_vertices; i++)
    {
        for (int j = 1; j <= number_of_vertices; j++)
        {
            adjacency_matrix[i][j] = scan.nextInt();
            if (i == j)
            {
                adjacency_matrix[i][j] = 0;
                continue;
            }
            if (adjacency_matrix[i][j] == 0)
            {
                adjacency_matrix[i][j] = Integer.MAX_VALUE;
            }
        }
    }

This is what the user input looks like:

Enter the number of vertices
5
Enter the Weighted Matrix for the graph
0 9 6 5 3 
0 0 0 0 0
0 2 0 4 0
0 0 0 0 0
0 0 0 0 0

How would I be able to hard code the matrix into the program rather than ask the user to enter the matrix?

3
  • Welcome to Stack Overflow. Please take the tour to learn how Stack Overflow works and read How to Ask on how to improve the quality of your question. It is unclear what you are asking or what the problem is. Commented Dec 3, 2022 at 21:45
  • 2
    You might want to look at other questions like stackoverflow.com/questions/13832880/initialize-2d-array Commented Dec 3, 2022 at 21:45
  • Use "something different" instead of System.in..? Commented Dec 3, 2022 at 22:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.