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 465941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:25:42+00:00 2026-05-12T23:25:42+00:00

I have, int[10] oneDim = {1, 2, 3, 4, 5, 6, 7, 8, 9,

  • 0

I have,

int[10] oneDim = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, index = 0;

as shown here, we create the the two dimensional one from the origin. But how do I iterate my oneDim inside for (index = 0; index < 10; index++) so that I could get my column index and row index there without creating a new one?
I want it looks like this while printing its indexes to a two dimensional array (2×5):

0,0
0,1
1,0
1,1
2,0
2,1
3,0
3,1
4,0
4,1

I think the main issue here is getting the column index and row index without creating the two dimensional one. Don’t you?

  • 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-12T23:25:43+00:00Added an answer on May 12, 2026 at 11:25 pm

    If you want row-major order, given row rowIndex, column columnIndex and are faking (for lack of a better term) a two-dimensional array with numberOfColumns columns, the formula is

    rowIndex * numberOfColumns + columnIndex.
    

    If you want row-major order, given row rowIndex, column columnIndex and are faking (for lack of a better term) a two-dimensional array with numberOfRow rows, the formula is

    columnIndex * numberOfRows + rowIndex.
    

    So, assuming row-major order:

    int[10] oneDim = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int rows = 2;
    int columns = 5;
    for (int row = 0; row < rows; row++) {
        for (int column = 0; column < columns; column++) {
            System.out.println(row + ", " + column + ": " + oneDim[row * columns + column]);
        }
    }
    

    Output:

    0, 0: 1
    0, 1: 2
    0, 2: 3
    0, 3: 4
    0, 4: 5
    1, 0: 6
    1, 1: 7
    1, 2: 8
    1, 3: 9
    1, 4: 10
    

    And if you insist on indexing using a single for loop, assuming row-major order, the formula that you want is the following:

    int column = index % numberOfColumns;
    int row = (index - column) / numberOfColumns;
    

    If you’re using column-major order, the formula that you want is the following:

    int row = index % numberOfRows;
    int column = (index - row) / numberOfRows;
    

    So,

    int[10] oneDim = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int rows = 2;
    int columns = 5;
    for(int index = 0; index < 10; index++) {
        int column = index % columns;
        int row = (index - column) / columns;
        System.out.println(row + ", " + column + ": " + oneDim[index]);
    }
    

    will output

    0, 0: 1
    0, 1: 2
    0, 2: 3
    0, 3: 4
    0, 4: 5
    1, 0: 6
    1, 1: 7
    1, 2: 8
    1, 3: 9
    1, 4: 10
    

    as expected.

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

Sidebar

Related Questions

i have two int array of images , i have implemented it in grid
I have an int array containing gray scale values from 0-254, i also have
I have some function, int somefunction( //parameters here, let's say int x) { return
Here's the piece of code I currently have: int main () { int pid,
We have int main(int argc, char** argv, char** envc) for the ordinary. but I
In getFieldSignExtended(int,int,int) , I have if-else statements inside if-else statements. I have int result
Ok here is the problem, I have three NSString s with int values, when
i have: int[] numbers = { 1, 2, 3}; string[] words = { one,
i have an array, int* array , with more than 10.000 int values, but
I am trying to flatten a two dimensional array into a one dimensional array.

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.