2d arrays

Hey lads, let me ask you a really good question,
Is there a possibility to write for 2d array syntax to insert and display a 2d array "M[10][10]" using while loop and do while loop?

I know the basic form is using the for loop and i know it works perfect with it, but I am curios because I didn't find info about 2d arrays with other loop then for.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

  int main()
  {
        srand(time(0));
      
       int M[100][100], n=15,m=15;
       
        /// insert
          for(int i=0; i<n; i++)
          {
             for(int j=0; j<m; j++)
             {
                 //manual       
                 //cout<<"M["<<i<<"]["<<j<<"]= ";
                 //cin>>M[i][j];
                
                 //rand
                 M[i][j]=rand()%10;
           }
        }
        cout<<endl;
        //display
        for(int i=0; i<n; i++)
          {
             for(int j=0; j<m; j++)
             {
               cout<<setw(3)<<M[i][j];
             }
            cout<<endl;
        }
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main()
{
   srand(time(0));
   const int n = 15, m = 15;
   int M[n][m];
   int x = n * m;
   int *p = &M[0][0];
   while ( x-- ) *p++ = rand()%10;
       
   //display
   for(int i=0; i<n; i++)
   {
      for(int j=0; j<m; j++)
      {
         cout << setw(3) << M[i][j];
      }
      cout << '\n';
   }
}
Last edited on
@lastchance thanks mate,

I've got a question , though... when declaring:

int *p = &M[0][0];

what does "*" and the "&" operators do?

p.s. I am just at the beginning and I have never used such things before .

Thanks!

also, what does the " while ( x-- ) " mean if x=m*n;

Would appreciate so much if you could explain these things to me;

int *p declares a pointer to an int; i.e. a variable that can hold the address of an int.
The &M[0][0] is the memory address of the start of the array. For a predeclared 2-d array like this the memory is contiguous (i.e. each row will follow directly after the previous one).

The while loop runs through m x n elements. x is decremented by 1 each time (after testing); when it gets to 0 the loop stops.

Within each loop, *p is the value at the memory address pointed to by p. p++ increments the pointer, so pointing to the next element of the array.
Last edited on
@lastchance, thank you sir
MaxGreen, be aware that you must not collapse 2-d to 1-d if the 2-d is dynamically allocated (eg from a ** or vector<vector< ) because the rows may not be touching in memory, and cannot just skip from one to the next with a pointer math, you have to jump to the new offset. This also can cause page faults which is why I recommend avoiding 2-d dynamic memory if possible. It is FINE for a 2-d array as those are assured to be a solid block of memory (this is what you have going on, so its correct for your program).

Last edited on
Topic archived. No new replies allowed.