Module V:
Arrays,
String and
Structures
By: Dimple Bohra
Array Basics
❖ Need of an array
❖ What is an array ?
❖ Array Declaration
❖ Array elements in memory
❖ Accessing Array elements
❖ Array Initialization
❖ Entering Data into array
❖ Accessing data from array
❖ Programs
WHY DO WE NEED AN ARRAY ?
• WE CAN USE NORMAL VARIABLES (V1, V2, V3, ..) WHEN WE HAVE A SMALL NUMBER OF OBJECTS, BUT IF
WE WANT TO STORE A LARGE NUMBER OF INSTANCES, IT BECOMES DIFFICULT TO MANAGE THEM WITH
NORMAL VARIABLES.
• THE IDEA OF AN ARRAY IS TO REPRESENT MANY INSTANCES IN ONE VARIABLE.
WHAT IS AN ARRAY ?
An array is a collection of similar type of data
❖ Array is a variable that can store multiple values
❖ Derived data type
❖ Stores primitive type of data like int, float, char
❖ Data is stored in a contiguous memory locations
❖ An array of characters is called as a string
ARRAY DECLARATION
❖ Arrays are declared like a variable
❖ Array declaration tells compiler which type of array it is and what is the size of an array
Syntax:
data_type array_name[array_size];
Example:
int arr[10];
One Dimensional Array
ARRAY ELEMENTS IN MEMORY
Address of any element = base address+(element number-1*data type size)
ACCESSING ARRAY ELEMENTS
❖ Array elements are accessed using indices
❖ Array index always starts from 0
ARRAY INITIALIZATION
Initialization of an array at the time of declaration:
int arr[5] = {1,2,3,4,5};
int arr[ ] = {1,2,3,4,5};
Initialization of each element in an array using index:
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;
ENTERING DATA INTO ARRAY
Initialization of an array at the time of execution:
int marks[5]; // Array Declaration Iteration 1 Iteration 3 Iteration 5
i=0 i=2 i=4
for(i=0; i<5; i++) // Data entry into an array 0 1 2 3 4
{
11 21 33 40 5
scanf(“%d”, &marks[i]); // Enter data at 𝑖 𝑡ℎ index
200 204 208 212 216
}
Iteration 2 Iteration 4
i=1 i=3
ACCESSING DATA FROM ARRAY
int marks[5]; // Array Declaration Iteration 1 Iteration 3 Iteration 5
i=0 i=2 i=4
for(i=0; i<5; i++) // Data entry into an array 0 1 2 3 4
{
11 21 33 40 5
printf(“%d ”, marks[i]); // print data from 𝑖 𝑡ℎ index
200 204 208 212 216
}
Iteration 2 Iteration 4
Output: 11 21 33 40 5
i=1 i=3
EXAMPLE
To get n values from user store into array and print those values with their addresses
int main() printf("\n Printing values in array");
{ for(i=0; i<n; i++)
int a[20]; {
int i, n; printf("\n a[%d] = %d ", i, a[i]);
printf("\n How many elemtns you want to enter? "); printf("\address of a[%d] = %u", i, &a[i]);
scanf("%d", &n); }
printf("\n Enter values in an array"); return 0;
for(i=0; i<n; i++) }
{
printf("\n Enter value at a[%d] = ",i);
scanf("%d", &a[i]);
}
Arrays & Functions
❖ Passing array elements to a function
❖ Passing an entire array to a function
PASSING ARRAY ELEMENTS TO A FUNCTION
Passing arrays elements to the function can be done in two ways:
❖ Call by value (Passing element of an array to the function)
❖ Call by reference (Passing addresses of elements of an array to the function)
PASSING ARRAY ELEMENTS TO A FUNCTION
#include<stdio.h> \\ Function Definition
void display(int); // Function Declaration void display(int x)
int main() {
Call by value
{ printf("%d ",x);
(passing array element
int a[5] = {11,12,13,14,15}; }
to the function)
int i;
printf("\nDisplaying array elements\n");
for(i=0; i<5; i++)
display(a[i]); // function call by value
return 0;
}
PASSING ARRAY ELEMENTS TO A FUNCTION
#include<stdio.h> \\ Function Definition
void display(int *); // Function Declaration void display(int *x)
int main() {
Call by reference
{ printf("%d ",*x);
(passing addresses of
int a[5] = {11,12,13,14,15}; }
array elements to the
int i;
function)
printf("\nDisplaying array elements\n");
for(i=0; i<5; i++)
display(&a[i]); // function call by reference
return 0;
}
PASSING AN ENTIRE ARRAY TO A FUNCTION
#include<stdio.h> \\ Function Definition
Passing address of an void display(int x[5], int n)
void display(int [], int); // Function Declaration
{
array using array (Self int main()
int i;
referencing pointer) { for(i=0; i<n; i++)
And displaying array int a[5] = {11,12,13,14,15}; {
elements using array printf("\nDisplaying array elements\n"); printf("%d “, x[i]);
containing address of }
display(a, 5); // display(200,5);
}
passed array return 0;
}
0 1 2 3 4
11 12 13 14 15
200 204 208 212 216
PASSING AN ENTIRE ARRAY TO A FUNCTION
#include<stdio.h> \\ Function Definition
Passing address of an void display(int *x, int n)
void display(int *, int); // Function Declaration
{
array using pointers int main()
int i;
and displaying array { for(i=0; i<n; i++)
elements by int a[5] = {11,12,13,14,15}; {
performing pointer printf("\nDisplaying array elements\n"); printf("%d “, *x);
arithmetic x++;
display(a, 5); // display(200,5);
}
return 0;
}
450 400 }
200 0 1 2 3 4
200
11 12 13 14 15
a x 200 204 208 212 216
2 DIMENSIONAL ARRAYS
❖ 2-dimensional array
❖ 2-D array declaration
❖ 2-D array initialization
❖ 2-D array elements in memory
❖ Entering data into 2-D array
❖ Reading data from 2-D array
❖ Matrix Operations
❖ Matrix addition
❖ Matrix multiplication
❖ Matrix transpose
2- DIMENSIONAL ARRAY
❖ 2-dimensional array is called as a matrix with rows and columns
❖ Each element in 2-d array is represented with row and column number
❖ 2-dimensional array is a collection of several 1-dimensional arrays
0 1 2 3
0 11 33 45 23
1 70 15 55 66
2 9 63 12 50
2 – D ARRAY DECLARATION
Syntax:
data_type array_name[rows][columns]; 0 1 2 3
0 a[0][0] a[0][1] a[0][2] a[0][3]
Example:
int a[3][4]; 1 a[1][0] a[1][1] a[1][2] a[1][3]
2 a[2][0] a[2][1] a[2][2] a[2][3]
// a is a 2-dimensional array with 3 rows and 4 columns
2 – D ARRAY DECLARATION
Valid Declarations: 0 1 2 3
❖ int a[3][4];
0 a[0][0] a[0][1] a[0][2] a[0][3]
❖ int a[][4];
1 a[1][0] a[1][1] a[1][2] a[1][3]
Invalid Declarations:
❖ int a[][]; 2 a[2][0] a[2][1] a[2][2] a[2][3]
❖ int a[3][];
2 – D ARRAY INITIALIZATION
int a[3][4] = {11,12,13,14,21,22,23,24,31,32,33,34};
Or
0 1 2 3
int a[ ][4] = {{11,12,13,14}, {21,22,23,24}, {31,32,33,34}};
0 11 12 13 14
Or
1 21 22 23 24
int a[3][4] = { 2 31 32 33 34
{11,12,13,14},
{21,22,23,24},
{31,32,33,34}
};
2 – D ARRAY ELEMENTS IN MEMORY
0 1 2 3
int a[3][4] = {
{11,12,13,14}, 0 11 12 13 14
{21,22,23,24}, 1 21 22 23 24
{31,32,33,34}
2 31 32 33 34
};
a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]
11 12 13 14 21 22 23 24 31 32 33 34
100 104 108 112 116 120 124 128 132 136 140 144
ENTERING DATA INTO 2-D ARRAY
int a[3][4]; // Array Declaration
a[3][4]
// Data entry into an array
for(i=0; i<3; i++) // i represents row index 0 1 2 3
{ 0 11 12 13 14
for(j=0; j<4; j++) // j represents column index 1 21 22 23 24
{
2 31 32 33 34
scanf(“%d”, &a[i][j]); // Enter data at 𝑖 𝑡ℎ row and 𝑗𝑡ℎ column
}
}
READING DATA FROM 2-D ARRAY
int a[3][4]; // Array Declaration
a[3][4]
// Reading data from an array
for(i=0; i<3; i++) // i represents row index 0 1 2 3
{ 0 11 12 13 14
for(j=0; j<4; j++) // j represents column index 1 21 22 23 24
{
2 31 32 33 34
printf(“%d ”, a[i][j]); // print data present at 𝑖 𝑡ℎ row and 𝑗𝑡ℎ column
}
}
MATRIX OPERATIONS
Different Matrix operations:
❖ Matrix Addition
❖ Matrix Subtraction
❖ Matrix Multiplication
❖ Matrix Transpose
MATRIX ADDITION
❖ A matrix can only be added to (or subtracted from) another matrix if the two matrices have the
same dimensions
❖ To add two matrices, just add the corresponding entries, and place this sum in the corresponding position
in the matrix which results
a[2][3] b[2][3] c[2][3]
0 1 2 0 1 2 0 1 2
0 4 5 6 0 1 2 3 0 5 7 9
1 7 8 9 1 4 5 6 1 11 13 15
MATRIX MULTIPLICATION
❖ You can only multiply two matrices if their dimensions are compatible , which means the number of
columns in the first matrix is the same as the number of rows in the second matrix
❖ In matrix multiplication first matrix one row element is multiplied by second matrix all column
elements
❖ Add the products
MATRIX MULTIPLICATION
a[2][2] b[2][3] c[2][3]
0 1 0 1 2 0 1 2
0 4 5 0 1 2 3 0 24 33 42
1 1 2 1 4 5 6 1 9 12 15
1*3
1*2 + 2*6
4*3
1*1
4*1
4*2 2*5 = 15
5*6
2*4
5*4
5*5 42
912
24
33
MATRIX TRANSPOSE
❖ Transpose of a matrix is obtained by changing rows to columns and columns to rows
❖ Transpose of A[i][j] is the matrix A[j][i]
a[2][3] a[3][2]
0 1 2 0 1
0 1 2 3 0 1 4
1 4 5 6 1 2 5
2 3 6
STRINGS
❖ What is string ?
❖ Sting Declaration
❖ String Initialization
❖ Accepting String from user
❖ String functions
WHAT IS STRING ?
String is a collection of characters terminated by ‘\0’ (Null character)
❖ String is a one dimensional array of characters terminated by null character ‘\0’
❖ Each element in character array takes one byte of memory
Null character ‘\0’:
❖ Used to indicate termination of string
❖ ASCII value of ‘\0’ is 0
STRING DECLARATION
❖ String can be declared as an array of characters
Syntax:
char string_name[size];
Example:
char s[5];
STRING INITIALIZATION
Two ways:
1. By Character array
2. By String literal
1. By character array:
‘\0’ to be added explicitly
char str[6] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};
2. By string literal:
‘\0’ is added by the compiler implicitly
char str[6] = “Hello”;
STRING INITIALIZATION
Note: (Not acceptable assignment to string variable)
char str[20]; // string declaration
str = “helloworld”; // assigning value to string variable
// error: assignment to expression with array type
ACCEPTING STRING FROM USER
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
char name[20]; char name[20];
printf("Enter name: "); printf("Enter name: ");
scanf("%s", &name); scanf("%[^\n]s", name);
printf("Your name is %s", name); printf("Your name is %s", name);
return 0; return 0;
} }
Note:
scanf() function reads the sequence of characters until it encounters whitespace
STRING FUNCTIONS
String library functions:
❖ String Library functions are
available in string.h header file
❖ To use string library functions
in a program you need to
include string.h header file
#include<string.h>
STRING FUNCTIONS
strlen() #include <stdio.h>
#include<string.h>
Counts the number of characters present int main()
in the string {
char s[20] = “college”;
Syntax: int len;
strlen(str); len = strlen(s);
printf(“Length of string s = %d”, len);
return 0;
}
STRING FUNCTIONS
#include <stdio.h>
Find length of a string without int main()
using strlen() function {
char s[20] = “college”;
int i = 0;
while(s[i] != ‘\0’)
i++;
printf(“Length of string s = %d”, i);
return 0;
}
STRING FUNCTIONS
#include <stdio.h>
strcpy()
#include<string.h>
int main()
Copies the content of one string into {
another char s[20] = “college”;
char d[20];
Syntax: strcpy(d, s);
strcpy(destination, source); printf(“Source string s = %s”, s);
printf(“Destination string d = %s”, d);
return 0;
}
STRING FUNCTIONS
#include <stdio.h>
int main()
Copy the content of one string {
into another string without char s[20] = “college”;
using strcpy() function char d[20];
int i = 0;
while(s[i] != ‘\0’)
{
d[i] = s[i];
i++;
}
printf(“Source string s = %s”, s);
printf(“Destination string d = %s”, d);
return 0;
}
STRING FUNCTIONS
#include <stdio.h>
strcmp()
#include<string.h>
int main()
Compares two strings to check whether they
{
are same or different
char s[20] = “college”;
❖ If the two strings are equal it returns 0
if(strcmp(s, “college”) == 0)
❖ Else it returns ASCII value difference
printf(“Strings are equal);
else
Syntax:
printf(“Strings are different”);
strcmp(string1, string2);
return 0;
}
STRING FUNCTIONS
int main()
{ Compare two strings without
char s[20] = “college”; using strcmp() function
char d[20] = “djsce”;
int cmp,i = 0;
while(s[i] != ‘\0’ || d[i] != ‘\0’)
{
if(d[i] == s[i]) if(cmp == 0)
{ printf(“Strings are equal);
cmp = 0;
i++; else
} printf(“Strings are different”);
else
{ return 0;
cmp = 1; }
break;
}
}
STRING FUNCTIONS
#include <stdio.h>
strcat() #include<string.h>
int main()
Concatenates two strings
{
i.e. concatenate source string at the end of
char s[20] = “college”;
destination string
char d[20]="djsce";
strcat(d, s);
Syntax:
printf(“Concatenated string d = %s”, d);
strcat(destination, source);
return 0;
}
STRING FUNCTIONS
int main()
{
Concatenate two strings char s[20] = “college”;
without using strcat() function char d[20] = “djsce”;
int i = 0, j = 0;
while(d[i] != ‘\0’)
i++;
while(s[j] != ‘\0’)
{
d[i] = s[j];
i++;
j++;
}
printf(“Concatenated string d = %s”, d);
return 0;
}
PROGRAMS
PROGRAM TO PRINT TRANSPOSE OF MATRIX
PROGRAM FOR ADDITION OF TWO MATRICES
PROGRAM TO DELETE AN ELEMENT FROM AN ARRAY FROM DESIRED LOCATION
PROGRAM FOR SORTING AN ARRAY
PROGRAM TO GET MINIMUM AND MAXIMUM ELEMENT FROM AN ARRAY
PROGRAM TO MULTIPLY TWO MATRICES
PROGRAMS ON STRING
PRINT CHARACTERS OF A STRING
PROGRAM TO COUNT VOWELS AND CONSONANTS IN A STRING
PROGRAM TO CHECK PALINDROME OR NOT
PROGRAM TO COUNT LINES, WORDS, CHARACTERS IN A TEXT