Unit Iii
Unit Iii
Unit Iii
UNIT-III
Syllabus:
Arrays: one and two dimensional arrays, creating, accessing and manipulating elements
of arrays, row/column major formats, standard problems (addition and multiplication of
two matrixes, Mean, Variance, SD, maximum and minimum elements using arrays).
Functions: Declaring a function, Signature of a function, passing parameters to functions,
passing arrays to functions, recursion, Standard problems [Factorial of a given number,
sum of natural numbers using recursion), External, Auto, Local, Static, Register Variables.
Arrays:
An array is a special type of variable used to store multiple values of same data
type at a time.
An array is a collection of similar data items stored in continuous memory
locations with single name.
Types of Arrays in C:
In c programming language, arrays are classified into two types. They are as
follows...
Single Dimensional Array / One Dimensional Array
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 an Array:
In C programming language, when we want to create an array we must know the
datatype of values to be stored in that array and also the number of values to be
stored in that array.
General syntax to create an array...
datatype arrayName [ size ] ;
Syntax for creating an array with size and initial values
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...
arrayName [ indexValue ] ;
For the above example the individual elements can be denoted as follows...
Selection Sort
Quick Sort
Merge Sort, etc.,
Arrays are used to implement Data structures
We use single dimensional arrays to implement datastructures like...
Stack Using Arrays
Queue Using Arrays
Arrays are also used to implement CPU Scheduling Algorithms
Example Programs:
Write a C Program to input and print array elements
Program:
#include <stdio.h>
#define MAX_SIZE 1000
void main()
{
int arr[MAX_SIZE];
int i,N;
printf("Enter size of array: ");
scanf("%d",&N);
printf("Enter %d elements in the array : \n",N);
for(i=0;i<N;i++)
{
printf("a[%d]= ",i);
scanf("%d",&arr[i]);
}
printf("\nElements in array are: ");
for(i=0;i<N;i++)
{
printf("%d\t",arr[i]);
}
}
Output:
Enter 5 elements in the array :
a[0]= 1
a[1]= 2
a[2]= 3
a[3]= 4
a[4]= 5
Elements in array are: 1 2 3 4 5
Write a C Program to print sum of array elements
Program:
#include <stdio.h>
#define MAX_SIZE 1000
void main()
{
int arr[MAX_SIZE];
int i,N,sum=0;
printf("Enter size of array: ");
scanf("%d",&N);
printf("Enter %d elements in the array : \n",N);
for(i=0;i<N;i++)
{
printf("a[%d]= ",i);
scanf("%d",&arr[i]);
}
for(i=0;i<N;i++)
{
sum=sum+arr[i];
}
printf("Sum of array elements is %d",sum);
}
Output:
Enter size of array: 5
Enter 5 elements in the array :
a[0]= 10
a[1]= 20
a[2]= 30
a[3]= 40
a[4]= 50
Sum of array elements is 150
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.
Linear Search Algorithm (Sequential Search Algorithm):
Linear search, often known as sequential search, is the most basic search
technique. In this type of search, you go through the entire list and try to fetch a
match for a single element. If you find a match, then the address of the matching
target element is returned.
On the other hand, if the element is not found, then it returns a NULL value.
Linear search is implemented using following steps...
Step 1 - Read the search element from the user.
Step 2 - Compare the search element with the first element in the list.
Step 3 - If both are matched, then display "Given element is found!!!" and terminate
the function
Step 4 - If both are not matched, then compare search element with the next element
in the list.
Step 5 - Repeat steps 3 and 4 until search element is compared with last element in
the list.
Step 6 - If last element in the list also doesn't match, then display "Element is not
found!!!" and terminate the function.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int list[20],size,i,sElement;
printf("Enter size of the list: ");
scanf("%d",&size);
printf("Enter any %d integer values: \n",size);
for(i=0; i<size;i++)
{
printf("a[%d]=",i);
scanf("%d",&list[i]);
}
printf("Enter the element to be Search: ");
scanf("%d",&sElement);
for(i=0; i<size;i++)
{
if(sElement==list[i])
{
printf("Element is found at %d index",i);
break;
}
}
if(i==size)
{
printf("Given element is not found in the list!!!");
}
getch();
}
Output:
Enter size of the list: 5
Enter any 5 integer values:
a[0]=10
a[1]=20
a[2]=30
a[3]=40
a[4]=50
Enter the element to be Search: 30
Element is found at 2 index
Binary Search Algorithm:
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 cannot be used for a list of elements arranged in random
order.
Binary search is implemented using following steps...
Step 1 - Read the search element from the user.
Step 2 - Find the middle element in the sorted 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.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int first, last, middle, size, i, sElement, list[100];
printf("Enter the size of the list: ");
scanf("%d",&size);
printf("Enter %d integer values in Assending order\n",size);
for (i=0;i<size;i++)
{
scanf("%d",&list[i]);
}
printf("Enter value to be search: ");
scanf("%d",&sElement);
first=0;
last=size-1;
middle=(first+last)/2;
while(first<=last)
{
if (list[middle]<sElement)
first=middle+1;
else if(list[middle]==sElement)
{
printf("Element found at index %d.\n",middle);
break;
}
else
{
last = middle-1;
}
middle=(first+last)/2;
}
if (first>last)
{
printf("Element Not found in the list.");
}
}
Output:
Enter the size of the list: 5
Enter 5 integer values in Assending order
10
11
12
13
14
Enter value to be search: 14
Element found at index 4.