Array:: Single / One Dimensional Array
Array:: Single / One Dimensional Array
Array is a collection of homogenous data stored under unique name. The values in an array is called as 'elements of an array.' These elements are accessed by numbers called as 'subscripts or index numbers.' Arrays may be of any variable type. Array is also called as 'subscripted variable.'
Types of an Array :
1. 2. One / Single Dimensional Array Two Dimensional Array
Syntax:
Example:
int a[3] = {2, 3, 5}; char ch[20] = "TechnoExam" ; float stax[3] = {5003.23, 1940.32, 123.20} ;
In above example, a is an array of type integer which has storage size of 3 elements. The total size would be 3 * 2 = 6 bytes.
* MEMORY ALLOCATION :
Program :
#include <stdio.h> #include <conio.h> void main() { int a[3], i;; clrscr(); printf("\n\t Enter three numbers : "); for(i=0; i<3; i++) { scanf("%d", &a[i]); } printf("\n\n\t Numbers are : "); for(i=0; i<3; i++) { printf("\t %d", a[i]); } getch(); } // print array // read array
Features :
o o o
String array always terminates with null character ('\0'). Array elements are countered from 0 to n-1. Useful for multiple reading of elements (numbers).
Disadvantages :
o o
There is no easy method to initialize large number of array elements. It is difficult to initialize selected elements.
Syntax:
Example:
int a[3][3];
In above example, a is an array of type integer which has storage size of 3 * 3 matrix. The total size would be 3 * 3 * 2 = 18 bytes. It is also called as 'multidimensional array.'
* MEMORY ALLOCATION :
Program :
#include <stdio.h> #include <conio.h> void main() { int a[3][3], i, j; clrscr(); printf("\n\t Enter matrix of 3*3 : "); for(i=0; i<3; i++) { for(j=0; j<3; j++) { scanf("%d",&a[i][j]); } } printf("\n\t Matrix is : \n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { printf("\t %d",a[i][j]); } printf("\n"); } getch(); } //print 3*3 array //read 3*3 array
o o
We cannot delete any element from an array. If we dont know that how many elements have to be stored in a memory in advance, then there will be memory wastage if large array size is specified.