0% found this document useful (0 votes)
21 views7 pages

Module 3 Arrays - Pointers

Uploaded by

preetha.cs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

Module 3 Arrays - Pointers

Uploaded by

preetha.cs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CPS Module 3 Arrays

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.

SINGLE DIMENSIONAL ARRAYS:


A single dimensional array is a linear list of related data items with a single subscript. In memory all the
data items are stored in contiguous memory locations one after the other.

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.

Ex: int a [10];


in this example, a is the name of the array contains 10 elements, which are indexed 0 to 9. Which contain
10 integer values

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 ().

1. One by one initialization:


We can initialize the elements of the array one by one.
Ex: Int a[5];
a [0]=10; a [1]=20; a [2]=30; a [3]=40; a [4]=50;

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.

Initialization using for loop and scanf():


The elements of the array can be read by input device(keyboard) by using for loop and scanf().
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);

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.

Program to find the largest or maximum element in the array.


void main()
{
int i,n,big=0,a[10];
printf(“enter the size of the array”);
scanf(“%d”,&n);
printf(“Enter the elements of the array :\n”);
for(i=0;i<n;i++)
Scanf(“%d”,&a[i]); /* reads the array elements*/
for(i=0;i<n;i++)
{
if(a [i]>big) /* checks each element of array with big element*/
big=a [i];
}
printf(“The biggest element in the array is=%d”,big);
}

TWO DIMENSIONAL ARRAYS:


Array with two subscript is called two-dimensional array. It is used to store a table of values of the same
data type. It has two subscripts rows and columns.

Declaring a Two dimensional array:


data_type array_name [size1][size2];

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.

Ex. int a[4][5];

Here, the array a is a two dimensional array containing 4 rows and 5 columns

2
CPS Module 3 Arrays

Initializing a Two–dimensional Array:

Initializing all specified memory locations:


The elements of a two dimensional array can be initialized similar to a one –dimensional array. The syntax
for this is as follows.
data_type array_name[size1][size2]={value1,value2,…………value n};

Ex. int a[2][3]={2,3,4,5,6,7}; //This is an array with 2 rows and 3 columns,

The above initialization is similar to ,


a[0][0]=2; a[0][1]=3; a[0][2]=4;
a[1][0]=5; a[1][1]=6; a[1][2]=7;

OR
int a[2][3]={{2,3},
{4,5},
{6,7}};

Initialization using for loop and scanf():


The elements of the array can be read by input device(keyboard) by using for loop and scanf().
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);

Partial array initialization


int a[3][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

Program to find the transpose of a given matrix

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);

printf("\n Enter %d elements of matrix A \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");
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;
C[i][j]= A[i][j]+B[i][j];
}
}
printf("The Addition of Matrix C is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",C[i][j]);
printf("\n");
}

Note: Perform subtraction and division same as above.

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.

Some examples are


int survey [3] [5] [12];
float table [5] [4] [5] [3];

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

Syntax: datatype pointer_vatriable;


Pointer variable=(casting*)malloc(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
• malloc() function allocates the memory
• sizeof()- Returns size in bytes
• n-specifies the number of elements to which memory to be allocated

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

• calloc() function allocates the memory


• sizeof()- Returns size in bytes
• n-specifies the number of elements to which memory to be allocated

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));

and allocated memory is as shown,


p→ 2000
2002
2004
2006
2008
2010
2012
2014
2016
2018
2020

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

You might also like