0% found this document useful (0 votes)
2 views6 pages

PSP Module4

The document provides a comprehensive overview of one-dimensional and two-dimensional arrays in programming, including their definitions, initialization, and access methods. It explains the structure of arrays, their advantages, and includes example code for inputting and displaying array elements. Additionally, it covers character arrays, also known as strings, and their initialization with a focus on the importance of the NULL character for string termination.

Uploaded by

Nandana Ullas
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)
2 views6 pages

PSP Module4

The document provides a comprehensive overview of one-dimensional and two-dimensional arrays in programming, including their definitions, initialization, and access methods. It explains the structure of arrays, their advantages, and includes example code for inputting and displaying array elements. Additionally, it covers character arrays, also known as strings, and their initialization with a focus on the importance of the NULL character for string termination.

Uploaded by

Nandana Ullas
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

PSP MODULE 4

Defining, initializing and accessing of one-dimensional arrays – Programs using one dimensional array.

Defining, initializing and accessing of two dimensional arrays – Programs using two dimensional arrays.
ARRAY
Array is the collection of similar data types or collection of similar entity stored in contiguous memory
location. Array of character is a string. Each data item of an array is called an element. And each element is
unique and located in separated memory location. Each of elements of an array share a variable but each
element having different index no. known as subscript.

An array can be a single dimensional or multi-dimensional and number of subscripts determines its
dimension. And number of subscripts is always starts with zero. One dimensional array is known as vector
and two-dimensional arrays are known as matrix.

ADVANTAGES: array variable can store more than one value at a time where other variable
can store one value at a time.

Example:

int arr[100];

int mark[100];

DECLARATION OF AN ARRAY :
Its syntax is :

data type array name [size];


Example:
int arr[100];
int mark[100];
int a[5]={10,20,30,100,5}

The declaration of an array tells the compiler that, the data type, name of the array, size of the array
and for each element it occupies memory space. Like for int data type, it occupies 2 bytes for each
element and for float it occupies 4 byte for each element etc. The size of the array operates the number
of elements that can be stored in an array and it may be a int constant or constant int expression.
We can represent individual array as :

int ar[5];

Symbolic constant can also be used to specify the size of the array as:

#define SIZE 10
INITIALIZATION OF AN ARRAY:
After declaration element of local array has garbage value. If it is global or static array then it will be
automatically initialized with zero. An explicitly it can be initialized that

Data type array name [size] = {value1, value2, value3…}

Example:

int ar[5]={20,60,90, 100,120}

Array subscript always start from zero which is known as lower bound and upper value is known as upper bound
and the last subscript value is one less than the size of array. Subscript can be an expression i.e. integer value.
It can be any integer, integer constant, integer variable, integer expression or return value from functional call
that yield integer value.

The array elements are standing in continuous memory locations and the amount of storage required
for hold the element depend in its size & type.

Total size in byte for 1D array is:

Total bytes=size of (data type) * size of array.

Example : if an array declared is: int [20];

Total byte= 2 * 20 =40 byte. Since size of int is 2 bytes

ACCESSING OF ARRAY ELEMENT:


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

Enter a value for arr[0] = 12


Enter a value for arr[1] =45
Enter a value for arr[2] =59
Enter a value for arr[3] =98
Enter a value for arr[4] =21
The array elements are 12 45 59 98 21

From the above example value stored in an array are and occupy its memory addresses 2000, 2002, 2004, 2006,
2008,2010 respectively.

arr[0]=12, arr[1]=45, arr[2]=59, arr[3]=98, arr[4]=21

2000 2002 2004 2008 2010


12 45 59 98 21

Two dimensional arrays


Two dimensional array is known as matrix. The array declaration in both the array [Link] single
dimensional array single subscript is used and in two dimensional array two subscripts are is used.

Its syntax is

Data-type array name[row][column];


Total no. of elements in 2-D array is calculated as row*column

Example:-

int a[2][3];

Total no of elements=row*column is 2*3 =6

It means the matrix consist of 2 rows and 3 columns For

example:-

20 2 7
8 3 15

Positions of 2-D array elements in an array are as below

00 01 02

10 11 12
a [0][0] a [0][1] a [0][2] a [1][0] a [1][1] a [1][2]

20 2 7 8 3 15
2000 2002 2004 2006 2008 2010

Accessing 2-d array /processing 2-d arrays


For processing 2-d array, we use two nested for loops. The outer for loop corresponds to the row and the inner
for loop corresponds to the column.

For example

int a[4][5];

for reading value:-


for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
scanf(“%d”,&a[i][j]);
}
}

For displaying value:-

for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{

printf(“%d”,a[i][j]);
}
}
Initialization of 2-d array:
2D array can be initialized in a way similar to that of 1-D array.

for example:- int Mat[4][3]={11,12,13,14,15,16,17,18,19,20,21,22};

These values are assigned to the elements row wise, so the values of elements after this initialization are

Mat[0][0]=11, Mat[1][0]=14, Mat[2][0]=17 Mat[3][0]=20

Mat[0][1]=12, Mat[1][1]=15, Mat[2][1]=18 Mat[3][1]=21

Mat[0][2]=13, Mat[1][2]=16, Mat[2][2]=19 Mat[3][2]=22


While initializing we can group the elements row wise using inner braces. for example:-
int mat[4][3]={{11,12,13},{14,15,16},{17,18,19},{20,21,22}};

And while initializing , it is necessary to mention the 2nd dimension where 1st dimension is optional.

int mat[][3];

int mat[2][3];

Example: m X n Matrix Addition

#include < stdio.h >

int main()

int m, n, c, d, first[10][10], second[10][10], sum[10][10];

printf("Enter the number of rows and columns of matrix\n");

scanf("%d%d", & m, & n);

printf("Enter the elements of first matrix\n");

for (c = 0; c < m; c++)

for (d = 0; d < n; d++) scanf("%d", & first[c][d]);

printf("Enter the elements of second matrix\n");

for (c = 0; c < m; c++)

for (d = 0; d < n; d++) scanf("%d", & second[c][d]);

printf("Sum of entered matrices:-\n");

for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

sum[c][d] = first[c][d] + second[c][d];

printf("%d\t", sum[c][d]);
}
printf("\n");
}
return 0;

}
Character Array: String
Array of character is called a string. It is always terminated by the NULL character. String is a one
dimensional array of character.

We can initialize the string as char name[]={‘j’,’o’,’h’,’n’,’\o’};


Here each character occupies 1 byte of memory and last character is always NULL character. Where
’\o’ and 0 (zero) are not same, where ASCII value of ‘\o’ is 0 and ASCII value of 0 is 48. Array
elements of character array are also stored in contiguous memory allocation.
From the above we can represent as;
J o h N ‘\o’
The terminating NULL is important because it is only the way that the function that work with string can
know, where string end.

String can also be initialized as;

char name[]=”John”;

Here the NULL character is not necessary and the compiler will assume it automatically.

You might also like