Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bidimensional array problem

Status
Not open for further replies.

SLider3

Programmer
Jun 17, 2003
56
0
0
PT
I'm trying to make a function, that receives a "bidimensional" array (ex: a[][]) and print it. The problem is that the function could receive arrays of different size and it must print them.
So, i cant declare the function like this:
Code:
void printTable(int table[][5])

and the C doesnt allow me to do this:
Code:
void printTable(int table[][])

Can someone please help me solving this?
 
Have you tried to

Code:
void PrintTable(int** table,int rows,int cols)
{
  for(int i = 0;i<rows;i++)
  {
     for(int j = 0;j<cols;j++)
     {
         cout<<table[rows][cols}<<", ";
     }
     cout<<endl;
  }
}

Matt
 
But an int** is not the same as an int[][]
Casting your true 2D array into an int** will cause problems.

Slider, read this
If you decide to go with the dynamic allocation route, then the int** parameter is the way to go.

> could receive arrays of different size and it must print them
As far as printTable(int table[][5]) is concerned, these arrays are the same
Code:
int a[5][5];
int b[50][5];
printTable( a );
printTable( b );
To make it general, do this
Code:
printTable( int table[][5], size_t nrows );
printTable( a, 5 );
printTable( b, 50 );


--
 
As far as printTable(int table[][5]) is concerned, these arrays are the same
That's the problem. The array could have values different than 5.

And i didnt know memory allocation and as thisn is for a scholl work I think it's suppose not to use it.


Can someone put here some code for what i'm asking?
 
According to Kernighan and Ritchie- THe C Programming Language 2nd edition, section 5.7 pgs 110-112

The function msust be declared:

f(int daytab[2][13]) {...}
or
f(int daytab[][13]){...}
or
f(int (*daytab)[13]){...}

so you can not declare it as such:
f(int daytab[][]){...}

and they say:
"only the first dimension (subscript) of an array is free; all others have to be specified."
 
Why does the 2nd dimension (subscript) of an array have to be specified? e.g.

printTable(int array[][5]){...}

say the func printTable wanted to access array[2][3], e.g.
printf("%d\n", array[2][3]);

array starts at a pointer in memory p. the first row of this is at addresses p, p+1, ..., p+4; the 2nd row at addresses p+5, p+6, ..., p+9; and so on.

so the equation to get to array[2][3] (remember [2][3] is row 3 column 4) is
p + ( (2*5) + 3 )
or if a is the row, b is the col, and C is the # of cols
p + ( (a*C) + b )

so how could the function calculate this address if it did not know what the value of C was?

So you have to ask yourself, what does the teacher want me to do? :)
 
One last hint.
SInce this is a school proj, I cant give you the code.

I dont know what your teacher wants, but you cant say.
printTable( int arr[][] ){...}

It sounds like it might be an exercise in pointer arithmetic.

would it be ok to say
printTable( int *p, int NumOfRows, int NumOfCols ) {...}

and if so, how would you use the previous email to print the table?. Remember, you dont want to print the addresses, but the values stored at the addresses.

 
SkiLift is right. Element a[2][3] of array a[4][5] is the same as b[2*5+3], where b is simply (int *), equal to a. So you must give to your printing subroutine additional parameters - number of columns in your array (or last n-1 dimensions of n-dimensional array).

Code:
#include <stdio.h>

void print_element(int *array, int row, int col, int num_of_cols)
{
   printf("%d\n", array[row*num_of_cols+col]);
}

int main(void)
{
int my_arr[4][5];
int ii, jj;

   for(ii=0; ii<4; ii++)
      for(jj=0; jj<5; jj++)
         my_arr[ii][jj]=0;

   my_arr[2][3]=123;
   print_element((int *)my_arr, 2, 3, 5);

return(0);
}
 
In fact, there is no such animal as n-dimensional array (n > 1) in C. Actually (in memory) it's 1D array, and you may (legally;) use contents of
Code:
int a2d[10][10]; 
/*{a[0][0], a[0][1], ... a[9][9] }*/
as
Code:
int a1d[100];
in your code. In other words, you may linearize arrays in C. Be careful:
Code:
a[10][10], a[][10], a[100], a[][], type** etc
are different things, but it's the other story...

Alas, the only way to write matrix (or 3,4,5...D) functions in C is to treat these nD arrays as linear memory chunks. You must pass dimensions in these functions (with proper casting of array pointer (name), i.e. (double*)array_name) and calculate linear index of each element by hand in functions bodies...

But don't worry: no C standard violation. For example:
Code:
double Max(double* row, int n)
{
  int i;
  double xmax = row[0];
  for (i = 1; i < n; ++i)
    if (row[i] > xmax)
      xmax = row[i];
  return xmax;
}

double MinMax(double* matrix, int rows, int cols)
{
  int i;
  double xmax, minmax = Max(matrix,cols);
  for (i = 1; i < rows; ++i)
    if ((xmax=Max(matrix+i*cols,cols)) < minmax)
       minmax = xmax;
  return minmax;
}
...
double mx[10][20];
...
double mmx = MinMax((double*)mx,10,20);
/* Warning: this code was not debugged */
Some consolation: you may optimize matrix element access code as you wish (by hand, by hand;)...

Are you ready to go to C++ with its true n-dimensional arrays templates (or come back to Fortran;)?...
 
Another variation of the printf in Mingis' code is to take away the brackets completely as follows:

Code:
#include <stdio.h>

void print_element(int *array, int row, int col, int num_of_cols)
{
   printf("%d\n", 
           *(array + (row*num_of_cols+col))
         );
}

int main(void)
{
int my_arr[4][5];
int ii, jj;

   for(ii=0; ii<4; ii++)
      for(jj=0; jj<5; jj++)
         my_arr[ii][jj]=0;

   my_arr[2][3]=123;
   print_element((int *)my_arr, 2, 3, 5);

return(0);
}
 
What is a true n-dim array? There really is no true n-dim array in any language. Raw memory is theoretically linear. High level languages give you the abstraction of n-dimentions. It's just that C gives you the tools to work with memory at a lower level than most high level languages.
 
SkiLift,
low-level linear memory is the other question. The language may (or may not) allow to linearize n-dim array, i.e. legal linear access to elements via single level indexing op.

Yes, C (and Fortran;) allows this linearization. But PL/I, for example, (and Java;) - does not allow. In other words, only programming language defines possible n-dim array low-level implementations (i.e. row/colon mapping to a linear memory). Remember that computer memmory may be by-segment-addressable etc...

For example, compiler implementors may (or may not) use non-continuous memory (for example, rows or colons by segment) for huge arrays.

We have true n-dim array (yes, it's not a common term;) if we can't pass 2D matrix as 1D array (and vice versa) as function argument legally. In C we may do it. Thank you, Ritchi. In Fortran we may - thank you, (deprecated) EQUIVALENCE statement...

In PL/I we may use UNSPEC() low-level generic function to convert n-dim array to (continuous) bit string. But the string may present the other storage with the array contents...

That's how matters stand...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top