Professional Documents
Culture Documents
P8 Arrays
P8 Arrays
Sequence of memory elements of same data type. Sequence of 10 integers stored one after another in memory represents an array. int a[10];
ARRAYS
Fixed number of data items of same type. Data items-Elements Type int ,float and long etc.. Array of datatype character - strings
ARRAY Declaration
Data type int a[10] Name of array Size of array
a array of 10 integers Square braces represents number of elements Array index starts from a[0] as shown below:
Note: In
Initialization of ARRAY
Can be initialized once they are declared. int a[10]={28,15,86,54,78,21,34,7,91,67}; Values should be given in curly brackets. Less than 10 elements-0 will be filled. Greater than 10 elements-excessive element declared warning will be reported. List of values-separated by commas.
ARRAYS
To handle matrix and tables, we need array with multiple subscripts. Two-dimensional array-two subscripts Row and column-starts with zero 2-D array can also be called as matrix.
float marks[4][3]
Name of the array 0 1 40.5 55.5 70.0 85.0 2 45.5 60.5 75.0 90.0
0 1 2 3
Marks[0][0] Marks[0][1]
Marks[1][0]
Marks[2][0]
Marks[3][0]
Write a program to add two matrices using function Eg: function addmat()
POINTER TO ARRAY
main() { int num[ ]={24,34,12,44,56,17}; int i=0,*j; j=&num[0]; while(i<=5) { printf(Address=%u \n,&num[i]); printf(Element=%d \n,*j); i++; j++; } }
POINTER TO ARRAY
Accessing array elements using pointer is faster than accessing them by subscripts. Should be accessed using pointers, if the elements are to be accessed in a fixed order. If there is no fixed logic, then subscript can be used but pointer will be faster always. Pointer notations: num[i] = *(num+i) = *(i+num) = i[num]
I request Electronics and communication ENGINEERING students to visit my blog for more abhishek1ek.blogspot.com awhengineering.blogspot.com