An Array is a series of elements of the same type placed in Contiguous Memory Locations that can be individually referenced by adding an index to a unique Identifier.
That means that, for example, we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different Identifier. Instead of that, using an Array we can store 5 different values of the same type, int for example, with a Unique Identifier. For example, an Array to contain 5 integer values of type int called Students_Id could be represented like this:
Students_Id:0 |
1 |
2 |
3 |
4 |
Initializing Array:
There are various ways to initialize an array:
Using method 1) we have to give elements to be stored in the array in curly brackets. The amount of values between braces { } must not be larger than the number of elements that we declare for the array between square brackets [ ].
In method 2) we don’t need to fix the no of elements. It is determined automatically and stored in the array. Alternatively we can store elements in array element by element.
Accessing Elements:Remember that index always starts with 0. Hence to access first element we have to put 0 at the place of index.
Example:Multidimensional arrays can be described as "arrays of arrays". For example, a Bidimensional Array can be imagined as a bidimensional table made of elements, all of them of a same uniform data type. The Array has two subscripts. One subscript denotes the row & the other the column.
The declaration of Two Dimension Arrays is as follows:
data_type array_name[row_size][column_size];
int m[10][20]
Matrix Using Multi-Dimensional Array:
#include <stdio.h>
#include <conio.h>
int main()
{
int matrix[3][4];
int i,j;
// Initializing matrix
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("\nEnter the value of element at %dth row and %dth column: ",i,j);
scanf("%d",&matrix[i][j]);
}
}
printf("\nPrinting matrix of 3X4 ... \n\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
printf("\t%d",matrix[i][j]);
printf("\n");
}
getch();
return;
}
Output is shown below:
Urgenthomework helped me with finance homework problems and taught math portion of my course as well. Initially, I used a tutor that taught me math course I felt that as if I was not getting the help I needed. With the help of Urgenthomework, I got precisely where I was weak:
Read More
Follow Us