UNIT III CProgramming
UNIT III CProgramming
In the above memory allocation, all the three memory locations have a common name 'a'. So
accessing individual memory location is not possible directly. Hence compiler not only
allocates the memory but also assigns a numerical reference value to every individual memory
location of an array. This reference number is called "Index" or "subscript" or "indices". Index
values for the above example are as follows...
For example, if we want to assign a value to the second memory location of above array 'a', we
use the following statement...
Example Code
a [1] = 100 ;
The result of the above assignment statement is as follows...
Types of Arrays in C
In c programming language, arrays are classified into two types. They are as follows...
1. Single Dimensional Array / One Dimensional Array
2. Multi Dimensional Array
Single Dimensional Array
In c programming language, single dimensional arrays are used to store list of values of same
datatype. In other words, single dimensional arrays are used to store a row of values. In single
dimensional array, data is stored in linear form. Single dimensional arrays are also called
as one-dimensional arrays, Linear Arrays or simply 1-D Arrays.
Declaration of Single Dimensional Array
We use the following general syntax for declaring a single dimensional array...
datatype arrayName [ size ] ;
Example Code
int rollNumbers [60] ;
The above declaration of single dimensional array reserves 60 continuous memory locations of
2 bytes each with the name rollNumbers and tells the compiler to allow only integer values
into those memory locations.
Initialization of Single Dimensional Array
We use the following general syntax for declaring and initializing a single dimensional array
with size and initial values.
datatype arrayName [ size ] = {value1, value2, ...} ;
Example Code
int marks [6] = { 89, 90, 76, 78, 98, 86 } ;
The above declaration of single dimensional array reserves 6 contiguous memory locations of
2 bytes each with the name marks and initializes with value 89 in first memory location, 90 in
We use the following general syntax to access individual elements of single dimensional array...
arrayName [ indexValue ]
Example Code
marks [2] = 99 ;
In the above statement, the third element of 'marks' array is assinged with value '99'.
Multi Dimensional Array
An array of arrays is called as multi dimensional array. In simple words, an array created with
more than one dimension (size) is called as multi dimensional array. Multi dimensional array
can be of two dimensional array or three dimensional array or four dimensional array or
more...
Most popular and commonly used multi dimensional array is two dimensional array. The 2-
D arrays are used to store data in the form of table. We also use 2-D arrays to create
mathematical matrices.
We use the following general syntax to access the individual elements of a two-dimensional
array...
arrayName [ rowIndex ] [ columnIndex ]
Example Code
matrix_A [0][1] = 10 ;
In the above statement, the element with row index 0 and column index 1 of matrix_A array
is assinged with value 10.
Code to read two dimensional Array elements
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
scanf(“%d”,&a[i][j]);
}
}
Applications of Arrays in C
In c programming language, arrays are used in wide range of applications. Few of them are as
follows...
● Arrays are used to Store List of values
In c programming language, single dimensional arrays are used to store list of values of same
datatype. In other words, single dimensional arrays are used to store a row of values. In single
dimensional array data is stored in linear form.
● Arrays are used to Perform Matrix Operations
We use two dimensional arrays to create matrix. We can perform various operations on matrices
using two dimensional arrays.
● Arrays are used to implement Search Algorithms
We use single dimensional arrays to implement search algorihtms like ...
1. Linear Search
2. Binary Search
● Arrays are used to implement Sorting Algorithms
We use single dimensional arrays to implement sorting algorihtms like ...
1. Insertion Sort
2. Bubble Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort, etc.,
● Arrays are used to implement Datastructures
We use single dimensional arrays to implement datastructures like...
1. Stack Using Arrays
2. Queue Using Arrays
● Arrays are also used to implement CPU Scheduling Algorithms
Concept Structure :
Stepwise Procedure:
Linear Search Method:
In the Linear Searching method, the elements in the list are scanned from the first element till either the
required key element is found or the list is over. The Key element is compared with every element. This
is also called as sequential search method. The list elements are not required to be in specific order.
The advantage of the method is its simplicity. But it is efficient only for small sized lists. The maximum
iterations required will be the size of the list ‘N’.
Pictorial Description:
Let us consider a list of integer elements with N = 12 as:
18 89 11 45 24 40 32 15 231 7 48 59 Elements
1. If search Key is 40 :
Starting with index = 0, the current element is compared with key. The Key 40 is found at index position
6. This is depicted pictorially as below:
18 89 11 45 24 40 32 15 231 7 48 59 Elements
A[0] 1 2 3 4 5 6 7 8 9 10 A[11]
18 89 11 45 24 40 32 15 231 7 48 59
A[0] 1 2 3 4 5 6 7 8 9 10 A[11]
18 89 11 45 24 40 32 15 231 7 48 59
A[0] 1 2 3 4 5 6 7 8 9 10 A[11]
18 89 11 45 24 40 32 15 231 7 48 59
A[0] 1 2 3 4 5 6 7 8 9 10 A[11]
18 89 11 45 24 40 32 15 231 7 48 59
A[0] 1 2 3 4 5 6 7 8 9 10 A[11]
18 89 11 45 24 40 32 15 231 7 48 59
#include<stdio.h>
#include<conio.h>
if(a[i]==key)
return i+1;
}
return 0;
} /algorithm ends/
void main()
{
int i,n, key,pos,a[20];
clrscr();
printf("enter the number of elements n\n");
scanf("%d",&n);
printf("enter the values\n"); /reading array elements/
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the key element\n");
scanf("%d",&key);
pos=linear(key,a,n);
if(pos==0)
printf("unsucessful\n");
else
printf("key found at position =%d\n",pos);
getch();
}
OUTPUT:
What is Search?
Search is a process of finding a value in a list of values. In other words, searching is the process
of locating given value position in a list of values.
Binary Search Algorithm
Binary search algorithm finds a given element in a list of elements with O(log n) time
complexity where n is total number of elements in the list. The binary search algorithm can be
used with only a sorted list of elements. That means the binary search is used only with a list
of elements that are already arranged in an order. The binary search can not be used for a list
of elements arranged in random order. This search process starts comparing the search element
with the middle element in the list. If both are matched, then the result is "element found".
Otherwise, we check whether the search element is smaller or larger than the middle element
in the list. If the search element is smaller, then we repeat the same process for the left sublist
of the middle element. If the search element is larger, then we repeat the same process for the
right sublist of the middle element. We repeat this process until we find the search element in
the list or until we left with a sublist of only one element. And if that element also doesn't match
with the search element, then the result is "Element not found in the list".
• Step 3 - Compare the search element with the middle element in the sorted list.
• Step 4 - If both are matched, then display "Given element is found!!!" and terminate
the function.
• Step 5 - If both are not matched, then check whether the search element is smaller or
larger than the middle element.
• Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5
for the left sublist of the middle element.
• Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5
for the right sublist of the middle element.
• Step 8 - Repeat the same process until we find the search element in the list or until
sublist contains only one element.
• Step 9 - If that element also doesn't match with the search element, then display
"Element is not found in the list!!!" and terminate the function.
REFER LAB RECORD FOR PROGRAM
Example
Consider the following list of elements and the element to be searched.
Arranging the data in either ascending or descending manner based on certain key in the record
is known as SORTING.
Arranging the data in either ascending or descending manner based on certain key in the record
is known as SORTING. Suppose we want to search for the telephone number of a person in the
telephone directory. if the telephone directory is not sorted in alphabetical order, we may have
to search for the entire telephone directory. just to know whether the telephone number of a
person exists or not. Since the telephone directory is sorted, we know how easy is to search for
telephone number.so,it is required to arrange the names in alphabetical order.
There are different types of sorting techniques, among few of them are listed below
✓ Bubble Sort
✓ Selection Sort
✓ Insertion Sort
✓ Quick Sort
✓ Merge Sort
✓ Shell Sort
✓ Radix Sort
✓ Heap Sort
Bubble Sort-
Bubble sort is the easiest sorting algorithm to implement.
It is inspired by observing the behavior of air bubbles over foam.
It is an in-place sorting algorithm.
It uses no auxiliary data structures (extra space) while sorting.
Now, we shall implement the above bubble sort algorithm on this array.
Step-01:
Step-02:
Step-03:
Step-04:
Finally after the first pass, we see that the largest element 11 reaches its correct position.
Step-05:
Step-06:
Step-07:
printf("\nOriginal Matrix:\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
Strings in C
String is a set of characters that are enclosed in double quotes. In the C programming language,
strings are created using one dimension array of character datatype.
There are two methods to create strings and they are as follows...
1. Using one dimensional array of character datatype ( static memory allocation )
2. Using a pointer array of character datatype ( dynamic memory allocation )
}
printf("\nThe length of the string = %d",i);
getch();
}
To find the length of a string using built in function
#include <stdio.h>
#include <string.h>
void main()
{
char str1[100], str2[100], str3[100];
clrscr();
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
printf("\nLength of first string: %d", strlen(str1));
printf("\nLength of second string: %d", strlen(str2));
strcpy(str3, str2);
printf("\n\nAfter copying, str3: %s", str3);
strcat(str1, str2);
printf("\nAfter concatenation, str1: %s", str1);
getch();
}
*****Kindly read all programs taught in Class*****