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

  • SEARCH
  • Home
  • 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 5968161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:02:58+00:00 2026-05-22T20:02:58+00:00

Let’s say we have a two dimensional arrays, arr[N][N] , where N is a

  • 0

Let’s say we have a two dimensional arrays, arr[N][N], where N is a constant integer.
Assume that every element of arr is initialized.

How do I print the elements of arr antidiagonal-wise using nested for loops?

What I mean is:

  • After first iteration of the outer-most loop, arr[0][0] will be printed
  • After second iteration of the outer-most loop, arr[0][1] and arr[1][0] will be printed
  • After third iteration of the outer-most loop, arr[0][2], arr[1][1], and arr[2][0] will be printed
  • …
  • After the last iteration of the outer-most loop, arr[N-1][N-1] will be printed.

Thanks for your time!

  • 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-05-22T20:02:59+00:00Added an answer on May 22, 2026 at 8:02 pm

    Sorry to everybody who wrote “The second half should be similar”… it’s not.

    Anyway, here you go:

    // traverse array diagonally
    int c, tmp, x;
    for (c = N - 1; c > -N; c--) {
        tmp = N - abs(c) - 1;
        x = tmp;
        while (x >= 0) {
            if (c >= 0) {
                std::cout << arr[x][tmp - x] << ", ";
            }
            else {
                std::cout << arr[N - (tmp - x) - 1][(N-1)-x] << ", ";
            }
            --x;
        }
        std::cout << "\n";
    }
    

    Do you need this for a game or something?

    [edit] looking at this again, I think my answer wasn’t very nicely written. Here’s a quick run through:

    Let’s pretend that N is 3.

    What we need is an iteration over coordinate-combinations that looks like this:

    (0, 0)
    (1, 0), (0, 1)
    (2, 0), (1, 1), (0, 2)
    (2, 1), (1, 2)
    (2, 2)
    

    So first some placeholders:

    int c,    // a counter, set by the outer loop
        tmp,  // for intermediate results
        x;    // the x-index into *arr* (*y* will be defined implicitly)
    

    Now this outer loop

    for (c = N - 1; c > -N; c--) { 
    

    makes c iterate over {2, 1, 0, -1, 2}.

    The next step

        tmp = N - abs(c) - 1;
        x = tmp;
    

    turns {2, 1, 0, -1, -2} into {0, 1, 2, 1, 0}, which are the lengths of the needed outputs at this step minus one (so they can be used as indices). We make two copies of this, tmp and x.

    Now we count down from x to 0:

        while (x >= 0) {
            ...
            --x;
        }
    

    if we’re on the upper-left half of arr, indicated by c >= 0, the x-indices into arr need to start at the diagonal and go down to zero (0 to 0, 1 to 0 and 2 to 0) , whereas the y-indices need to start at zero and go up to the diagonal (0 to 0, 0 to 1 and 0 to 2):

            if (c >= 0) {
                std::cout << arr[x][tmp - x] << ", ";
            }
    

    once we’re on the lower-right half, the x-indices need to start at N and to down to the diagonal (2 to 1 and 2 to 2), whereas the y-indices need to start at the diagonal and go up to N (1 to 2 and 2 to 2):

            else {
                std::cout << arr[N - (tmp - x) - 1][(N-1)-x] << ", ";
            }
    

    finally we just need a line-break at the end of each line:

        std::cout << "\n";
    

    Savy? 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.