0% found this document useful (0 votes)
2 views

C_Tutorial_Day_9

The document provides a comprehensive overview of programming in C, focusing on arrays and strings, including their definitions, manipulations, and operations such as searching, insertion, and deletion. It covers both single-dimensional and multi-dimensional arrays, along with practical examples and code snippets for various operations like finding the largest or smallest element, adding matrices, and determining the day of a given date. The course is designed for individuals with basic C knowledge and spans a duration of 6 hours.

Uploaded by

Deepak
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 views

C_Tutorial_Day_9

The document provides a comprehensive overview of programming in C, focusing on arrays and strings, including their definitions, manipulations, and operations such as searching, insertion, and deletion. It covers both single-dimensional and multi-dimensional arrays, along with practical examples and code snippets for various operations like finding the largest or smallest element, adding matrices, and determining the day of a given date. The course is designed for individuals with basic C knowledge and spans a duration of 6 hours.

Uploaded by

Deepak
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
You are on page 1/ 17

Programming in C: Arrays and Strings

Arrays and
and Strings

Course Duration: 6 Hrs


Pre- Requisites: Basic C concepts.

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

characters, representation sparse matrices.

Created and Verified by B.K. Deepak


Date: 20/12/2004
Programming in C: Arrays and Strings

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.

Created and Verified by B.K. Deepak


Date: 20/12/2004
Programming in C: Arrays and Strings

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.

Naming and Declaring Arrays


The rules for assigning names to arrays are the same as for variable names. An array name must be unique
in that program which means it should not be used for another array or for any other identifier (variable, con-
stant). The array declaration consists of an array name followed by the number of elements in the array must
be enclosed in square brackets.
When you declare an array, you can specify the number of elements with a literal constant or with a symbolic
constant created with the #define directive.
The following are few array declarations:

#define MONTH 12
int months[MONTH];
is equivalent to this statement:
int months[12];

Created and Verified by B.K. Deepak


Date: 20/12/2004
Programming in C: Arrays and Strings

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

diff  remainder of (diff / 7)


select case diff
case 0 : Display “It is Monday”
case 1 : Display “It is Tuesday”
case 2 : Display “It is Wednesday”
case 3 : Display “It is Thursday”
case 4 : Display “It is Friday”
case 5 : Display “It is Saturday”
case 6 : Display “It is Sunday”
end select
end.

Created and Verified by B.K. Deepak


Date: 20/12/2004
Programming in C: Arrays and Strings

/* Calendar of any date */


#include <stdio.h>
void main()
{
int dd, mm, yy, leap, diff, yr, mn;
int months[12]={31,28,31,30,31,30,31,31,30,31,30,31};
char days[7][7]={ ”Mon”,”Tues”,”Wednes”,”Thurs”,”Fri”,”Satur”,”Sun”};
char sep[2];
printf(“\n\tEnter date in dd-mm-yyyy format : “);
scanf(“%d%[/- ]%d%[/- ]%d”, &dd, sep, &mm, sep, &yy);
if( ( (yy%4==0) && (yy%100!=0) ) || (yy%400 == 0) )
months[1]=29;
yr=1753;
diff=yy - yr;
leap = 0;
for ( yr=1753 ; yr < yy ; yr++)
{
if( ( (yr%4==0) && (yr%100!=0) ) || (yr%400 == 0) )
leap=leap+1;
}
diff = (diff + leap) % 7;
diff = diff + dd -1;
for(mn=0;mn<mm-1;mn++)
{
diff = diff + months[mn];
}
diff = diff % 7;
printf(“\n\tThe day is %sday”,days[diff]);
}

Created and Verified by B.K. Deepak


Date: 20/12/2004
Programming in C: Arrays and Strings

How an array is useful?


Problem: WAP to find out highest marks among 5 students marks.
#include <stdio.h>
void main()
{
int marks1, marks2, marks3, marks4, marks5;
int great=0;
printf(“\n\tEnter marks of Student 1 : “);
scanf(“%d”, &marks1);
printf(“\n\tEnter marks of Student 2 : “);
scanf(“%d”, &marks2);
printf(“\n\tEnter marks of Student 3 : “);
scanf(“%d”, &marks3);
printf(“\n\tEnter marks of Student 4 : “);
scanf(“%d”, &marks4);
printf(“\n\tEnter marks of Student 5 : “);
scanf(“%d”, &marks5);
if (great < marks1)
great=marks1;
if (great < marks2)
great=marks2;
if (great < marks3)
great=marks3;
if (great < marks4)
great=marks4;
if (great < marks5)
great=marks5;
printf(“\n\tHighest marks is : %d“, great);
}

Created and Verified by B.K. Deepak


Date: 20/12/2004
Programming in C: Arrays and Strings

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;

for(i=0; i<5; i++)


{
printf(“\n\tEnter marks of Student %d : “, i+1 );
scanf(“%d”, &marks[i]);
}

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.

Created and Verified by B.K. Deepak


Date: 20/12/2004
Programming in C: Arrays and Strings

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];

Created and Verified by B.K. Deepak


Date: 20/12/2004
Programming in C: Arrays and Strings

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.

Created and Verified by B.K. Deepak


Date: 20/12/2004
Programming in C: Arrays and Strings

Inserting elements into an array


Given below is the flow for inserting the values into an array at run time

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.

Created and Verified by B.K. Deepak


Date: 20/12/2004
Programming in C: Arrays and Strings

Deleting elements from an array


Listed below is the flow for deleting the value of an element from an array at
run time
DELETE(array[], int num, int item)
Begin
index  0
found false
repeat until (index< num)
if item=array[index] then
found true
break
endif
index=index+1
end of repeat
if found=true then
n  index
repeat until (n<num-1)
array[n]=array[n+1]
n  n+1
end of repeat
else
Display “Item not found”
endif
end.

Created and Verified by B.K. Deepak


Date: 20/12/2004
Programming in C: Arrays and Strings

Find the largest of the elements of an array

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
-------;
-------;

Created and Verified by B.K. Deepak


Date: 20/12/2004
Programming in C: Arrays and Strings

Find the smallest of the elements of an array

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
-------;
-------;

Created and Verified by B.K. Deepak


Date: 20/12/2004
Programming in C: Arrays and Strings

/* Addition of two matrices */


#include <stdio.h>
void main()
{
static int mat_a[10][10], mat_b[10][10], mat_c[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<rows; i++)
for( j=0; j<cols; j++)
scanf(“%d” , &mat_b[i][j]);
for( i=0; i<rows; i++)
for( j=0; j<cols; j++)
mat_c[i][j]=mat_a[i][j] + mat_b[i][j];
printf(“\n\tMatrix C is :\n “);
for( i=0; i<rows; i++)
{
for( j=0; j<cols; j++)
printf(“%3d ” , &mat_c[i][j]);
printf(“\n”);
}
}
Created and Verified by B.K. Deepak
Date: 20/12/2004
Programming in C: Arrays and Strings

/* 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 */

Created and Verified by B.K. Deepak


Date: 20/12/2004
Programming in C: Arrays and Strings

1. WAP to addition of two matrices.


2. WAP to find out transpose of matrix.
3. WAP to multiply two matrices.
4. WAP to find out greatest and second highest marks from an array list.
5. WAP to sort the array using bubble sort.
6. WAP to search the item using binary search.
7. WAP to search using linear search.

Created and Verified by B.K. Deepak


Date: 20/12/2004

You might also like