Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8871299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:57:27+00:00 2026-06-14T17:57:27+00:00

Hi I am trying to create a matrix on the console using 2D array.

  • 0

Hi I am trying to create a matrix on the console using 2D array. The idea is that the output should look like this one :

1|8|9 |16
2|7|10|15
3|6|11|14
4|5|12|13

Is there any one who has an idea how it can be done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:57:28+00:00Added an answer on June 14, 2026 at 5:57 pm

    Few things you can guess from the matrix: –

    • First, you have to traverse all rows of a columns first before moving to the next column

    • Second, you need to alternate between downwards and upwards direction on each iteration

    • So, you would need two nested for loop, for iterating through rows for a particular column. One will go from row 0 to max - 1, and the next will go from row = max - 1 to 0.

    • Now, to alternate the iteration direction, you can use a boolean variable, and toggle it after each iteration of inner loop finishes.

    • Each loop needs to be enclosed inside an if-else. Both of them will be executed on a certain condition. If boolean downwards = false;, then loop moving upwards will be executed and vice-versa.

    • On each iteration, fill the current cell with an integer counter, that you would have to initialize with 1, and increment it after each fill.


    Pseudo code : –

        // Initialize variables row, col, and count = 1
    
        boolean goDown = true;
    
        int[][] matrix = new int[row][col];  // declare matrix
    
        for i = 0 to col:
            if (goDown)
                for j = 0 to row:  // Move in downwards direction
                    assign count++ to matrix[j][i] 
                    // assign to `[j][i]` because, we have to assign to rows first
    
                goDown = false;    // Toggle goDown
    
            else
                for j = row - 1 to 0:  // Move in upwards direction
                    assign count++ to matrix[j][i] 
    
                goDown = true;  // toggle goDown
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a matrix to look like this: [1 x x^2
I am trying to create a matrix like this from a vector: vec= c(2,
I am trying to create a matrix that is 3 x n, with each
I am trying to create a huge matrix in ff, and I know that
I am trying to create a sortable image matrix, 5x5 using scriptaculous javascript library
Im trying to create a program that shows a character matrix 10x10, and shows
I am trying to create a Matrix class using c++. So far, I have
I'm trying to create an array in one method (or function? Or object? Side
So I'm trying to create a matrix-like program with raining green code. Everything has
I'm new to using R and I am trying to create a matrix of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.