Array Initialization: An array can be initialized by assigning with a list of values enclosed in a pair
of braces.
Eg., int a[5] = { 10, 20, 35, 45, 100 };
The values are stored into array by starting from first cell (index 0) onwards.
If an array is not initialized, by default the array elements are assigned with garbage values.
If the number of values given for initialization are less than array size, the leftover elements are
assigned with 0 (zero).
Eg., int a[5] = { 10, 20, 35 };
In this case, the elements with index numbers 3 and 4 are initialized with 0.
*****
/* Program to store values into an array of 5 cells and print values stored and sum of values */
#include <stdio.h>
void main()
{
int a[5], I, sum=0;
for (i=0; i<=4; i++)
{ printf(“Enter value into cell %d : “, i);
scanf(“%d”, &a[i]);
sum += a[i];
}
printf (“\nValues stored into array..”);
for(i=0; i<5; i++)
printf(“\nCell %d”, a[i]);
printf(“\nSum of values : %d”, sum);
}
****
/* Program to find minimum, maximum, and average in an array of integers */
#include <stdio.h>
void main()
{
int a[10], i, min, max, sum;
float avg;
printf("Enter value : ");
scanf ("%d", &a[0]);
min = max = a[0];
sum = a[0]; /* store first element value in to sum */
for(i=1; i<10; i++)
{
printf("Enter next value : ");
scanf ("%d", &a[i]);
sum += a[i]; /* add each element value to sum */
if (a[i] < min)
min = a[i];
if (a[i] > max)
max = a[i];
}
avg = (float)sum / i; /* calculate average of 10 elements */
printf("\nMinimum value : %d", min);
printf("\nMaximum value : %d", max);
printf("\nAverage value : %f", avg);
}
Page 1 of 2
/* Program to reverse values of an array */
#include <stdio.h>
#define SIZE 10
void main()
{
int a[SIZE], i, t;
printf("Enter values into array..\n");
for(i=0; i<SIZE; i++)
{
printf("Enter values into cell %d : ", i);
scanf("%d", &a[i]);
}
for(i=0; i<=SIZE/2; i++)
{
t = a[i];
a[i] = a[SIZE-1-i];
a[SIZE-1-i] = t;
}
printf("\nArray values in reverse..\n");
for(i=0; i<SIZE; i++)
printf("%5d", a[i]);
}
****
/* Program to sort values of an array */
#include <stdio.h>
#define SIZE 10
void main()
{
int a[SIZE], i,j,t;
/* Input values into array */
printf("Enter values into array..\n");
for(i=0; i<SIZE; i++)
{
printf("Enter cell %d value : ",i);
scanf("%d", &a[i]);
}
/* Sorting */
for (i=0; i<= SIZE-2; i++)
{
for(j=i+1; j<=SIZE-1; j++)
{
if(a[i] > a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
/* Output */
printf("\nArray values after sorting..\n");
for(i=0; i<SIZE; i++)
printf("%5d", a[i]);
Page 2 of 2