Multi-dimensional Arrays
Multidimensional arrays are defined with a separate pair of square brackets for each subscript.
A multi-dimension array definition may expressed as
storage-class data-type array[expression1] [expression2]...;
storage-class - Storage class of the array (option)
data-type - Data type
array - Array name
[expression1] - Positive integer expressions that indicate the number of array elements associated with each subscript.
| Multidimensional Arrays |
Example: int number[3] [4];
- The above example defines number as an integer array having 3 rows and 4 columns, i.e 3x4 =12 elements.
- if a multidimensional array definition includes the assignment of initial values, then care must be given to the order in which the initial values are assigned to the array elements.
- Thus, the elements of a two-dimensional array will be assigned by rows, i.e., the element of the first row will be assigned, then the element of the second row, and so on.
- The array number represents a table having 3 rows and 4 columns, which looks like this :
- Here the first subscript ranges from 0 to 2, and the second subscript ranges from 0 to 3.
- Multidimensional arrays are processed in the same manner as one-dimensional arrays, on an element-by-element basis.
- The formal argument declarations within the function definition must include explicit size specifications in all of the subscript positions except the first. These size specifications must be consistent with the corresponding size specifications in the calling program. The first subscript position may be written as an empty pair of square brackets.
#define FIRST_DIM 3
#define SECOND_DIM 3
/* accepting integers in a 2-diminsional array & displaying it back */
void main()
{ int i,j;
int double_array[FIRST_DIM][SECOND_DIM];
clrscr();
printf("Enter Integers In An Array, Rowwise\n");
for(i=0; i<FIRST_DIM; i++)
for(j=0; j<FIRST_DIM; j++)
scanf("%d", &double_array[i][j];
for(i=0; i<FIRST_DIM; i++)
printf("\tcol %d\t",i);
printf("\n-----------------------------------");
for(i=0; i<FIRST_DIM; i++)
{
printf(nrow %d", i);
for(j=0; j<FIRST_DIM; j++)
printf('\t%d\t",double_array[i][j]); } }
Program-2
#define MAXROWS20
#define MAXCOLS30
/* accept integers in a 2-diminsional array display totals */
void main()
{ int rows, cols;
int a[MAXROWS][MAXCOLS], b[MAXROWS][MAXCOLS],
c[MAXROWS][MAXCOLS];
- Learn how to write the C program step-by-step | Lesson -1
- Learn how to write the C program step-by-step | Lesson -2
- Learn how to write the C program step-by-step | Lesson -3
- Learn how to write the C program step-by-step | Lesson -4
- Learn how to write the C program step-by-step | Lesson -5
- Learn how to write the C program step-by-step | Lesson -6
- Learn how to write the C program step-by-step | Lesson -7
Let's see also...
- Revolution in Information Technology
- Important Terminology of Computer Science
- Most useful website list for job seekers
- Do you know how to delete Facebook account
- What is TCP/IP Reference Model
No comments:
For More Details Subscribe & Comment..!