c
C Programming Tutorial : Introduction

One Dimensional Array in C:

A one-dimensional array is a structured collection of the components (often called array elements). It can be accessed individually by specifying the position of a component with a single index value.

Declaration of One Dimensional Array

Syntax-

datatype arrayName[size]; 

Here arrayName denotes the name of the array. It can be any valid C identifier. The data type is the type of data for elements of an array. The size of the array specifies the number of elements that can be stored in the array.

Here are some examples of array declarations-

Syntax-

int age[ 100]; .
float sal[15];
char grade[20];
 

Here age is an integer type array, which can store 100 elements of integer type. The array sal is a floating type array of size 15, can hold float values and the third one is a character type array of size 20, can hold characters.

The individual elements of the above arrays are-

age[0], ageI1], age[2], …………………………………… age[99]
sal[0], sal[1], sal[2]! ………………………………………. al[14]
grade[0], grade[1], grade[2], ………………………… grade[19]
 

Initializing Arrays

You can initialize an array in C either one by one or using a single statement as follows –

Example-

int mark[5] = {19, 10, 8, 17, 9}; 

The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].

If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, you can also initialize an array like this also.

int mark[] = {19, 10, 8, 17, 9}; 

The symbolic constants can also be used to specify the size of the array.

For example-

#define SIZE 10
main( )
{
int size=15;
float sal [SIZE];
int marks [ SIZE];
…………….
…………….
}
 

The use of symbolic constant to specify the size of the array makes it convenient to modify the program if the size of the array is to be changed later because the size must be changed only in one place i.e. In the #define directive.

Accessing Array Elements:

The elements of an array can be accessed by specifying the array name followed by subscript in brackets. In C, the array subscripts start from 0. Hence if there is an array of size 5 then the valid subscripts will be from 0 to 4.

Let us take an array-

int arr[5]; 	/*Size of array arr is 5, can hold five integer elements*/ 

The elements of this array are-

arr[0], arr[1], arr[2], arr[3], arr[4] 

Here 0 is the lower bound and 4 is the upper bound of the array.

The following example shows how to use all the three above mentioned concepts viz. declaration, assignment, and accessing arrays −

#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;
}
 

When the above code is compiled and executed, it produces the following result −

Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109

Processing One dimensional Arrays

For processing arrays, we generally use a for a loop. The loop variable is used at the place of the subscript. The initial value of the loop variable is taken 0 since array subscripts start from zero. The loop variable is increased by 1 each time so that we can access and process the next element in the array. The total number of passes in the loop will be equal to the number of elements in the array. In each pass, we will process one element.

Suppose arr[10] is an array of int type.

(i) Reading values in arr[10]

For( i = 0; i < 10; i++)

scanf(“%d”, &arr[i]);

(ii) Displaying values of arr[ 10]

For( i = 0; i<10; i++)

printf(%d “, arr[i]);

(iii) Adding all the elements of arr[ 10]

sum=0;

for( i = 0; i<10; i++)

sum+=arr[i];

Example:1

/*Program to input values into an array and display them*/
#include<stdio.h>
main()
{
int arr[5J,i;
for(i=0;i<5;i++)
{
printf ("Enter the value for arr[ %d]:”, i);
scanf("%d",&arr[i));
}
printf ("The array elements are :\n”);
for(i=0;i<5;i++)
printf("%d\t",arr[i]);
printf ("\n");
}
 

Example:2

/*Program to add the elements of an array*/
#include<stdio.h>
main ()
{

int arr[10], i,sum=0;
for(i=0;i<10;i++)
{
printf ("Enter the value for arr [%d] :”,i);
scanf("%d”,&arr[i]);
sum+=arr[i];
}
printf ("Sum = %d\n", sum) ;
}
 

Example:3

/*Program to count the even and odd numbers in a array*/
#include<stdio.h>
#define SIZE 10
main()
{
int arr [SIZE], I, even=0, odd=0;
for(i=0; i<SIZE; i++)
{
printf ("Enter the value for arr lId] : “,i)
scanf("%d",&arr[i]) ;
if(arr[i]%2==0)
even++;
else
odd++;
}
print£: ("Even numbers %d, Odd numbers =%d\n", even, odd );
}
 

Example:4

/ *Program to find the maximum and minimum number in an array*/
#include<stdio.h>
main ( )
{
int i, j , arr[ 10] = { 2 , 5, 4, 1, 8, 9, 11 , 6, 3 , 7} ;
int min, max;
min=max=arr[O] ;
for(i=l;i<lO;i++)
{
if(arr[i]<min)
min=arr[i] ;
if(arr[i]>max)
max=arr[i] ;
}
printf("Minimum %d, Maximum'%d\n" ,min, max) ;

}

Output:
Minimum=1,	 Maximum=11
 

We have taken the value of first element as the initial value of min and max. Inside the for loop, we will start comparing from second element onwards so this time we have started the loop from 1 instead of 0.

Example:5

/* Program to reverse "the elements of an array*/
#include<stdio.h>
main ( )
{
int i, j, temp, arr[10)={1,2,3,4,5,6,7,8,9,10}:
for(i=0, j=9; i<j; i++, j- -)
{
temp=arr[i);
arr [i) =arr [j) ;.
arr[j]=temp;
}
printf ("After reversing the array is );
for(i=0; i<10; i++)
printf("%d",arr[i]);
printf("\n");
}

Output:
After reversing the array is 10 9 8 7 6 5 4 3 2 1
 

In the for loop, we have used a comma operator and taken two-variable i and j. The variable i is initialized with the lower bound and j is initialized with an upper bound. After each pass of the loop, i is incremented while j is decremented.

Inside the loop, a[i] is exchanged with a(j]’ So a[O] will be exchanged with a[9], a[l] with a[8], a[2] with a[7] and so on.

Example:6

/*Program to convert a decimal number to binary number */
#include<stdio.h>
main( )
{
int num, rem, arr [ 15) , i, j ;
printf ("Enter a decimal number :”);
scanf("%d",&num) ;
i=0;
while(num>0)
{
arr [i) =num%2;  	/* s tore the remainder in array*/
num/=2;
i++;
}
printf ("Binary number is ");
for(j=i-1;j>=0;j- -)	/*print the array backwards*/
printf("%d",arr[j]);
printf("\n");
}
Output:
Enter a decimal number: 29
Binary number is: 11101
 

Example:7

/*Program to search for an item in the array*/
# include<stdio. h>
#define SIZE 10
main ()
{
int i, arr[SIZE)={23,12,56,98,76,14,65,11,19,45};
int item;
printf ("Enter the i tern to be searched ") ;
scanf("%d",&item);
for(i=0;i<SIZE;i++)
{
if(item==arr[i))
{
printf ("%d found at position %d\n", item, i+1);
break;
}
}
if(i==SIZE)
printf("Item %d not found in array\n" ,item);
}
 

The item to be searched is compared with each element of the array if the element is found, for loop is terminated through break statement. The control can come out of the loop in two cases, first if the item is found and loop is terminated through the break and second if the item is not found and the loop is fully executed. In the second case, the value of i would be equal to SIZE. Here we are searching the item sequentially, so this search is known as linear or sequential search.