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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:28:30+00:00 2026-06-05T04:28:30+00:00

//The size of test doesn’t matter for now just assume it’s fitting int* test

  • 0
//The size of test doesn't matter for now just assume it's fitting
int* test = new int[50000]
    for(int i=stepSize;i<=maxValue;i+=stepSize){

               for(int j=0;j<=i;j+=stepSize){
                    //Comput something and store it
                    test[i*30+j] = myfunc();
               }

    }

If I now would want to convert it in a 1D array how could I calculate the correct indices for the 1D array? For example for i=5 and j=0 it should be at the first posistion etc.

EDIT: Updated the code. I tried to calculate something and store it in a 1d array by calculating its index with i*30+j but this doesnt work.

  • 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-05T04:28:31+00:00Added an answer on June 5, 2026 at 4:28 am

    Assuming the array was defined as follows:

    int a[30][5];
    

    you could index into it like this:

    a[i][j]
    

    Or define it as a 1 dimension array as follows:

    int a[30*5];
    a[j + 5*i];
    

    Here is an example program that displays the iterations:

    (Notice that there are those who might say I switched the rows and columns, but it doesnt really matter since its iterating contiguously through the array. That is if you think of the rows and columns differently, just switch all occurrences and you should get the same result.)

    int main(int argc, char **argv)
    {
        int columns = 30;
        int rows = 5;
        int a[columns*rows]; // not really needed for this example
    
        for(int i = 0; i < columns; ++i)
        {
            for(int j = 0; j < rows; ++j)
            {
                cout << "[" << i << "][" << j << "] offset: " << (i*rows + j)
                     << endl;
            }
        }
    }
    
    [0][0] offset: 0
    [0][1] offset: 1
    [0][2] offset: 2
    [0][3] offset: 3
    [0][4] offset: 4
    [1][0] offset: 5
    [1][1] offset: 6
    [1][2] offset: 7
    [1][3] offset: 8
    [1][4] offset: 9
    [2][0] offset: 10
    [2][1] offset: 11
    [2][2] offset: 12
    [2][3] offset: 13
    [2][4] offset: 14
    [3][0] offset: 15
    [3][1] offset: 16
    [3][2] offset: 17
    [3][3] offset: 18
    
    ...
    
    [27][4] offset: 139
    [28][0] offset: 140
    [28][1] offset: 141
    [28][2] offset: 142
    [28][3] offset: 143
    [28][4] offset: 144
    [29][0] offset: 145
    [29][1] offset: 146
    [29][2] offset: 147
    [29][3] offset: 148
    [29][4] offset: 149
    

    And one more piece of information, if you need to allocate a 2D array dynamically, here’s how:

    int **a = new int*[30];
    for(int i = 0; i < 30; ++i)
    {
        a[i] = new int[5];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

const size_t size = 5; int *i = new int[size](); for (int* k =
From a question on the Practice C test from GeekInterview, why is the size
I am wondering is there any way we can test the font size/color of
I develop the application on tablet and want to test it on smaller size
How do output the directory size in Linux? I am currently using du /root/test
Is there any tool to generate test data based on specific requirements? e.g. Size,
In following code the issue is, that I cannot test dao.add() without using dao.list().size()
<?php error_reporting(E_ALL); include func.php; connectToDatabase(localhost,root,,test); $fileName = $_FILES['name']; $fileSize = $_FILES['size']; $fileType = $_FILES['type'];
I'm new to Robolectric and I'm trying to unit test a simple getter/setter code,
It doesn't seem to do squat for the following test program. Is this because

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.