Using SWP.C as a Template

Chapter 9 - Arrays

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Multidimensional Arrays
The term dimension represents the number of indexes used to reference a particular element in an array. All of the arrays discussed so far have been one-dimensional and require only one index to access an element. By looking at an array’s declaration, you can tell how many dimensions it has. If there is only one set of brackets ([ ]), the array is one-dimensional, two sets of brackets ([ ][ ]) indicate a two-dimensional array, and so on. Arrays of more than one dimension are called multidimensional arrays. For real-world modeling, the working maximum number of dimensions is usually three.
The following declarations set up a two-dimensional array that is initialized while the program executes:
/*
*   2daray.c
*   A C program demonstrating the use of a two-dimensional array
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>

#define iROWS 4
#define iCOLUMNS 5

main( )
{
 int irow;
 int icolumn;
 int istatus[iROWS][iCOLUMNS];
 int iadd;
 int imultiple;

 for(irow=0; irow < iROWS; irow++)
   for(icolumn=0; icolumn < iCOLUMNS; icolumn++) {
     iadd = iCOLUMNS - icolumn;
     imultiple = irow;
     istatus[irow][icolumn] = (irow+1)
*
       icolumn + iadd
* imultiple;
   }

 for(irow=0; irow<iROWS; irow++) {
   printf(“CURRENT ROW: %d\n”,irow);
   printf(“RELATIVE DISTANCE FROM BASE:\n”);
   for(icolumn=0; icolumn<iCOLUMNS; icolumn++)
     printf(“ %d ”,istatus[irow][icolumn]);
   printf(“\n\n”);
 }

 return(0);
}
The program uses two for loops to calculate and initialize each of the array elements to its respective “offset from the first element.” The created array has 4 rows (iROWS) and 5 columns (iCOLUMNS) per row, for a total of 20 integer elements. Multidimensional arrays are stored in linear fashion in the computer’s memory. Elements in multidimensional arrays are grouped from the rightmost index inward. In the preceding example, row 1, column 1 would be element three of the storage array. Although the calculation of the offset appears a little tricky, note how easily each array element itself is referenced:
istatus[irow][icolumn] = . . .
The output from the program looks like this:
CURRENT ROW: 0
RELATIVE DISTANCE FROM BASE:
0  1  2  3  4

CURRENT ROW: 1
RELATIVE DISTANCE FROM BASE:
5  6  7  8  9

CURRENT ROW: 2
RELATIVE DISTANCE FROM BASE:
10  11  12  13  14

CURRENT ROW: 3
RELATIVE DISTANCE FROM BASE:
15  16  17  18  19
Multidimensional arrays can also be initialized in the same way as one-dimensional arrays. For example, the following program defines a two-dimensional array dpowers and initializes the array when it is defined. The function pow( ) returns the value of x raised to the y power:
/*
*   2dadbl.c
*   A C program using a 2-dimensional array of doubles
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>
#include <math.h>

#define iBASES 6
#define iEXPONENTS 3
#define iBASE 0
#define iRAISED_TO 1
#define iRESULT 2

main( )
{
 double dpowers[iBASES][iEXPONENTS]={
   1.1, 1, 0,
   2.2, 2, 0,
   3.3, 3, 0,
   4.4, 4, 0,
   5.5, 5, 0,
   6.6, 6, 0
 };

 int irow_index;

 for(irow_index=0; irow_index < iBASES; irow_index++)
   dpowers[irow_index][iRESULT] =
     pow(dpowers[irow_index][iBASE],
     dpowers[irow_index][iRAISED_TO]);

 for(irow_index=0; irow_index < iBASES; irow_index++) {
   printf(“    %d\n”,(int)dpowers[irow_index][iRAISED_TO]);
   printf(“ %2.1f = %.2f\n\n”,dpowers[irow_index][iBASE],
                              dpowers[irow_index][iRESULT]);
 }

 return(0);
}
The array dpowers was declared to be of type double because the function pow( ) expects two double variables and returns a double. Of course, you must take care when initializing two-dimensional arrays; you must make certain you know which dimension is increasing the fastest. Remember, this is always the rightmost dimension.
The output from the program looks like this:
 1
1.1 = 1.10

  2
2.2 = 4.84

  3
3.3 = 35.94

  4
4.4 = 374.81

  5
5.5 = 5032.84

  6
6.6 = 82653.95

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net