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.
Example of two-dimensional array definition
int number[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
- The array number represents a table having 3 rows and 4 columns, which looks like this :
number[0][0] = 1 number[0][1] = 2 number[0][2] = 3 number[0][3] = 4
number[1][0] = 5 number[1][1] = 6 number[1][2] = 7 number[1][3] = 8
number[2][0] = 9 number[2][1]=10 number[2][2] =11 number[1][3]=12
- 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.
#include <stdio.h>
#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]); } }
#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
#include <stdio.h>
#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];
#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];
void get_input(int a[ ] [MAXCOLS], int rows, int cols);
void add_tables(int a[ ] [MAXCOLS], int b[][MAXCOLS], int c[][MAXCOLS], int rows, int cols);
void display_output(int c[ ] [MAXCOLS], int rows, int cols);
printf("How Many Rows :");
scanf("%d", &rows);
printf("How Many Columns :");
scanf("%d", &cols);
printf("\nFirst table : \n");
get_input(a, rows, cols);
printf("\nSecond table : \n");
get_input(b,rows, cols);
add_tables(a,b,c,rows cols);
printf("\nTotal Of All The elements : \n");
display_output(c,rows,cols);
}
{ int row, col;
for(row=0; row<m; ++row)
{ printf("Enter data for row no. %2d\n", row +1);
for(col=0; col<n; ++col)
scanf("%d", &a[row][col]);
}
return;
}
void add_tables(int a[] [MAXCOLS], int b[][MAXCOLS], int c[][MAXCOLS], int m, int n)
{ int row, col;
for(row=0; row<m; ++row)
{ printf("Enter data for row no. %2d\n", row +1);
for(col=0; col<n; ++col)
c[row][col] = a[row][col] + b[row][col];
return;
}
void display_output(int c[ ] [MAXCOLS], int rows, int cols);
{ int row, col;
for(row=0; row<m; ++row)
for(col=0; col<n; ++col)
printf("%4d"' a[row][col]);
printf("\n");
}
return;
}
See also:
Exercise regularly. Eat a nutritious diet. Don't smoke - WHO (Covid-19).
- 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
Thanks a lot for reading.
Neel Kamal
What is Multi-dimensional Arrays in C programming
Reviewed by Neel Kamal
on
October 22, 2021
Rating:
No comments:
For More Details Subscribe & Comment..!