40 likes | 145 Views
ARRAYS. Arrays are data structures consisting of related data items of the same type. An array is a group of memory locations – where the elements of the array have the same name and same datatype.
E N D
ARRAYS • Arrays are data structures consisting of related data items of the same type. • An array is a group of memory locations – where the elements of the array have the same name and same datatype. • To refer to a particular element in the array, we specify the name of the array and the position number of the particular element. • The first element in every array is the zeroth element. • The position number contained within square brackets is called a subscript.
Name of the array a[0] a[1] a[2] a[3] a[4] a[5] a[6] Position number/Subscript
Declaring And Initializing Arrays • float a[20]; a is a float array consisting of 20 elements. • int a[8] = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 }; The elements of array a are initialized to the values in braces.
/* Program to calculate the sum of the elements in the array */ #include <stdio.h> #define SIZE 8 int main() { int a[ SIZE ] = {1, 2, 3, 4, 5, 6, 7, 8}; int j , sum = 0; for ( j = 0; j <= SIZE –1 ; j++) sum += a[ j ]; printf(“Sum of the elements is %d “,sum); }