Array
- Michael Felix

- Dec 13, 2018
- 1 min read
An array is a collection of data values of the same type in a certain order that uses the same name.
According to its dimensions, arrays can be divided into:
1. One dimensional array
o Each array element can be accessed through an index
o The index array by default starts at 0
o Array declaration:
array_name [size]Example
// Program to find the average of x (x < 10) numbers using arrays
#include <stdio.h>
int main()
{
int marks[10], i, x, sum = 0, average;
printf("Enter number: ");
scanf("%d",&x);
for(i=1; i<=x; ++i)
{
printf("Enter marks: " );
scanf("%d",&marks[i]);
sum += marks[i];
}
average = sum/x;
printf("Average = %d",average);
getchar();
return 0;
}2. An array of two dimensions
- A two-dimensional array is an array consisting of n rows and n columns. The form can be a matrix or table.
- array declaration:
array_name [row][column]Example
#include <stdio.h>
int main() {
int x[2][3] = {{1, 2, 3}, {4, 5, 6}}; // initialize data
int i, j;
for (i=0; i<2; i++) { // first for
for (j=0; j<3; j++) { // second for
printf("%d ", x[i][j]); // print array
}
printf("\n");
}
return 0;
}3. Multidimensional arrays
- Multidimensional arrays are arrays that have more than two sizes. The form of multidimensional array declaration is tantamount to declarations of one and two dimensional arrays.
- array declaration:
array_name [size1][size2]...[sizeN]
Comments