The document discusses arrays in C programming. It defines an array as a collection of similar data elements stored in adjacent memory locations that share a single name. Arrays allow storing multiple values of the same type using this single name. The document covers array declaration syntax, initialization, passing arrays to functions, and multidimensional arrays. It provides examples of one-dimensional and two-dimensional arrays as well as operations like matrix addition and transpose.
2. IntroductionIntroduction
Many programs require theMany programs require the
processing of multiple, related dataprocessing of multiple, related data
items that have commonitems that have common
characteristics likecharacteristics like list of numbers,list of numbers,
marks in a course etc..marks in a course etc..
3. ExampleExample
Consider to store marks of fiveConsider to store marks of five
students. They can be stored usingstudents. They can be stored using
five variables as follows:five variables as follows:
int ml,m2,m3,m4,m5;int ml,m2,m3,m4,m5;
Now, if we want to do the same thingNow, if we want to do the same thing
for 100 students in a class then onefor 100 students in a class then one
will find it difficult to handle 100will find it difficult to handle 100
variables.variables.
4. In such situations it is oftenIn such situations it is often
convenient to place the dataconvenient to place the data
items into an array, where theyitems into an array, where they
will share the same name with awill share the same name with a
subscript.subscript.
5. Characteristic Features of an ArrayCharacteristic Features of an Array
An array is a collection of similarAn array is a collection of similar
kind of data elements stored inkind of data elements stored in
adjacent memory locations and areadjacent memory locations and are
referred to by a single array-name.referred to by a single array-name.
Arrays are defined in much theArrays are defined in much the
same manner as ordinary variables,same manner as ordinary variables,
except that each array name mustexcept that each array name must
be accompanied by a sizebe accompanied by a size
specification.specification.
6. Contd…Contd…
In the case of C, you have toIn the case of C, you have to
declare and define array before itdeclare and define array before it
can be used.can be used.
Declaration and definition tell theDeclaration and definition tell the
compiler the name of the array,compiler the name of the array,
the data type of the elements,the data type of the elements,
and the size or number ofand the size or number of
elements.elements.
7. Syntax of an Array DeclarationSyntax of an Array Declaration
data-type array_name [size];data-type array_name [size];
Data-typeData-type refers to the type of elementsrefers to the type of elements
you want to storeyou want to store
sizesize is the number of elementsis the number of elements
Examples:Examples:
int char[80];int char[80];
floatfloat farr[500];farr[500];
static int iarr[80];static int iarr[80];
charchar charray[40];charray[40];
8. int ar[100];int ar[100];
In the above figure, as each integer valueIn the above figure, as each integer value
occupies 2 bytes.occupies 2 bytes.
200 bytes of consecutive memory200 bytes of consecutive memory
locations were allocated in the memory.locations were allocated in the memory.
2001 2003 2199
9. Points to RememberPoints to Remember
There are two things to remember forThere are two things to remember for
using arrays in C:using arrays in C:
The amount of storage for a declaredThe amount of storage for a declared
array has to be specified atarray has to be specified at compile timecompile time
before execution. This means that anbefore execution. This means that an
array has a fixed size.array has a fixed size.
The data type of an array appliesThe data type of an array applies
uniformly to all the elements; for thisuniformly to all the elements; for this
reason, an array is called areason, an array is called a
homogeneoushomogeneous data structure.data structure.
10. Use of Symbolic ConstantUse of Symbolic Constant
To declare size of the array it would be better to use theTo declare size of the array it would be better to use the
symbolic constant as shown below:symbolic constant as shown below:
#include< stdio.h >#include< stdio.h >
#define SIZE 100#define SIZE 100
main( )main( )
{{
int i = 0;int i = 0;
int stud_marks[SIZE];int stud_marks[SIZE];
for( i = 0;i<SIZE;i++)for( i = 0;i<SIZE;i++)
{{
printf (“Element no. =%d”,i+1);printf (“Element no. =%d”,i+1);
printf(“ Enter the value of the element:”);printf(“ Enter the value of the element:”);
scanf(“%d”,&stud_marks[i]);scanf(“%d”,&stud_marks[i]);
}}
11. Array InitializationArray Initialization
Arrays can be initialized at the timeArrays can be initialized at the time
of declaration.of declaration.
The syntax is:The syntax is:
datatype array-name[ size ] = {val 1, val 2, .......val n};datatype array-name[ size ] = {val 1, val 2, .......val n};
12. ExamplesExamples
int digits [10] = {1,2,3,4,5,6,7,8,9,10};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};int digits[ ] = {1,2,3,4,5,6,7,8,9,10};
char thing[4] = “TIN”;char thing[4] = “TIN”;
char thing[ ] = “TIN”;char thing[ ] = “TIN”;
Note:Note: A special character called null character ‘ 0 ’,A special character called null character ‘ 0 ’,
implicitly suffixes every string.implicitly suffixes every string.
13. /* 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: %dn”,i+1);
break; }
}
}
14. Multidimensional ArraysMultidimensional Arrays
In principle, there is no limit to theIn principle, there is no limit to the
number of subscripts (or dimensions)number of subscripts (or dimensions)
an array can have.an array can have.
Arrays with more than oneArrays with more than one
dimension are calleddimension are called multi-multi-
dimensional arraysdimensional arrays..
19. Transpose of a MatrixTranspose of a Matrix
#include<stdio.h>#include<stdio.h>
#defind SIZE 3#defind SIZE 3
main()main()
{{
int mat[SIZE][SIZE];int mat[SIZE][SIZE];
int i,j;int i,j;
printf(“Enter the elements of the matrixn”);printf(“Enter the elements of the matrixn”);
for(i=0;i<SIZE;i++)for(i=0;i<SIZE;i++)
{{
for (j=0;j<SIZE;j++)for (j=0;j<SIZE;j++)
{ scanf(“%d”,&mat[i][j]);{ scanf(“%d”,&mat[i][j]);
}}
}}
printf(“Transpose of the matrixn”);printf(“Transpose of the matrixn”);
for(i=0;i<SIZE;i++)for(i=0;i<SIZE;i++)
{{ for (j=0;j<SIZE;j++)for (j=0;j<SIZE;j++)
{printf(“%d”,&mat[j][i]);{printf(“%d”,&mat[j][i]);
}}
printf(“n”);printf(“n”);
}}
}}
20. Addition of Two MatricesAddition of Two Matrices
#include<stdio.h>#include<stdio.h>
#defind SIZE 3#defind SIZE 3
main()main()
{{
int a[SIZE][SIZE], b[SIZE][SIZE];int a[SIZE][SIZE], b[SIZE][SIZE];
int i,j;int i,j;
printf(“Enter the elements of the matrix An”);printf(“Enter the elements of the matrix An”);
for(i=0;i<SIZE;i++)for(i=0;i<SIZE;i++)
{{
for (j=0;j<SIZE;j++)for (j=0;j<SIZE;j++)
scanf(“%d”,&a[i][j]);scanf(“%d”,&a[i][j]);
}}
printf(“Enter the elements of the matrix B”);printf(“Enter the elements of the matrix B”);
for(i=0;i<SIZE;i++)for(i=0;i<SIZE;i++)
{{ for (j=0;j<SIZE;j++)for (j=0;j<SIZE;j++)
scanf(“%d”,&b[i][j]);scanf(“%d”,&b[i][j]);
}}
22. Passing Arrays to FunctionsPassing Arrays to Functions
An entire array can be passed to aAn entire array can be passed to a
function as an argument.function as an argument.
To pass an array to a function, theTo pass an array to a function, the
array must appear by itself, withoutarray must appear by itself, without
brackets or subscripts, as an actualbrackets or subscripts, as an actual
argument within the function call.argument within the function call.
The size of the array is not specifiedThe size of the array is not specified
within the formal argumentwithin the formal argument
declaration.declaration.