c arrays with programming examples


Arrays
An ARRAY is a COLLECTION of SIMILAR ELEMENTS.
Their SIMILAR ELEMENTS can be all INTEGERS or all FLOATS or all CHARS.
The ARRAY of CHARACTERS is called as a STRING or WORD.
The ARRAY of INTEGERS or FLOATS is called by the original name as ARRAY.
The FIRST ELEMENT in the ARRAY is numbered as 0, there fore the LAST
ELEMENT is 1 LESS THAN the original size of the ARRAY.
An ARRAY is also termed as SUBSCRIPTED VARIABLE.
The Elements inside an ARRAY are STORED in CONTINUIOUS MEMORY
LOCATIONS, IRRESPECTIVE of its SIZE.
Array Declaration:
Syntax:
<datatype> Array_Name[array_size];
Example:
int value[40];
float amount[10];
char names[20];
Array Initialization:
Specifying an initial value into an array at the time of its declaration is array
initialization
int num[6] = {10, 3, 4, 9, 8, 9};
float interest[] = {10.5, 2.6, 3.7};
If the array is initialized at its declaration then the dimension of the array is
optional.
Until the array elements are not initialized they are occupied by garbage
values.
In order to secure an array from garbage values it can be declared as static.
Example:
static char string[25];
static float array[25];
Name of an array indicated the address of the first cell
a[0] indicates the data in the first cell.
a[j] indicates the data in the jth cell.
&a[j] returns the address of the jth cell.


Illustrative Examples
Program to accept and display numbers in original order
Program
#include<stdio.h>
#define N 10
void main()
{
int a[N], k;
for(k = 0; k < N; k++)
{
printf("\nEneter The Element For a[%d] : ", k);
scanf("%d", &a[k]);
}
printf("\nWriting The Numbers in the Original Order...Please Wait...\n");
for(k = 0; k < N; k++)
printf("\nThe Element in Index a[%d] = %d", k, a[k]);
}
Program to calculate sum of 10 Numbers in an array
Program
#include<stdio.h>
void main()
{
int array[10], j, sum = 0;
printf("\nEnter 10 Numbers : ");
for(j = 0; j <= 9; j++)
scanf("%d", &array[j]);
printf("\nCalculating The Sum of Elements in the Array : ");
for(j = 0, sum = 0; j <= 9; j++)
sum += array[j];
printf("\nSum of the Elements of the Array = %d", sum);
}


No comments: