BCA, JIMS Vasant Kunj-II teaches C language to First Semester students. In this pdf, you can read the fundamentals of Array. JIMS Vasant Kunj-II is one of the best BCA colleges in Delhi NCR with an updated Curriculum.
Axa Assurance Maroc - Insurer Innovation Award 2024
Arrays Fundamentals Unit II
1. Jagannath Institute of Management Sciences
Vasant Kunj-II, New Delhi - 110070
Subject Name: Programming In C
Department of Information Technology
Created By: Dr. Arpana Chaturvedi
@Dr. Arpana Chaturvedi
3. Topics to be Covered
▰ Arrays
▰ Types of Arrays
▰ One Dimensional Array manipulation
▰ Searching an element in an Array
▰ Insertion of a element in an Array
▰ Deletion of an element from an Array
▰ Finding the largest/smallest element in an Array
4. Introduction to Arrays in C
@Dr. Arpana Chaturvedi
▰ An array is defined as the collection of similar type of data items stored at
contiguous memory locations.
▰ Arrays are the derived data type in C programming language which can store the
primitive type of data such as int, char, double, float, etc.
▰ It also has the capability to store the collection of derived data types, such as
pointers, structure, etc.
▰ The array is the simplest data structure where each data element can be randomly
accessed by using its index number.
▰ It is a best practice to initialize an array to zero or null while declaring, if we don’t
assign any values to array.
TYPES OF C ARRAYS:
▰ There are 2 types of C arrays. They are,
▰ One dimensional array
▰ Multi dimensional array: Two dimensional array,Three dimensional array etc.
5. Need of an Array in C
@Dr. Arpana Chaturvedi
In Case you need to store 100 different Integer Values to store Score of a Candidate,
you might declare and process it in the below mentioned way
6. Need of an Array in C
@Dr. Arpana Chaturvedi
Instead of declaring individual variables, such as score0, score1, ..., and score99, you
declare one array variable such as score and use score[0], score[1], and ..., score[99]
to represent individual variables. A specific element in an array is accessed by an
index.
9. Initialization of an Array in C
@Dr. Arpana Chaturvedi
Only fixed-length arrays can be initialized when they are defined. Variable
length arrays must be initialized by inputting or assigning the values.
10. Example of an Array in C
@Dr. Arpana Chaturvedi
/*C Program to explain Use of Array,
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of Required Input tvariable and an array
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;
clrscr();
printf("ntExample of Array Initialization, Declaration and
Accessn");
printf("n
t**********************************************************n");
//Accept Inputs of Different data Types
/* initialize elements of array n to 0 */
printf("nt The Initialization of Elelment in an
Array")
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100; /* set element at location i to i +
100 */
}
/* output each array element's value */
14. Example to Swap Adjacent Element in an Array
in C
@Dr. Arpana Chaturvedi
/*C program to swap adjacent elements of an one dimensional
array.,
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
#define MAX 100
void main()
{
//Declaration of Required Input tvariable and an array
int arr[MAX],n,i;
int temp;
clrscr();
printf("ntExample of Swapping Adjacent Elements of an
Array");
printf("n
t*************************************************");
//prompt the user to enter size of an array
printf("nt Enter total number of elementst:t ");
scanf("%d",&n);
//value of n must be even
if(n%2 !=0)
{
printf("Total number of elements should be EVEN.");
return;
15. Output: Example to Swap Adjacent Element in
an Array in C
@Dr. Arpana Chaturvedi
16. Searching an Element in C
@Dr. Arpana Chaturvedi
▰ Searching is the process of finding a given value position in a list of values.
▰ Searching means that given a value, we want to find the location (index) of the first
element in the array that contains that value.
▰ It decides whether a search key is present in the data or not.
▰ Searching Techniques
▰ To search an element in a given array, it can be done in following ways:
1. Sequential Search
2. Binary Search
20. Sequential Search or Linear Search
▰ Sequential search is also called as Linear Search.
▰ Sequential search starts at the beginning of the list and checks every element of
the list.
▰ It is a basic and simple search algorithm.
▰ Sequential search compares the element with all the other elements given in the
list. If the element is matched, it returns the value index, else it returns -1.
▰ The above figure shows how sequential search works. It searches an element or
value from an array till the desired element or value is not found. If we search the
element 25, it will go step by step in a sequence order. It searches in a sequence
order. Sequential search is applied on the unsorted or unordered list when there
are fewer elements in a list.
@Dr. Arpana Chaturvedi
22. Example of Sequential Search
@Dr. Arpana Chaturvedi
/*C Program to explain Linear Search
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of Required Input and Output variables
int arr[50], search, cnt, num;
clrscr();
printf("ntExample of Linear Search Of an Element");
printf("nt*************************************n");
printf(" ntEnter the number of elements in array t:t");
scanf("%d",&num);
printf("nt Enter %d integer(s)in an Arrayn", num);
for (cnt = 0; cnt < num; cnt++)
{
printf("nt Element at arr[%d] t=t ",cnt);
scanf("%d", &arr[cnt]);
}
printf(" ntEnter the number to searcht:t");
scanf("%d", &search);
24. Binary Search in C
@Dr. Arpana Chaturvedi
▰ Binary Search is used for searching an element in a sorted array.
▰ It is a fast search algorithm with run-time complexity of O(log n).
▰ Binary search works on the principle of divide and conquer.
▰ This searching technique looks for a particular element by comparing the middle
most element of the collection.
▰ It is useful when there are large number of elements in an array.
▰ The above array is sorted in ascending order. As we know binary search is applied
on sorted lists only for fast searching.
25. Working of Binary Search in C
@Dr. Arpana Chaturvedi
For example, if searching an element 25 in the 7-element array, following figure
shows how binary search works:
Binary searching starts with
middle element. If the
element is equal to the
element that we are
searching then return true. If
the element is less than then
move to the right of the list
or if the element is greater
than then move to the left of
the list. Repeat this, till you
find an element.
28. Example of Binary Search in C
@Dr. Arpana Chaturvedi
/*C Program to explain Binary Search
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of Required Input and Output
variables
int f, l, m, size, i, sElement, list[50]; //int f,
l ,m : First, Last, Middle
clrscr();
printf("ntExample of Binary Search Of an
Element");
printf("nt*************************************
n");
printf("nt Enter the size of the listt:t ");
scanf("%d",&size);
printf("nt Enter %d integer values t:t ",
size);
for (i = 0; i < size; i++)
{
30. Insertion of an element in an Array C
@Dr. Arpana Chaturvedi
Input the array elements, the position of the new element to be inserted and the new
element.
Insert the new element at that position and shift the rest of the elements to right by
one position.
31. Insertion of an element in an Array in C
@Dr. Arpana Chaturvedi
/* C program to insert an element in an array
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
#define N 100
void main()
{
//Declaration of Required Input and Output variables
int size;
int arr[N];
int i;
int pos;
int ele;
clrscr();
printf("ntExample of Insertion Of an Element in an Array");
printf("nt**********************************************");
printf("nt Enter the size of the listt:t ");
scanf("%d",&size);
printf("nt Enter %d integer values t:t ", size);
for (i = 0; i < size; i++)
{
printf("nt Element at arr[%d] t=t ",i);
33. Deletion of an Element from an Array in
C
@Dr. Arpana Chaturvedi
Input the array elements, the position of the new element to be inserted and the new
element.
Delete the element and shift the rest of the elements to left by one position.
34. Example of Deletion of an Element from
an Array in C
@Dr. Arpana Chaturvedi
/* C program to delete an element in an array
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
#define N 100
void main()
{
//Declaration of Required Input and Output variables
int arr[N], pos, c, n,size;
clrscr();
printf("ntExample of Deletion Of an Element in an Array");
printf("nt**********************************************");
printf("nt Enter the size of the listt:t ");
scanf("%d",&size);
printf("nt Enter %d integer values t:t ", size);
for (c = 0; c < size; c++)
{
printf("nt Element at arr[%d] t=t ",c);
scanf("%d",&arr[c]);
}
printf("nt Enter the position from which element is to be
36. Finding the largest/smallest element
in an Array
@Dr. Arpana Chaturvedi
Program to find the smallest and largest elements
in an array is discussed here. Given an array, the
task is to find the largest and smallest elements of
the array.
Method 1: Traverse the array iteratively and keep
track of the smallest and largest element until the
end of the array.
Method 2: Traverse the array recursively and keep
track of the smallest and largest element until the
end of the array.
▰ For example, consider the array.
▰ arr = {1, 2, 3, 4, 5}
▰ Smallest element : 1
▰ Largest element : 5
Algorithm to find the smallest
and largest numbers in an
array
• Input the array elements.
• Initialize small = large =
arr[0]
• Repeat from i = 2 to n
• if(arr[i] > large)
• large = arr[i]
• if(arr[i] < small)
• small = arr[i]
• Print small and large.
37. Program to Find Largest and Smallest
Element from an Array
@Dr. Arpana Chaturvedi
/* C program to find the smallest and largest element in an
array
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of Required Input and Output variables
int arr[50],i,large,small,size;
clrscr();
printf("ntExample of Searching Smallest and Largest
Element in an Array");
printf("n
t*************************************************************
");
printf("nt Enter the size of the listt:t ");
scanf("%d",&size);
printf("nt Enter %d integer values t:t ", size);
for (i = 0; i < size; i++)
{
printf("nt Element at arr[%d] t=t ",i);
38. Output to show smallest and Largest
Element of an Array
@Dr. Arpana Chaturvedi
39. Properties of an Array
@Dr. Arpana Chaturvedi
The array contains the following properties.
▰ Each element of an array is of same data type and carries the same size, i.e., int =
4 bytes.
▰ Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
▰ Elements of the array can be randomly accessed since we can calculate the
address of each element of the array with the given base address and the size of
the data element.
40. Advantages-Disadvantages of an Array in C
@Dr. Arpana Chaturvedi
Advantages:
1) Code Optimization: Less code to
the access the data.
2) Ease of traversing: By using the
for loop, we can retrieve the elements
of an array easily.
3) Ease of sorting: To sort the
elements of the array, we need a few
lines of code only.
4) Random Access: We can access
any element randomly using the array.
Disadvantages:
1) Fixed Size: Whatever size, we
define at the time of declaration of
the array, we can't exceed the
limit. So, it doesn't grow the size
dynamically like Linked List
42. Storage Classes in C
Storage class specifiers in C language tells the compiler where to store a variable,
how to store the variable, what is the initial value of the variable and life time of the
variable.
SYNTAX:
▰ storage_specifier data_type variable _name;
TYPES OF STORAGE CLASS SPECIFIERS IN C:
▰ There are 4 storage class specifiers available in C language. They are,
▰ auto
▰ extern
▰ static
▰ register
@Dr. Arpana Chaturvedi
43. Types of Storage Specifiers in C
@Dr. Arpana Chaturvedi
Storage Specifier Description
auto
Storage place: CPU Memory
Initial/default value: Garbage value
Scope: local
Life: Within the function only.
extern
Storage place: CPU memory
Initial/default value: Zero
Scope: Global
Life: Till the end of the main program. Variable definition might be
anywhere in the C program.
static
Storage place: CPU memory
Initial/default value: Zero
Scope: local
Life: Retains the value of the variable between different function calls.
register
Storage place: Register memory
Initial/default value: Garbage value
Scope: local
Life: Within the function only.
44. Key Points to use Storage Specifiers in C
▰ For faster access of a variable, it is better to go for register specifiers rather than
auto specifiers.
▰ Because, register variables are stored in register memory whereas auto variables
are stored in main CPU memory.
▰ Only few variables can be stored in register memory. So, we can use variables as
register that are used very often in a C program.
@Dr. Arpana Chaturvedi
45. EXAMPLE PROGRAM FOR AUTO
VARIABLE IN C:
The scope of this auto variable is within the function only. It is
equivalent to local variable. All local variables are auto variables
by default.
@Dr. Arpana Chaturvedi
46. EXAMPLE PROGRAM FOR AUTO
VARIABLE IN C:
@Dr. Arpana Chaturvedi
/*C Program to explain Auto Variables
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
void increment(void);
void main()
{
//Declaration of Required Input to accept input using
unformatted getch function
int c;
char str[100];
clrscr();
printf("ntExample of Auto Variables");
printf("nt*************************n");
increment();
increment();
increment();
increment();
printf("nnttMade By Dr. Arpana");
getch();
}
47. OUTPUT OF EXAMPLE PROGRAM FOR
AUTO VARIABLE IN C:
@Dr. Arpana Chaturvedi
48. EXAMPLE PROGRAM FOR STATIC
VARIABLE IN C:
▰ Static variables retain the value of the variable between
different function calls.
@Dr. Arpana Chaturvedi
49. EXAMPLE PROGRAM FOR STATIC
VARIABLE IN C:
@Dr. Arpana Chaturvedi
/*C Program to explain Static Variables
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
void increment(void);
void main()
{
clrscr();
printf("ntExample of Static Variablesn");
printf("nt***************************n");
increment();
increment();
increment();
increment();
printf("nnttMade By Dr. Arpana");
getch();
}
void increment(void)
{
static int i = 0 ;
printf ( "nt The Value of Static variable is %d ", i ) ;
50. OUTPUT OF EXAMPLE PROGRAM FOR
STATIC VARIABLE IN C:
@Dr. Arpana Chaturvedi
51. EXAMPLE PROGRAM FOR EXTERN
VARIABLE IN C:
▰ The scope of this extern variable is throughout the main
program. It is equivalent to global variable. Definition for
extern variable might be anywhere in the C program
@Dr. Arpana Chaturvedi
52. EXAMPLE PROGRAM FOR EXTERN
VARIABLE IN C:
@Dr. Arpana Chaturvedi
/*C Program to explain External Storage Variable,
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
//Declared and initialised Variable externally or global
int x = 10 ;
void main()
{
//Declaration of Required Input to accept input using
unformatted getch function
extern int y;
clrscr();
printf("ntExample of External Storage Variablen");
printf("nt*************************************n");
//Display the value accessed of External variables
printf("nt The value of x is %d n",x);
printf("nt The value of y is %d",y);
printf("nnttMade By Dr. Arpana");
getch();
}
int y=50;
//Declared and initialised Variable externally or Outside
53. OUTPUT OF EXAMPLE PROGRAM FOR
EXTERN VARIABLE IN C:
@Dr. Arpana Chaturvedi
54. EXAMPLE PROGRAM FOR REGISTER
VARIABLE IN C:
▰ Register variables are also local variables, but stored in
register memory. Whereas, auto variables are stored in main
CPU memory.
▰ Register variables will be accessed very faster than the
normal variables since they are stored in register memory
rather than main memory.
▰ But, only limited variables can be used as register since
register size is very low. (16 bits, 32 bits or 64 bits)
@Dr. Arpana Chaturvedi
55. EXAMPLE PROGRAM FOR REGISTER
VARIABLE IN C:
@Dr. Arpana Chaturvedi
/*C Program to explain Register Variables
Made by Dr. Arpana on 11-07-20 */
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of Variables
register int i;
int arr[5];// declaring array
clrscr();
printf("ntExample of Register Variablesn");
printf("nt*****************************n");
arr[0] = 10;// Initializing array
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
for (i=0;i<5;i++)
{
// Accessing each variable
printf("nt The value of arr[%d] using Register variables
is %d n", i, arr[i]);
}
56. OUTPUT OF EXAMPLE PROGRAM FOR
REGISTER VARIABLE IN C:
@Dr. Arpana Chaturvedi