# size of 2D array

• 01-31-2013
nizwa_hazel
size of 2D array
until now, i have always thought that size of a 2D array means..

A[3][4] = 12 cells

but i came across this program

Code:

void accept(int a[ ][ ],int size)
{  cout<<"Diagonal One:";
for (int i=0;i<size;i++)
for(int j=0;j<size;j++)
if (i= = j)
cout<<a[i][j]<<’\t’;
cout<<"\n Diagonal Two:";
for (i=0;i<size;i++)
for(j=0;j<size;j++)
if((i+j)= =(size-1))
cout<<a[i][j]<<’\t’;
}

this is a very normal prog
but i dont understand how i and j can be limited by the 'size'

i mean, i and j stand for rows and cols...so how can the loop run for total number of cells?

so i thought, size might stand for something else here...
• 01-31-2013
std10093
Code:

array[3][4]
How many cells? 3*4=12
Code:

array[N][M]
How many cells? N*M
Code:

array[N][N]
How many cells? N*N = N². This is a square matrix (the number of its rows is equal with the number of its columns). They are used pretty frequently. It is sufficient to have one dimension for them.
• 01-31-2013
nizwa_hazel
Quote:

Originally Posted by std10093
Code:

array[N][N]
How many cells? N*N = N². This is a square matrix (the number of its rows is equal with the number of its columns). They are used pretty frequently. It is sufficient to have one dimension for them.

yeah so that becomes N²
so u mean to say, size here means rows or cols and not row*col because its a square matrix?
• 01-31-2013
c99tutorial
This goes back to good advice: functions should be documented. It seems the writer of the function wants you to give size, where size is the dimension of a square matrix. This notion of size, and the fact that the array is a square matrix, should be stated somewhere like in the header file or in the API documentation.

Also, don't write == with a space in-between. I didn't even know that was legal.
• 01-31-2013
std10093
Quote:

Originally Posted by nizwa_hazel
yeah so that becomes N²
so u mean to say, size here means rows or cols and not row*col because its a square matrix?

It means:
rows = N
columns = N

This is a special case of normal 2D array:
rows = N
columns = M
where N = M
• 01-31-2013
nizwa_hazel
Quote:

Originally Posted by c99tutorial
This goes back to good advice: functions should be documented. It seems the writer of the function wants you to give size, where size is the dimension of a square matrix. This notion of size, and the fact that the array is a square matrix, should be stated somewhere like in the header file or in the API documentation.

Also, don't write == with a space in-between. I didn't even know that was legal.

thank you..
this isnt my prog. But i assure you i never leave spaces between ==

if its of any help, this is the actual question:
Write a function in C++ which accepts a 2D array of integers and its size as arguments and displays the elements which lie on diagonals. [Assuming the 2D Array to be a square matrix with odd dimension i.e., 3x3, 5x5 ,7x7 etc…]

so if they say its a square matrix, i can consider size to be one of the dimensions, yeah?
• 01-31-2013
std10093
Yes!!
In order to figure out which are the diagonal elements, take a 5x5 array and draw it in a piece of paper ;)
• 01-31-2013
nizwa_hazel
Quote:

Originally Posted by std10093
It means:
rows = N
columns = N

This is a special case of normal 2D array:
rows = N
columns = M
where N = M

i get all that...
but my actual question is:
why are the rows and cols (i and j) limited to size (N²) and not to no. of rows and no. of cols?
• 01-31-2013
std10093
The number of cells are N*N = Ν²

The number of rows is limited to N
The number of columns is limited to M
• 01-31-2013
nizwa_hazel
Quote:

Originally Posted by std10093
Yes!!
In order to figure out which are the diagonal elements, take a 5x5 array and draw it in a piece of paper ;)

lol i know that *rolls eyes* hehe
thanks neways for ur help :)
• 01-31-2013
std10093
You are welcome :)
• 01-31-2013
King Mir
FYI, this "program" won't compile, and isn't valid C++. A function cannot be declared to take a 2d array of unspecified width, and == must not have a space between it.
• 02-03-2013
C_ntua
Quote:

Originally Posted by King Mir
FYI, this "program" won't compile, and isn't valid C++. A function cannot be declared to take a 2d array of unspecified width, and == must not have a space between it.

This also points out that you cannot use an array[][] in general as you cannot specify the size since you need it flexible as you are passing its size as well. You can use int** instead.
• 02-05-2013
Elysia
I would argue the real solution in that case would be to use a template, allowing the size to be a template parameter.
Of course, it would be better std::array was used in the first place (perhaps with templates) [requires C++11, see sig].