Unit II Arrays and Strings
Unit II Arrays and Strings
INTRODUCTION
In this computerized world, Data structures and Algorithms are the nuts-and-bolts used
by programmers to store and manipulate the data efficiently in computer memory. To develop
the software for any real world problem, having an in-depth understanding on every component
of software engineering is not mandatory. However it is important to understand that, the subject
of data structures and algorithms which are majorly concerned in coding phase of the software.
Different types of data structures can be applied to an application but, only some of them
provide the efficient results. No single data structure works well for all purpose, and so it is
important to know the strengths and limitations of several of them. This chapter will get you
started in thinking about the necessity of data structures, classification of data structures and
many of the fundamental in C language for developing the complex data structures.
BASIC TERMINOLOGIES
Data
Data is a basic fact of entity that is utilized in calculation or manipulation. It may be a
single or set of values that plays a vital role in the computational procedures. In general, Data are
classified as Numerical and Alphanumerical data.
Example: Name of the person (Alphanumerical), Roll number of the student
(Numerical).
Structuring of Data
Whether data is a single or group of values, it must be organized in a particular fashion
for computer processing (Storing, retrieving and manipulating). This organization leads to
structuring of data.
Example1: Consider the data {23, 4, 8, 35, 11, 90}. These are belonging to numerical
type, so it’s organized in a single group such as “Integer Array”. Due to this type of
organization, we can perform operation like sorting, adding new values, delete a value and
searching.
Example2: Consider the data {“Arun”, 65, “Employee”, 15000} which indicate person
information such as name, age, designation, salary. These are belonging to Numerical as well as
Alphanumerical data. So it’s best to organize like a multiple group such as two “2-Dimensional
Character Array” for storing name and designation respectively, and two “Integer Array” for
storing age and salary. But accessing a person information is become complex using these
multiple array. Instead of that, we can organize these data using single group which has an
ability to handle multiple data type such as “Structure”. So structuring of data plays important
role in problem solving.
ALGORITHM
To solve a well-defined computational problem, an algorithm is a step-by-step finite
sequence of definite instructions while terminates with the production of correct output from the
given input. It is generally written in Pseudo-English like statements. Algorithm is independent
of programming language. It can be easily converted into any programming language, based on
its syntactic and semantic constructs. It should be concise and free of ambiguity.
PROGRAM
Computer solution to a problem is a set of explicit and unambiguous instruction
expressed in a programming language. This set of instruction is called Program. In other words,
a program is the expression of an algorithm in a programming language.
What do you meant by Efficient Program?
Efficient program is determined by its time and space complexity. Time complexity
states the time needed to execute (compile as well as run time) a given program. Space
complexity states the memory space needed to store and process the program.
Example: Addition of two given numbers.
Program-1 Program-2
#include<stdio.h> #include<stdio.h>
void main() void main()
UNIT II - ARRAYS AND STRINGS
{ {
int a,b; int a,b,c,d;
printf(“Enter 2 nos for addition\n”); printf(“Enter 2 nos for addition\n”);
scanf(“%d %d”, &a,&b); scanf(“%d %d”, &a,&b);
printf(“Sum=%d\n”,a+b); c=a;
} d=b;
e=c+d;
printf(“Sum=%d\n”,e);
}
From the above example, Program-1 & Program-2 produces same output. But the time
complexity of Program-2 is greater than Program-1. Even though Program-2 gives same output,
number of lines of code (LOC) is high. Let we consider, each instruction needs 1 second for its
execution then execution time of Program-1 is 4 whereas Program-2 is 7. So Program-1 is
efficient than the Program-2.
We can say that, the efficiency of a given program is based on its time complexity. The
time complexity is not only depends on LOC, but also based on some properties that will be
discussed later in this chapter. Time complexity is represented or denoted by the Asymptotic
Notation which is universally accepted format. For instance, time complexity of “Insertion sort”
algorithm is denoted by O(N2). Pronounced like Big-O of N2.
Arrays are widely used data type in ‘C’ language. It is a collection of elements of similar data
type. These similar elements could be of all integers, all floats or all characters. An array of
character is called as string whereas and array of integer or float is simply called as an array. So
array may be defined as a group of elements that share a common name and that are defined by
position or index. The elements of arrays are store in sequential order in memory.
There are mainly two types of Arrays are used:
• One dimensional Array
• Multidimensional Array
UNIT II - ARRAYS AND STRINGS
One-Dimensional Array
Syntax Storagetype Datatype Arrayname[Size];
Where,
Storagetype => Define the scope of variable (i.e.) any one of the keyword such as
Auto(optional) / Register / Static / Extern.
Datatype => Type of value stored in array. E.g. int, char.
Arrayname => Name of the array represented by the user.
Size => Maximum no.of element stored in array.
Example int X[5];
Address of array and the address of first element are same.
The system need not keep track of the address of every element of A, but needs to keep
address of first element only, denoted by Base(A) and called as Base Address.
We can obtain the address of variable using the Address operator (&). For instance, ‘&A’
display the Base address of variable A.
The computer calculates the address of any element of A by the following formula.
Example, consider the statement int X[5]; The Memory allocation will be,
420 422 424 426 428
Here Base address is “420” and Bytes per element is 2. Suppose the system need to
calculate the address of 3rd element,
Address of 3rd element= 420 + (3 * 2)
= 426
Initialization of One-Dimensional Array
Example
1. int mark[7]={100,7,45,75,88};
Here, only 5 elements are initialized to the array “mark”. In default, the compiler will
assign zero to the uninitialized elements of an integer array and Garbage values to the
character array.
Program 1: Write a ‘C’ program to find the biggest number among the given ten numbers using
array.
#include<stdio.h> Output
UNIT II - ARRAYS AND STRINGS
In C, arrays are zero-based: the ten elements of a 10-element array are numbered from 0 to 9.
The subscript which specifies a single element of an array is simply an integer expression in
square brackets.
The first element of the array is a[0], the second element is a[1], etc. You can use these ``array
subscript expressions'' anywhere you can use the name of a simple variable, for example:
a[0] = 10;
a[1] = 20;
a[2] = a[0] + a[1];
Notice that the subscripted array references (i.e. expressions such as a[0] and a[1]) can appear on
either side of the assignment operator. it is possible to initialize some or all elements of an array
when the array is defined. The syntax looks like this:
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
The list of values, enclosed in braces {}, separated by commas, provides the initial values for
successive elements of the array.
UNIT II - ARRAYS AND STRINGS
The subscript does not have to be a constant like 0 or 1; it can be any integral expression. For
example, it's common to loop over all elements of an array:
int i;
Arrays are a real convenience for many problems, but there is not a lot that C will do with them
for you automatically. In particular, you can neither set all elements of an array at once nor
assign one array to another; both of the assignments
a = 0; /* WRONG */
and
int b[10];
b = a; /* WRONG */
are illegal.
To set all of the elements of an array to some value, you must do so one by one, as in the loop
example above. To copy the contents of one array to another, you must again do so one by one:
int b[10];
sum=sum+a[i];
}
x=(float)sum/(float)n;
printf("Mean\t= %f",x);
for(i=0;i<n;i++) {
for(j=i+1;j<n;j++) {
if(a[i]>a[j]) {
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
if(n%2==0)
y=(float)(a[n/2]+a[(n-1)/2])/2;
else
y=a[(n-1)/2];
printf("\nMedian\t= %f",y);
for(i=0;i<n-1;i++) {
mode=0;
for(j=i+1;j<n;j++) {
if(a[i]==a[j]) {
mode++;
}
}
if((mode>max)&&(mode!=0)) {
k=0;
max=mode;
b[k]=a[i];
k++;
}
else if(mode==max) {
b[k]=a[i];
k++;
}
}
for(i=0;i<n;i++) {
if(a[i]==b[i])
c++;
}
if(c==n)
printf("\nThere is no mode");
else {
printf("\nMode\t= ");
for(i=0;i<k;i++)
printf("%d ",b[i]);
}
}
UNIT II - ARRAYS AND STRINGS
Two-dimensional array
It is also referred as matrix. The total number of elements in array can be calculated by
the multiplication of row-size and column-size. For instance, row size is 3 and column
size is 2 then total element is 3x2 = 6.
It is not compulsory that the row size and column size should be equal. All the elements
of the first row will be assigned and then second row and so on.
While initializing an array, the row size is optional.
An array of string can be represented as a two dimensional array of characters.
Syntax StorageType Datatype Arrayname[Row_size] [Column_size]
Example
1. int rank[ ][2]={1,2,3,4,5,6};
2. char nam[4][8] = { {“madhu”}, {“revathy”}, {“anand”}, {“vijaya”} }
Transpose
The transpose of a matrix is a new matrix whose rows are the columns of
the original. (This makes the columns of the new matrix the rows of the
original). Here is a matrix and its transpose:
#include<stdio.h>
#include<conio.h>
int main()
{
int m,n,a[20][20],b[20][20],i,j,sum[20][20],sub[20][20],opt,tr[20][20],opt1,ch,e,f;
printf("Note : For Addition or Subtraction , no. of rows and columns should be same and for
transpose of matrices , your first matrices entered should be the desired matrices .\n");
printf("Enter the no. of rows: ");
scanf("%d",&m);
printf("Enter the no. of columns: ");
scanf("%d",&n);
printf("Enter the Data Elements of first matrices\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
} printf("Enter the no. of rows for second matrices: ");
scanf("%d",&e);
printf("Enter the no. of columns: ");
scanf("%d",&f);
printf("Enter the Data Elements of second matrices\n");
for(i=0;i<e;i++)
{
UNIT II - ARRAYS AND STRINGS
for(j=0;j<f;j++)
{
scanf("%d",&b[i][j]);
}
}
do
{
if(m==e&&n==f)
{
printf("Enter 1 for addtion or subtraction of matrices\n");
if(n==e){printf("Enter 2 for multiplication of matrices\n");}
printf("Enter 3 for transpose of first matrices\n");
}
else if(m!=n&&n==e)
{
printf("Enter 2 for multiplication of matrices\n");
printf("Enter 3 for transpose of first matrices\n");
}
else
{
printf("Enter 3 for transpose of first matrices\n");
}
scanf("%d",&ch);
switch(ch)
{
case 1 :
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum[i][j]=a[i][j]+b[i][j];
sub[i][j]=a[i][j]-b[i][j];
}
}
printf("Enter 1 for Addition or 2 for Subtraction: ");
scanf("%d",&opt);
switch(opt)
{
case 1 :
printf("The resultant matrices is :\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3d",sum[i][j]);
}
printf("\n");
}
break;
case 2 :
UNIT II - ARRAYS AND STRINGS
break; }}
while(ch>0);
getch();
}
UNIT II - ARRAYS AND STRINGS
Determinant of a Matrix
What is Determinant of a Matrix?
Determinant of a Matrix is a special number that is defined only for square matrices (matrices
which have same number of rows and columns). Determinant is used at many places in calculus
and other matrix related algebra, it actually represents the matrix in term of a real number which
can be used in solving system of linear equation and finding the inverse of a matrix.
How to calculate?
The value of determinant of a matrix can be calculated by following procedure –
For each element of first row or first column get cofactor of those elements and then multiply the
element with the determinant of the corresponding cofactor, and finally add them with alternate
signs. As a base case the value of determinant of a 1*1 matrix is the single value itself.
Cofactor of an element, is a matrix which we can get by removing row and column of that
element from that matrix.
Determinant of 2 x 2 Matrix:
Determinant of 3 x 3 Matrix:
return D;
}
UNIT II - ARRAYS AND STRINGS
Multidimensional Array
a2[i][j] = 10 * i + j;
}
This pair of nested loops sets a[1][2] to 12, a[4][1] to 41, etc. Since the first dimension of a2 is 5,
the first subscripting index variable, i, runs from 0 to 4. Similarly, the second subscript varies
from 0 to 6.
We could print a2 out (in a two-dimensional way, suggesting its structure) with a similar pair of
nested loops:
for (i = 0; i < 5; i = i + 1)
{
for (j = 0; j < 7; j = j + 1)
printf ("%d\t", a2[i][j]);
printf ("\n");
}
(The character \t in the printf string is the tab character.)
Just to see more clearly what's going on, we could make the ``row'' and ``column'' subscripts
explicit by printing them, too:
for(j = 0; j < 7; j = j + 1)
printf("\t%d:", j);
printf ("\n");
for(i = 0; i < 5; i = i + 1)
{
printf("%d:", i);
for(j = 0; j < 7; j = j + 1)
printf("\t%d", a2[i][j]);
printf("\n");
}
This last fragment would print
0: 1: 2: 3: 4: 5: 6:
0: 0 1 2 3 4 5 6
1: 10 11 12 13 14 15 16
2: 20 21 22 23 24 25 26
3: 30 31 32 33 34 35 36
4: 40 41 42 43 44 45 46
STRING
String are the combination of number of characters these are used to store any word in any
variable of constant. A string is an array of character. It is internally represented in system by
using ASCII value. Every single character can have its own ASCII value in the system. A
character string is stored in one array of character type.
e.g. “Ram” contains ASCII value per location, when we are using strings and then these strings
are always terminated by character ‘\0’. We use conversion specifies %s to set any string we can
have any string as follows:-
char nm [25].
When we store any value in nm variable then it can hold only 24 character because at the end of
the string one character is consumed automatically by ‘\0’.
#include<string.h>
There are some common inbuilt functions to manipulation on string in string.h file. these are as
follows:
UNIT II - ARRAYS AND STRINGS
It calculates the length of the string and returns its length. For example:
#include<string.h>
string = "Mumbai";
printf("Length = %d",strlen(string));
The above code displays 5 because Mumbai consists of 5 characters. Note: it does not count null
character.
Now it removes the null character from string1 and joins the first character of string2 at that
position. Now, string1 consists of both string1 and string2 in joined form. Example:
#include<string.h>=
strcat(string1,string2);=
It returns 0 if string1 is same as string2 and returns 1 if they are not same. Example:
#include<string.h>
UNIT II - ARRAYS AND STRINGS
if(strcmp(string1,string2)==0){
}else{
strcpy(destination_string, source_string);
#include<string.h>
strcpy(destination,source);
These are some of the functions in string.h for string operation. To use these functions you must
include header file <string.h>. But we can make our own functions to perform above task
without including string,h. Here is the complete source code that has own functions find_length
(like strlen) to find the length, join_strings( like strcat) for joining strings, compare_strings(like
strcmp) for comparing two strings and copy_string(like strcpy) to copy one string from another.
Observer carefully the code, if you are a beginner, you will learn a lot of things about string
operation.
///fundamental string operation, lenth, concatenation, compare and copy strings without
string.h
#
include < stdio.h > #include < stdlib.h >
int find_length(char string[]) {
int len = 0, i;
for (i = 0; string[i] != '\0'; i++) {
UNIT II - ARRAYS AND STRINGS
len++;
}
return len;
}
void join_strings(char string1[], char string2[]) {
int i, len1, len2;
len1 = find_length(string1);
len2 = find_length(string2);
for (i = len1; i < len1 + len2; i++) {
string1[i] = string2[i - len1];
}
string1[i] = '\0'; //adding null character at the end of input
}
case 2:
printf("Enter two strings: ");
scanf("%s%s", string1, string2);
join_strings(string1, string2);
printf("The concatenated string is %s", string1);
break;
case 3:
printf("Enter two strings: ");
scanf("%s%s", string1, string2);
if (compare_strings(string1, string2) == 0) {
printf("They are equal");
} else {
printf("They are not equal");
}
break;
case 4:
printf("Enter a string: ");
scanf("%s", string1);
printf("String1 = %s\n");
printf("After copying string1 to string 2\n");
copy_string(string2, string1);
printf("String2 = %s", string2);
break;
case 5:
exit(0);
}
}
return 0;
}