Like Tree1Likes
  • 1 Post By Elysia

size of 2D array

This is a discussion on size of 2D array within the C++ Programming forums, part of the General Programming Boards category; until now, i have always thought that size of a 2D array means.. A[3][4] = 12 cells but i came ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    8

    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...
    please do help

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    8
    Quote Originally Posted by std10093 View Post
    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?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    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.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by nizwa_hazel View Post
    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
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    8
    Quote Originally Posted by c99tutorial View Post
    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?

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Yes!!
    In order to figure out which are the diagonal elements, take a 5x5 array and draw it in a piece of paper
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    8
    Quote Originally Posted by std10093 View Post
    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?

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The number of cells are N*N = Ν˛

    The number of rows is limited to N
    The number of columns is limited to M
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    8
    Quote Originally Posted by std10093 View Post
    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

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are welcome
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    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.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by King Mir View Post
    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.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    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].
    Elkvis likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of an array poited by array element
    By mitofik in forum C Programming
    Replies: 7
    Last Post: 12-24-2010, 12:09 AM
  2. How do I get the size of an array from a pointer to the array?
    By Programmer_P in forum C++ Programming
    Replies: 31
    Last Post: 06-03-2010, 05:13 PM
  3. size of array - why function gives size ONE only
    By noob123 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2009, 05:20 PM
  4. Finding Words in a array[size][size]
    By ^DJ_Link^ in forum C Programming
    Replies: 8
    Last Post: 03-08-2006, 03:51 PM
  5. array size??
    By CCCP in forum C Programming
    Replies: 2
    Last Post: 01-10-2002, 10:27 AM

Tags for this Thread


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