Unit Iii

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Computational Thinking and Programming

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

Department of Computer Science & Engineering Page: 1


Computational Thinking and Programming

datatype arrayName [ size ] = {value1, value2, ...} ;


 Syntax for creating an array without size and with initial values
datatype arrayName [ ] = {value1, value2, ...} ;
 In the above syntax, the datatype specifies the type of values we store in that array
and size specifies the maximum number of values that can be stored in that array.
 Example:
int a[3];
 Here, the compiler allocates 6 bytes of contiguous memory locations with a single
name 'a' and tells the compiler to store three different integer values (each in 2
bytes of memory) into that 6 bytes of memory.
 For the above declaration, the memory is organized as follows...

 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...

Accessing Individual Elements of an Array:


 The individual elements of an array are identified using the combination of
'arrayName' and 'indexValue'.
 We use the following general syntax to access individual elements of an array...

Department of Computer Science & Engineering Page: 2


Computational Thinking and Programming

arrayName [ indexValue ] ;
 For the above example the individual elements can be denoted as follows...

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.
Declaration of Two Dimensional Array:
 We use the following general syntax for declaring a two dimensional array..
datatype arrayName [ rowSize ] [ columnSize ] ;
int matrix_A [2][3] ;
 The above declaration of two dimensional array reserves 6 continuous memory
locations of 2 bytes each in the form of 2 rows and 3 columns.
Initialization of Two Dimensional Array:
 We use the following general syntax for declaring and initializing a two
dimensional array with specific number of rows and coloumns with initial values.
datatype arrayName [rows][colmns] = {{r1c1value, r1c2value, ...},{r2c1,
r2c2,...}...} ;
int matrix_A [2][3] = { {1, 2, 3},{4, 5, 6} } ;
 The above declaration of two-dimensional array reserves 6 contiguous memory
locations of 2 bytes each in the form of 2 rows and 3 columns. And the first row is
initialized with values 1, 2 & 3 and second row is initialized with values 4, 5 & 6.

Department of Computer Science & Engineering Page: 3


Computational Thinking and Programming

 We can also initialize as follows...


 Example Code
int matrix_A [2][3] = { {1, 2, 3}, {4, 5, 6} } ;
Accessing Individual Elements of Two Dimensional Array:
 In a c programming language, to access elements of a two-dimensional array we
use array name followed by row index value and column index value of the
element that to be accessed. Here the row and column index values must be
enclosed in separate square braces. In case of the two-dimensional array the
compiler assigns separate index values for rows and columns.
 We use the following general syntax to access the individual elements of a two-
dimensional array...
arrayName [ rowIndex ] [ columnIndex ]
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.
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 ...
 Linear Search
 Binary Search
Arrays are used to implement Sorting Algorithms
 We use single dimensional arrays to implement sorting algorihtms like ...
 Insertion Sort
 Bubble Sort

Department of Computer Science & Engineering Page: 4


Computational Thinking and Programming

 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]);
}
}

Department of Computer Science & Engineering Page: 5


Computational Thinking and Programming

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

Department of Computer Science & Engineering Page: 6


Computational Thinking and Programming

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.

Department of Computer Science & Engineering Page: 7


Computational Thinking and Programming

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();
}

Department of Computer Science & Engineering Page: 8


Computational Thinking and Programming

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.

Department of Computer Science & Engineering Page: 9


Computational Thinking and Programming

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

Department of Computer Science & Engineering Page: 10


Computational Thinking and Programming

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.

Department of Computer Science & Engineering Page: 11

You might also like