Module 3 Arrays - Pointers
Module 3 Arrays - Pointers
Arrays:
• An array is a collection of similar set of elements.
• An array is a collection of same type of values stored in consecutive memory locations.
• The values in the array are referred by a name,( which is the name of the array ) and index, this
indicates the position of the value in the array.
• The different types of arrays are
1. Single –dimensional array.
2. Two-dimensional array
3. Multidimensional arrays.
Declaring one dimensional array: Declaration of array is similar as declaring ordinary variables in
program. An array is to be declare before using it. Array is declared by using the following syntax,
Syntax: Data_type array_name[size];
In the above line, datatype represents the data type of the array to be declared, array_name represents the
name of the array and size represents the number of values to be sorted in the array.
The elements in one dimensional array are stored starting at the index zero to one less than the size of the
array. The total amount of memory that can be allotted to the array can be calculated using the formula
Total memory=size*[sizeof[data-type)]
= 10*2 bytes=20 bytes.
Initializing one dimensional array: initializing array is similar to initializing ordinary variables.
The various types of array initialization are,
• One by one initialization
• Initializing in a single statement.
• Initializing using for loop and scanf ().
2. Initializing in a single statement: We can initialize all the elements of the array at the time of
declaration of the array. The syntax to initialize array is as follows,
Array_name [index] =value;
Array_name indicated the name of the array, and index represents the position of the array element in the
array and the value represents the value being assigned to the array.
Ex. int a[5]={10,20,30,40,50}; this specifies initializing all the array elements at the same time.
1
CPS Module 3 Arrays
Initialization
int a[5]={10,20,30}; only 3 elements are initialized. Even though the compiler reserves 5 spaces only 3 are
initialized the next two will automatically initialized to 0.
Limitations of arrays:
▪ The size of the array specified at the time of creation cannot be altered later. Or we cannot add
more elements to an array than specified in its size.
▪ It is difficult to insert an element between two existing elements.
▪ The amount of memory taken by an array depends on the size specified for the array and not on the
number of elements stored in the array.
In the above syntax data_type represents the data type of the array to be declared, array_name represents
the name of the array, size1 represents the number of rows and size2 represents the number of columns.
Here, the array a is a two dimensional array containing 4 rows and 5 columns
2
CPS Module 3 Arrays
OR
int a[2][3]={{2,3},
{4,5},
{6,7}};
This array has the size 3X3 with only 6 elements, the remaining locations will be filled with 0s
2 3 0
4 5 0
6 7 0
void main()
{
int i,j,m,n;
int a[4][4],b[4][4];
printf(“Enter the number of rows :\n”);
scanf(“%d”,&m);
printf(“Enter the number of columns :\n”);
scanf(“%d”,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
printf(“the transpose of the matrix is\n”);
for(i=0;i<m;i++)
{
3
CPS Module 3 Arrays
for(j=0;j<n;j++)
{
b[i][j]=a[j][i];
printf(“%d\t”,b[i][j]);
}
printf(“\n”);
}
}
}
Develop, implement and execute a C program that reads two matrices A (m x n) and B (p x q) and
Compute product of matrices A and B.
#include<stdio.h>
void main()
{
int A[5][5], B[5][5], C[5][5], m,n,p,q,i,j,k;
printf("Enter the order of matrix A");
scanf("%d%d",&m,&n);
printf("Enter the order of matrix B");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("\n Matrix multiplication is not possible");
getch(); exit(0);
}
printf("\n Matrix multiplication is possible\n");
printf("\n Enter %d elements of matrix A \n",m*n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&A[i][j]);
}
printf("\n Enter %d elements of matrix B\n", p*q);
for(i=0;i<q;i++)
{
for(j=0;j<p;j++)
scanf("%d",&B[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
C[i][j]+= A[i][k]*B[k][j];
}
}
printf("The Product Matrix C is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
4
CPS Module 3 Arrays
printf("%d\t",C[i][j]);
printf("\n");
}
Develop, implement and execute a C program that reads two matrices A (m x n) and B (p x q) and
Compute addition of matrices A and B.
#include<stdio.h>
void main()
{
int A[5][5], B[5][5], C[5][5], m,n,p,q,i,j,k;
printf("Enter the order of matrix A");
scanf("%d%d",&m,&n);
printf("Enter the order of matrix B");
scanf("%d%d",&p,&q);
5
CPS Module 3 Arrays
Multi-Dimensional Array
C allows arrays of 3 or more dimensions. The exact limit is determined by the compiler. The general form
of a multi-dimensional array is
data_type array_name [s1] [s2] [s3]…………….[sm]
where si is the size of the ith dimension.
Dynamic Arrays:
• An array created at compile time by specifying size in the source code has a fixed size and cannot
be modified at run-time.
• The process of a allocating memory at compile time is known as static memory allocation.
• In C it is possible to allocate memory to arrays at run time are called dynamic memory allocation
and the arrays created at run time are called dynamic arrays.
• Dynamic arrays are created using hat are known as pointer variables and memory management
functions malloc, calloc and realloc.
1. malloc( ):this function allocates specified amount of memory . The syntax is as shown
Ex.
int *p;
p=(int*)malloc(5*sizeof(int));
Here p is pointer variable holds the address of first memory location returned by malloc () to hold 5
integer numbers.
Ex.
p→ 2000
2002
2004
2006
2008
2. calloc( ): this function is same as malloc() ,it allocates a block of memory. but it initializes memory
locations with 0 and returns pointer to first memory location
syntax :datatype pointer_vatriable;pointer
variable=(casting*)calloc(n*sizeof(datatype));
• pointer_ variable is of any data type
• casting specifies the allocated memory is to cleared to store the specified type of data
6
CPS Module 3 Arrays
Ex.
int *p;
p=(int*)calloc(5,sizeof(int));
Here p is pointer variable holds the address of first memory location returned by calloc () to hold 5 integer
numbers.
p→ 0 2000
0 2002
0 2004
0 2006
0 2008
3. realloc( ) : this function reallocates the memory that is already allocated either by malloc or calloc.This
function helps to alter the memory size by extending or shrinking the allocated memory.
int *p;
p=(int*)malloc(5*sizeof(int));
p→ 2000
2002
2004
2006
2008
when the memory to be increased so another 6 numbers can be stored, then realloc is used as shown
below
p=(int*)realloc(11*sizeof(int));
4. free() : function is used to deallocate the allocated memory either by malloc or calloc or realloc
syntax: free(pointer_variable);
if the above example is considered then
free(p); releases the memory