Useing a single dimension array as a multi simension one

This is a discussion on Useing a single dimension array as a multi simension one within the C++ Programming forums, part of the General Programming Boards category; Ive been told that the computer can seek a number in a single dimension array faster than it can in ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    Useing a single dimension array as a multi simension one

    Ive been told that the computer can seek a number in a single dimension array faster than it can in say a 4d one, I cant seem to figure out how to index to the right number in the 1d one though?

  2. #2
    Unregistered
    Guest
    You might be getting thrown off by the fact that an array is zero based...
    therefore to access it by row and col or any other discard the 0 element and make the array greater by 1...
    see this sample


    Code:
    int rows = 10;
    int cols   = 10;
    int *myArray;
    
    myArray = new int[rows * cols + 1];
    
    
    0 -ignored
    1-10
    11-20
    21-30
    ......
    91-100
    myArray[3 * 10l] = 30;
    will assign the value 30 to the 31st item or item index 30...
    in a two dimensional array this would be item[2][9]....3rd row 10th column....

    or to loop through the items..
    the following code assigns each index a value equal to it's index
    starting at 1 and ending at n
    Code:
    for(i=0;i<rows;i++)
    {
        for(j=1;j<=cols;j++)
        {
             myArray[i*10 + j] = ((i *10) + j);
         }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Help printing a multi array
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 05-05-2008, 02:35 PM
  3. multi dimensional array at runtime
    By sawer in forum C++ Programming
    Replies: 21
    Last Post: 05-10-2006, 11:59 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM
  5. multi array to function
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-09-2001, 04:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21