You are on page 1of 23

Arrays

in
C Programming

V.V. Subrahmanyam
SOCIS, IGNOU
Date: 24-05-08
Time: 11-00 to 11-45
Introduction
 Many programs require the
processing of multiple, related data
items that have common
characteristics like list of numbers,
marks in a course etc..
Example
 Consider to store marks of five
students. They can be stored using
five variables as follows:

int ml,m2,m3,m4,m5;

Now, if we want to do the same thing


for 100 students in a class then one
will find it difficult to handle 100
variables.
 In such situations it is often
convenient to place the data
items into an array, where they
will share the same name with a
subscript.
Characteristic Features of an Array

 An array is a collection of similar


kind of data elements stored in
adjacent memory locations and are
referred to by a single array-name.
 Arrays are defined in much the
same manner as ordinary variables,
except that each array name must
be accompanied by a size
specification.
Contd…
 In the case of C, you have to
declare and define array before it
can be used.
 Declaration and definition tell the
compiler the name of the array,
the data type of the elements,
and the size or number of
elements.
Syntax of an Array Declaration
data-type array_name [size];
 Data-type refers to the type of elements
you want to store
 size is the number of elements

Examples:
int char[80];
float farr[500];
static int iarr[80];
char charray[40];
int ar[100];

2001 2003 2199

 In the above figure, as each integer value


occupies 2 bytes.
 200 bytes of consecutive memory
locations were allocated in the memory.
Points to Remember
There are two things to remember for
using arrays in C:
 The amount of storage for a declared
array has to be specified at compile time
before execution. This means that an
array has a fixed size.
 The data type of an array applies
uniformly to all the elements; for this
reason, an array is called a
homogeneous data structure.
Use of Symbolic Constant
To declare size of the array it would be better to use the
symbolic constant as shown below:

#include< stdio.h >


#define SIZE 100
main( )
{
int i = 0;
int stud_marks[SIZE];
for( i = 0;i<SIZE;i++)
{
printf (“Element no. =%d”,i+1);
printf(“ Enter the value of the element:”);
scanf(“%d”,&stud_marks[i]);
}
Array Initialization
 Arrays can be initialized at the time
of declaration.

The syntax is:

datatype array-name[ size ] = {val 1, val 2, .......val n};


Examples
int digits [10] = {1,2,3,4,5,6,7,8,9,10};
int digits[ ] = {1,2,3,4,5,6,7,8,9,10};

char thing[4] = “TIN”;


char thing[ ] = “TIN”;

Note: A special character called null character ‘ \0 ’,


implicitly suffixes every string.
/* Linear Search*/

# include<stdio.h>
# define SIZE 05
main()
{
int i = 0;
int j;
int num_list[SIZE];
printf(“Enter any 5 numbers: \n”);
for(i = 0;i<SIZE;i ++)
{ printf(“Element no=%d Value of the element=”,i+1);
scanf(“%d”,&num_list[i]); }
printf (“Enter the element to be searched:”);
scanf (“%d”,&j);
/* search using linear search */
for(i=0;i<SIZE;i++)
{
if(j == num_list[i])
{ printf(“The number exists in the list at position: %d\n”,i+1);
break; }
}
}
Multidimensional Arrays

 In principle, there is no limit to the


number of subscripts (or dimensions)
an array can have.
 Arrays with more than one
dimension are called multi-
dimensional arrays.
Syntax

datatype array_name[size1][size2];
Examples

float table[50][50];
int a[100][50];
char page[24][80];
Illustration of a 3X3 Array
Initialization of 2 Dimensional arrays
int table[2][3] = { 1,2,3,4,5,6 };

Or

int table[2][3] = { {1,2,3},


{4,5,6} };

It means that element:


table [ 0][0] = 1;
table [ 0][1] = 2;
table [ 0][2] = 3;
table [ 1][0] = 4;
table [ 1][1] = 5;
table [ 1][2] = 6;
Transpose of a Matrix
#include<stdio.h>
#defind SIZE 3
main()
{
int mat[SIZE][SIZE];
int i,j;
printf(“Enter the elements of the matrix\n”);
for(i=0;i<SIZE;i++)
{
for (j=0;j<SIZE;j++)
{ scanf(“%d”,&mat[i][j]);
}
}
printf(“Transpose of the matrix\n”);
for(i=0;i<SIZE;i++)
{ for (j=0;j<SIZE;j++)
{printf(“%d”,&mat[j][i]);
}
printf(“\n”);
}
}
Addition of Two Matrices
#include<stdio.h>
#defind SIZE 3
main()
{
int a[SIZE][SIZE], b[SIZE][SIZE];
int i,j;
printf(“Enter the elements of the matrix A\n”);
for(i=0;i<SIZE;i++)
{
for (j=0;j<SIZE;j++)
scanf(“%d”,&a[i][j]);
}
printf(“Enter the elements of the matrix B”);
for(i=0;i<SIZE;i++)
{ for (j=0;j<SIZE;j++)
scanf(“%d”,&b[i][j]);
}
Contd…
printf(“\n Matrix Addition\n”);
for(i=0;i<SIZE;i++)
{
for (j=0;j<SIZE;j++)
printf(“%d”,a[i][j]+b[i][j]);
printf(“\n”);
}
}
Passing Arrays to Functions
 An entire array can be passed to a
function as an argument.
 To pass an array to a function, the
array must appear by itself, without
brackets or subscripts, as an actual
argument within the function call.
 The size of the array is not specified
within the formal argument
declaration.
Illustration
#include<stdio.h>
main()
{
int n;
float average(int a, float x[]);
float avg;
float list[100];
…….
…….

avg=average(n,list);

….
}

float average(int a, float x[])


{
……….
}

You might also like