Arrays
Array Declaration:
Syntax: data type arrayname [size];
Size indicates maximum no of elements that can be stored inside an
array. Size is an integer constant, an Integer variable or more complex
integer expression
Ex. int x[5];
- Array is a collection of elements.
- All elements are of same type.
- All elements are stored using contiguous memory.
- int a[5], declares an integer array of five elements.
- Elements of an array can be initialized at the time of declaration.
int a [5] = {1,2,3,4,5};
It is also possible to initialize individual elements as follows:
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
a[4]=5;
- A particular value is accessed by writing a number called index number of subscript in brackets after the array name. For example, a[2] represents the third element of the array.
Initializing arrays.
When declaring an array of local scope (within a function), if we do
not specify otherwise, it will not be initialized, so its content is
undetermined until we store some values in it.
If we declare a global array (outside any function) its content will be
initialized with all its elements filled with zeros. Thus, if in the
global scope we declare: int x[ 5 ];
Every element of x will be set initialized to 0:
But additionally, when we declare an Array, we have the possibility to
assign initial values to each one of elements using curly brackets {
}
For example: int x[5]= {16, 2, 77, 40, 12071};
This declaration would have created an array like the following one:
The number of elements in the array that we initialized within curly
brackets () must match the length in elements that we declared for the
array enclosed within square brackets [ ]
Also, the size of the Array will be defined by the number of values
included between curly brackets { }
int x= [ ] = {16, 2, 77, 40, 12071 };
Access to the values of an Array- We can access individually anyone of its values for reading or
modifying as if it was a normal variable.
The format is the following name[index]
Brackets [ ] perform two different tasks one is to set the size
of arrays when declaring them, and second is to specify indices for a
concrete array element when referring to it. We must simply take care
not to confuse these two possible uses of brackets [ ] with arrays;
int x[5]; //
declaration of a new Array (begins with a type name)
x[2] =75; // access to an element of the Array.
In an array, element a[1] can be manipulated like a normal variable
a[5]=0;
a[5]=a[4]+2
Other valid operations with arrays;
x[0] = a;
x[a]=75;
b=x[a+2];
Memories address calculation Mechanism:
arrayname [index] base address index+ sizeof(datatype)
2. Dimensional Array:
It is used to store the data in the matrix format.
Syntax:
Datatype arrayname [ M ] [ N ];
Where M is no of Rows and N is no of columns.
For ROWS array Index start from 0 to M-1.
For COLUMNS array Index start from 0 to N-1
No. of elements in the 2D Array M * N.
Memory requirement (M* N * size of datatype) Bytes
Ex. int a[3][3]:
No. of elements in the 2D Array-33-9
For ROWS array Index start from 0 to 2.
For COLUMNS array Index start from 0 to 2.
No. of Elements in the 2D Array = 3 * 3= 9
Memory Requirement = 3 * 3 * 2=18 Bytes
Memory Map (How the 2-D array will be stored inside the RAM?)
Passing array elements to a function:
Array elements can be passed to a function by calling the function by
value or by reference. In the call by value we pass the value of array
elements and in call by reference.
we pass addresses of array elements to the function. Let us consider a
program:
Ex: WAP to find the average of a number by passing array to the
function:
Code
#include<stdio.h float average (int x[],int n); void main() { int sum-0,n; int i, a[100]; printf (“\n How many numbers:"); scanf(“%d”,&n); printf("Enter the numbers”); for (i=0; i<n; i++) { scanf(“%d”, &a[i]); } sum= average (a, n); printf(“\nSum of marks is =%d",sum); printf(“\n Average of marks is %f", (sum/n)); } float average (int a[ ], int n) { float s= 0, avg; for(int i=0;i<n;j++) { s=s + a[ i]; } avg=s/n; return (avg); }
Post a Comment