Vektoret me shume dimensione

- Vektoret mund te kene shume dimensione (indekse)

P.Sh. tabelat perbehen nga rreshta dhe kolona. Vektori me m rreshta dhe n shtylla quhet vektor mxn.

Elementi i vektorit a[i][j] tregon se elementi ndodhet ne rreshtin i dhe shtyllen j.

- I referohemi nje elementi a[x][y] dhe jo a[x,y] sepse merret si a[y] (gabim)

 

- Inicializimi:

int b[2][2] = {{1},{3,4}}

ku b[0][0]=1, b[0][1]=0, b[1][0] = 3, b[1][1] = 4.

Shembull printimi i vlerave ne tre  vektore dydimensionale:

// Initializing multidimensional arrays.

#include <iostream>

using namespace std;

void printArray( const int [][ 3 ] ); // prototype

 

int main()

{

   int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };

   int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 };

   int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };

 

   cout << "Values in array1 by row are:" << endl;

   printArray( array1 );

 

   cout << "\nValues in array2 by row are:" << endl;

   printArray( array2 );

 

   cout << "\nValues in array3 by row are:" << endl;

   printArray( array3 );

   return 0; // indicates successful termination

} // end main

 

// output array with two rows and three columns

void printArray( const int a[][ 3 ] )

{

   // loop through array's rows

   for ( int i = 0; i < 2; i++ )

   {   

      // loop through columns of current row

      for ( int j = 0; j < 3; j++ )

         cout << a[ i ][ j ] << ' ';

 

      cout << endl; // start new line of output

   } // end outer for

} // end function printArray

/* printon

Values in array1 by row are:

1 2 3

4 5 6

 

Values in array2 by row are:

1 2 3

4 5 0

 

Values in array3 by row are:

1 2 0

4 0 0

 

--------------------------------

Process exited after 0.08219 seconds with return value 0

Press any key to continue . . .

*/

Nese kalohet si argument tek nje funksion nje vektor dydimensional jepet vetem permasa e dyte ne deklarimin e parametrit.

p.sh.

PrintoVektor (int a[][3])

{....}