Arrays in C': Shashidhar G Koolagudi CSE, NITK, Surathkal
Arrays in C': Shashidhar G Koolagudi CSE, NITK, Surathkal
Arrays in C': Shashidhar G Koolagudi CSE, NITK, Surathkal
in ‘C’
Shashidhar G Koolagudi
CSE, NITK, Surathkal
Introduction
In C language, arrays are referred to as structured data
types.
An array is defined as
Finite ordered collection of homogenous data, stored in
contiguous memory locations.
Examples:
• List of Employee or Student names,
• Marks of students,
• List of numbers or characters etc.
Declaration of Arrays
• Like any other variable, arrays must be declared before they are used.
General form of array declaration is,
• data-type variable-name[size];
• int arr[10];
Here int is the data type, arr is the name of the array and 10 is the size of array. It means array arr can only contain 10 elements
of int type.
Index of an array starts from 0 to size-1 i.e first element of arr array will be stored at arr[0] address and the last element will
occupy arr[9].
This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid
C data type. For example, to declare a 10-element array called balance of type double, use this statement −
double balance[10];
You can use array subscript (or index) to access any element stored in array. Subscript starts with 0, which means arr[0] represents the first element in the array
arr.
In general arr[n-1] can be used to access nth element of an array. where n is any integer number.
Initialization of an Array
After an array is declared it must be initialized. Otherwise, it will
contain garbage value(any random value). An array can be initialized at
either compile time or at runtime.
Compile time initialization of array elements is same as
ordinary variable initialization. The general form of initialization of array
is,
data-type array-name[size] = { list of values };
Here are a few examples
int marks[4]={ 67, 87, 56, 77 }; // integer array initialization
float area[5]={ 23.4, 6.8, 5.5 }; // float array initialization
int marks[4]={ 67, 87, 56, 77, 59 }; // Compile time error
Different initializations during declaration
Memory status in a system after array is initialized.
int val[7];
Name of an array always contains the address of the first element of an array, It is also the same as the address of the element val[0].
#include <stdio.h>
int main()
{
int avg = 0; int sum =0; int x=0; /* Array- declaration – length 4*/
int num[4]; /* We are using a for loop to traverse through the array * while
storing the entered values in the array */
for (x=0; x<4;x++)
{
printf("Enter number %d \n", (x+1));
scanf("%d", &num[x]);
}
for (x=0; x<4;x++)
{
sum = sum+num[x]; /*sum+=num[x];*/
}
avg = sum/4;
printf("Average of entered numbers is: %d", avg);
return 0;
}
Another simple programme
#include <stdio.h>
int main ()
{
int n[ 10 ]; /* n is an array of 10 integers */
int i,j; /* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100; /* set element at location i to i + 100 */
} /* output each array element's value */
for (j = 0; j < 10; j++ )
{ printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}
Programme to reverse the list
#include <stdio.h>
void main()
{
int i,n,a[100];
printf("\n\nRead n number of values in an array and display it in reverse order:\n");
printf("------------------------------------------------------------------------\n");
printf("Input the number of elements to store in the array :");
scanf("%d",&n);
printf("Input %d number of elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}
printf("\nThe values store into the array are : \n");
for(i=0;i<n;i++)
{
printf("% 5d",a[i]);
}
printf("\n\nThe values store into the array in reverse are :\n");
for(i=n-1;i>=0;i--)
{
printf("% 5d",a[i]);
}
printf("\n\n");
}
Flow of execution
Programme to separate out even and odd numbers in two arrays
#include <stdio.h>
void main()
{
int arr1[10], arr2[10], arr3[10];
int i,j=0,k=0,n;
printf("\n\n");
}
Flow Chart
Programme to count the frequency of each element in the array
main()
{
int arr1[100], fr1[100];
int n, i, j, ctr;
printf("\n\nCount frequency of each element of an array:\n");
printf("------------------------------------------------\n");
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
fr1[i] = -1;
}
for(i=0; i<n; i++)
{
ctr = 1;
for(j=i+1; j<n; j++)
{
if(arr1[i]==arr1[j])
{
ctr++; fr1[j] = 0;
}
if(fr1[i]!=0)
{
fr1[i] = ctr;
}
}
printf("\nThe frequency of all elements of array : \n");
for(i=0; i<n; i++)
{
Programme- Bubble sorting
#include <stdio.h>
int main()
{
int array[100], n, i, j, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
for (i = 0 ; i < n - 1; i++)
{
for (j = 0 ; j < n - i - 1; j++)
{
if (array[j] > array[j+1]) /* For decreasing order use '<' instead of '>' */
{
swap = array[j];
array[j] = array[j+1];
array[j+1] = swap;
}
}
}
#include <stdio.h>
int main ()
{ /* an array with 5 elements */
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p; int i; p = balance; /* output each array element's value */
printf("value of p is %d and balance is %d\n\n", p, balance);
printf( "Array values using Normal approach\n");
for ( i = 0; i < 5; i++ )
{ printf(" %f is stored in : %dth location \n", balance{i}, i+1 ); }
#include <stdio.h>
int main ()
{ /* an array with 5 elements */
int marks[5], i;
printf( "Enter 5 array elements\n");
for ( i = 0; i < 5; i++ )
scanf("%d", &marks[i]);
printf("Printing array elements using normal and address approach\n\n");
for ( i = 0; i < 5; i++ )
printf("%d is stored in the address %d\n", marks[i], marks+i);
/*Add 5 to each element of teh array marks*/
printf("Array elements after adding 5 to each element\n");
for ( i = 0; i < 5; i++ )
*(marks+i)=*(marks+i)+5;
for ( i = 0; i < 5; i++ )
printf("%d is stored in the address %d\n", marks[i], marks+i);
return 0;
}
Output