C_Tutorial_Day_9
C_Tutorial_Day_9
Arrays and
and Strings
Objective:
One dimensional arrays: Array manipulation; Searching, Insertion, Deletion of an element from
an array; Finding the largest / smallest element in an array; Two dimensional arrays, Addition /
Multiplication of two matrices, Transpose of a square matrix; Null terminated strings as array of
What Is an Array?
An array is a collection of data storage locations which is in sequence, each having the same data type and
the same name. Each storage location in an array is called an array element. For example sequence of 5 in-
tegers stored one after another in memory represents an array. String can be considered as an array of cha-
racters, the last of which is the null character. (ASCII value of null is zero)
Single-Dimensional Arrays
A single-dimensional array has only a single subscript. A subscript is a number in brackets that follows an ar-
ray name. This number can identify the number of individual elements in the array. An example should make
this clear.
For the calendar program, We have to create a group of locations that can store 12 values of months days.
We could use the following line to declare an array of type int:
int months[12];
The array is named months, and it contains 12 elements. Each of the 12 elements is the exact equivalent of
a single int variable. All of C's data types can be used for creating arrays. C array elements are always num-
bered starting at 0, so the 12 elements of months are numbered 0 through 11.
In the above example, January's days would be stored in months[0], February's days in months[1], and so
on.
When an array is declared, the compiler sets aside a block of memory large enough to hold the entire array.
Individual array elements are stored in sequential memory locations.
#define MONTH 12
int months[MONTH];
is equivalent to this statement:
int months[12];
Initialization of an array
Array elements can be initialized at the time of declaration. For example
int months[12]= { 31,28,31,30,31,30,31,31,30,31,30,31};
Problem: WAP to find out the day of any date from year 1753 to 9999.
Begin
Display ”Enter date in dd-mm-yyyy format : ”
Accept dd, mm, yy
months[12] { 31,28,31,30,31,30,31,31,30,31,30,31}
If (yy is divisible by 4 and yy is not divisible by 100) or (yy is divisible by 400 ) Then
months[1] 29
Endif
leap 0
yr 1753
diff yy – yr
i yr
Repeat until i < yy
If (i is divisible by 4 and i is not divisible by 100) or (i is divisible by 400) Then
leap leap +1
end if
i i+1
end of repeat
diff remainder of (diff + leap) / 7
diff diff + dd –1
mn 0
repeat until mn < mm -1
diff diff + months[mn]
mn mn+1
end of repeat
In the above example we can create an array of integer that can hold the marks
students. As follows:
#include <stdio.h>
void main()
{
int marks[5], i ;
int great=0;
great=0;
for(i=1 ; i<5 ; i++ )
{
if (great < marks[i])
great=marks[i];
}
printf(“\n\tHighest marks is : %d“, great);
}
Warning:
C does not check validity of the script when we use arrays. Use range as in array if
defined
int marks[20];
This declaration will store 20 elements. Index will be from 0 to 19.
Multidimensional Arrays
To manipulate two dimensional data structures such as matrices and tables, then we have use an array
which have two subscripts. One subscript will denote rows and the other, the columns.
How to declare
The declaration of two dimensional array is as follows:
float fees [ 4 ] [12];
Here fees is declared as a matrix having 4 rows and 12 columns. (It is Numbered 0 through 47). The first
element in the matrix is :
fees [0] [0]
and the last element of matrix is :
fees [3] [11]
The above declaration will occupy 192 bytes of memory for fees to store 48 float type values. The first 48
bytes stores the 1st rows values, 2nd 48 bytes stores the 2nd row values, 3rd 48 byte will stores 3rd rows values
and 4th 48 bytes stores 4th rows values.
The above fees can store 12 installments for 4 years of a student. If we have to store fees for 50 students.
Then it will be declared as:
float fees[50] [4] [12];
Searching an array
A common array operation is to search the values of an array for a particular
value. You may be doing this in order to find and change a value or to find and
process information in a corresponding element of a paired or parallel array.
A linear search of an array
A linear search starts searching for the value with the first element of the array
and continues through the array, one element at a time until the wanted value is
found or you reach the end of the array
Linear Search
LSEARCH( array[], max_num, item)
Begin
Index 0
found 0
repeat until (index <= max_num)
If array(index)=item Then
found 1
exit from repeat
end if
index=index +1
end of repeat
If found=1 Then
Display array (index)
Else
Display 'Item not found'
End if
End.
A binary search of an array
Created and Verified by B.K. Deepak
Date: 20/12/2004
Programming in C: Arrays and Strings
A binary search requires that the element values in the array be in order, such
as ascending order, from smallest to largest. Now, you can search the array by:
This is done until you find the value you want or you run out of array elements
BSEARCH (array[ ], num_max, item)
Begin
found 0
min 0
max num_max
mid (min + max ) / 2
repeat until ( array[mid] <> item) AND (min <= max)
If item < array[mid] Then
max mid-1
Else
min mid+1
Endif
mid ( min + max ) / 2
End of repeat
If array[mid] = item Then
Display “Item found”
Else
Display “Item not found”
Endif
End.
INSERT(array[],int num)
Begin
index 0
Repeat until (index < num)
Read the input value and store in array [index]
index = index+1
end of repeat
end.
This time, we are looking for the largest value in the array. We just set largest to
the value of the first element of the array. Then we compare this value to each
of the other elements in the array. If one is larger, we replace the value in larg-
est with the value and continue to check the rest of the array. The following the
flow to find the largest of the elements of an array.
-----------;
-----------;
great array[0]
For count = 1 to num_elements step 1
If array[count] > great Then
great array[count]
endif
Next count
Display “Largest Number is “, great
-------;
-------;
Now, we want to find the smallest value in the array. We set smallest to the val-
ue of the first element of the array and start comparing it to other elements in
the array. The following is the flow to find the smallest of the elements of an ar-
ray:
-----------;
-----------;
smallest array[0]
For count = 1 to num_elements step 1
If array[count] < smallest Then
smallest array[count]
endif
Next count
Display “Smallest Number is “, smallest
-------;
-------;
/* Transpose of matrices */
#include <stdio.h>
void main()
{
static int mat_a[10][10], mat_b[10][10];
int I, j, rows, cols;
printf(“\n\tEnter rows and columns of matrix : “);
scanf(“%d %d”,&rows, &cols);
printf(“\n\tEnter element of matrix A row wise : “);
for( i=0; i<rows; i++)
for( j=0; j<cols; j++)
scanf(“%d” , &mat_a[i][j]);
printf(“\n\tEnter element of matrix B row wise : “);
for( i=0; i<cols; i++)
for( j=0; j<rows; j++)
mat_b[i][j]=mat_a[j][i];
for( i=0; i<cols; i++)
{
for( j=0; j<rows; j++)
printf(“ %3d “,mat_b[i][j];
printf(“\n “);
}
}
/*Transpose of Matrix */