1
Array and Strings
Array
an array is a collection of similar elements.
These similar elements could be percentage marks of 100
students, or salaries of 300 employees, or ages of 50
employees.
What is important is that the elements must be ‘similar’.
We cannot have an array of 10 numbers, of which 5 are ints
and 5 are floats. Usually, the array of characters is called a
‘string’, whereas an array of ints or floats is called simply an
array.
Arrays 2
1D Arrays 2D Arrays
Declaration of 1D Arrays Elements of 2D Arrays
Global Declaration Initialization of 2D Arrays
Local Declaration Passing Array Element to a
Initialization of Arrays Function
Compile Time Initialization Passing Array to a
Function
Run Time Initialization
Passing 2D Array to a
Accessing the Array Function
Elements
Returning Array Element
Assigning values to from a Function
array elements,
Returning Array from a
Displaying the array Function
elements
One Dimensional Array 3
Declaration of One-Dimensional Arrays
[storage_class] data_type variable_name[size];
Global Declaration
char string[50]; /* Global Declaration */
void main(){
…………………
}
Local Declaration
void main(){
int age[] ={21,22,19,20}; /* Local Declaration & Initialization */
…………………
}
Initialization of Arrays 4
1. Compile Time Initialization
int number[3]={0,0,0};
2. Run Time Initialization
int x[3];
scanf(“%d%d%d”,&x[0],&x[1],&x[2]);
Accessing the Array 5
Elements
i. Assigning values to array elements,
int a[5], b[3];
a=0; /* wrong*/
b=a; /* wrong*/
if(a<b)
Another Example
{…} /* wrong*/
float marks[3];
marks[0] =90.05;
marks[1] =88.77;
marks[2] =78.12;
marks[3] =90.77; /* Invalid */
Compile Time 6
Initialization char name[] =“Ram”;
/*
Same as
char name[] ={‘R’,’a’,’m’,’\0’};
*/
Program to
Run Time Initialization count 7
negative,
positive &
zeros from
entered
numbers
2D Array 8
[storage_class] data_type array_name[row_size][column_size];
Declaration of 2D Array
int m[3][3];
Initialization of 2D Array a[0][0] a[0][1] a[0][2]
int a[3][3]={{5,6,7},{4,3,8},{1,5,9}}; 5 6 7
is equivalent to
a[1][0] a[1][1] a[1][2]
a[0][0]=5; a[0][1]=6; a[0][2]=7; 4 3 8
a[1][0]=4; a[1][1]=3; a[1][2]=8;
a[2][0] a[2][1] a[2][2]
a[2][0]=1; a[2][1]=5; a[2][2]=9;
1 5 9
Accessing 2D Array 9
Element
int b[4][4] ={
{5,6,7,8},
{1,2,3,4}, 5 6 7 8
{8,5,2,0}, 1 2 3 4
{7,5,2,0} 8 5 2 0
}; 7 5 3 4
b[2][1] =?
b[0][3] =?
b[3][3] =?
b[1][1] =?
WAP to read m*n 10
matrices and
display their sum.
Task 11
WAP to read m*n matrices and
display their product.
Sort Array Element 12
Passing Array Element to a 13
Function
Passing Array to a 14
Function
Task 15
WAP to add n numbers
entered by user. Use the
concept of array & function.
Passing 2-D Array to a 16
Function
Returning Array Element 17
from a Function
Returning Array from a Fxn 18
Multidimensional Arrays 19
General Syntax:
[storage_class] data_type
array_name [s1][s2][s3]…[sn];
Task 20
WAP to read 3*3 matrix & display it
to screen.
Give an example program to add
two matrices & store the results in
the 3rd matrix.
Strings 21
Declaration and Initializing String Variable
Reading Strings from Terminal
Reading a Line of Text
String Handling Functions
Passing String to a Function
Declaration and Initializing String Variable
Syntax: char string_name[size];
K H W O P A \0 \0 \0 \0
char college[10] =“KHWOPA”;
char college[4] =“KHWOPA”; /* Illegal */
Reading a Line of Text 22
Using scanf() Function
char address[20];
scanf(“%s”, adress);
Reading a Line of Text 23
ASCII Value & Character 24
String Handling Functions 25
i. strlen() – return length of a string
ii. strcpy() – strcpy(destination_string, source_string)
iii. strcat() – concatenates two strings
iv. strcmp() – compares two strings
i. Returns 0 if both are same
ii. Returns less than 0 if 1st string is less than 2nd
string
iii. Returns greater than 0 if 1st string is greater than
2nd string
v. strrev() – reverse given string
String Concatenation 26
String Concatenation without using string.h 27
Illustrations of String-Handling Functions
28
Passing String to a 29
Function
Passing Multiple Strings to 30
a Function