5 Replies - 1421 Views - Last Post: 16 October 2001 - 11:35 AM Rate Topic: -----

#1 runtime error  Icon User is offline

  • Lucky.Code
  • member icon

Reputation: 3
  • View blog
  • Posts: 629
  • Joined: 19-March 01

using a single array  as if it were a multi

Posted 14 October 2001 - 11:18 PM

ok i have a question (shocking i know).

i have a list of numbers and i have to print from a specified row and column to a specified row and column.
easy as pie if i could use a multi-dimensional array. But i cant.so i need to know what goes into the element part of the array.

void text_display (int array[ ], int nr, int nc, int start_row, int start_column,

                           int end_row, int end_column) { int r,c; r=start_row;

while (r<=end_row) {

c=start_column; while(c<=end_column) { cout<<array[what goes in here?]; c++; }

r++;

cout<<endl; }

}


nr and nc is the amount of rows and columns i have to work with. the rest is self explanatory.

Thanks.



Is This A Good Question/Topic? 0
  • +

Replies To: using a single array  as if it were a multi

#2 malkiri  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 364
  • Joined: 29-March 01

Re: using a single array  as if it were a multi

Posted 15 October 2001 - 07:27 AM

I think I know what you're asking...
If the data would look like this if you used a multi-dimensional array:
1 2 3 4
5 6 7 8
9 10 11 12
but instead looks like this in your single dim. array:
1 2 3 4 5 6 7 8 9 10 11 12
All you would need to do to find the index of the element at row row and column column is:
index = (column - 1) + (row - 1)*row_length


where row_length is the length of the rows.
(this is based on the assumption that row & column are indexes starting at 1. if they start at 0, then drop the " - 1" part.)

-Malkiri

Was This Post Helpful? 0
  • +
  • -

#3 malkiri  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 364
  • Joined: 29-March 01

Re: using a single array  as if it were a multi

Posted 15 October 2001 - 07:29 AM

Now someone post the code to find the row and column in the multi-dimensional array, given the index in the single dimensional array and the row and column lengths. :)
Was This Post Helpful? 0
  • +
  • -

#4 Null and Void  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 204
  • Joined: 29-April 01

Re: using a single array  as if it were a multi

Posted 15 October 2001 - 02:19 PM

int FirstIndex  = (int)(Index / Width);
int SecondIndex =      (Index % Width);


Math is your friend. So is properly used integer truncation.
Was This Post Helpful? 0
  • +
  • -

#5 runtime error  Icon User is offline

  • Lucky.Code
  • member icon

Reputation: 3
  • View blog
  • Posts: 629
  • Joined: 19-March 01

Re: using a single array  as if it were a multi

Posted 15 October 2001 - 03:41 PM

woohoo i knew you guys would pull through for me. Thanks!
:biggrin:
Was This Post Helpful? 0
  • +
  • -

#6 malkiri  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 364
  • Joined: 29-March 01

Re: using a single array  as if it were a multi

Posted 16 October 2001 - 11:35 AM

I should have said, "Now someone other than Null and Void..." :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1