SlideShare a Scribd company logo
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
Subject: Programming In C
Topic: Unit II- Part I
Arrays
@Dr. Arpana Chaturvedi
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
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.
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
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.
Processing Data in an Array
@Dr. Arpana Chaturvedi
Declaration of an Arrays in C
@Dr. Arpana Chaturvedi
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.
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 */
Output of an Arrays in C
@Dr. Arpana Chaturvedi
Exchanging the Array elements: Wrong Way
@Dr. Arpana Chaturvedi
One array cannot be copied to another using assignment.
Exchanging the values of Element: Right Way
@Dr. Arpana Chaturvedi
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;
Output: Example to Swap Adjacent Element in
an Array in C
@Dr. Arpana Chaturvedi
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
Searching an Element in C
@Dr. Arpana Chaturvedi
Searching Data in Unsorted Array
@Dr. Arpana Chaturvedi
Unsuccessful Search in Unordered
Array in C
@Dr. Arpana Chaturvedi
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
Sequential Search in an Array in C
@Dr. Arpana Chaturvedi
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);
Output of Linear Search
@Dr. Arpana Chaturvedi
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.
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.
Binary Search in C
@Dr. Arpana Chaturvedi
Unsuccessful Binary Search in C
@Dr. Arpana Chaturvedi
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++)
{
Output of Binary Search
@Dr. Arpana Chaturvedi
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.
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);
Ouput after Insertion in an Array in C
@Dr. Arpana Chaturvedi
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.
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
Output of Deletion Example
@Dr. Arpana Chaturvedi
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.
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);
Output to show smallest and Largest
Element of an Array
@Dr. Arpana Chaturvedi
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.
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
Storage Classes
@Dr. Arpana Chaturvedi
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
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.
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
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
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();
}
OUTPUT OF EXAMPLE PROGRAM FOR
AUTO VARIABLE IN C:
@Dr. Arpana Chaturvedi
EXAMPLE PROGRAM FOR STATIC
VARIABLE IN C:
▰ Static variables retain the value of the variable between
different function calls.
@Dr. Arpana Chaturvedi
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 ) ;
OUTPUT OF EXAMPLE PROGRAM FOR
STATIC VARIABLE IN C:
@Dr. Arpana Chaturvedi
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
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
OUTPUT OF EXAMPLE PROGRAM FOR
EXTERN VARIABLE IN C:
@Dr. Arpana Chaturvedi
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
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]);
}
OUTPUT OF EXAMPLE PROGRAM FOR
REGISTER VARIABLE IN C:
@Dr. Arpana Chaturvedi
Thank You !!

More Related Content

PDF
Arrays 2 Dimensional Unit 2 Part 1.pdf
PPTX
Introduction to Eclipse
PDF
C intro
PDF
Consuming and Creating Libraries in C++
PDF
Unit iii
PDF
PPTX
PPTX
Linking the prospective and retrospective provenance of scripts
Arrays 2 Dimensional Unit 2 Part 1.pdf
Introduction to Eclipse
C intro
Consuming and Creating Libraries in C++
Unit iii
Linking the prospective and retrospective provenance of scripts

What's hot (20)

PDF
Tapp 2014 (belhajjame)
PPTX
Chapter 2.datatypes and operators
PPT
Getting started with c++
PPTX
Compiler and symbol table
PPTX
What is symbol table?
PDF
Introduction to c programming
DOCX
Yacc topic beyond syllabus
PPTX
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
PPT
358 33 powerpoint-slides_1-introduction-c_chapter-1
PDF
Introduction to database-ER Model
PPT
Unit3 cspc
PDF
Introduction to c++
PPS
Clanguage
PDF
Python Programming - XII. File Processing
PPT
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
PPTX
Introduction to Python Basics
PDF
C++ Version 2
PPTX
Cpu-fundamental of C
PPT
PPTX
LISP: Input And Output
Tapp 2014 (belhajjame)
Chapter 2.datatypes and operators
Getting started with c++
Compiler and symbol table
What is symbol table?
Introduction to c programming
Yacc topic beyond syllabus
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
358 33 powerpoint-slides_1-introduction-c_chapter-1
Introduction to database-ER Model
Unit3 cspc
Introduction to c++
Clanguage
Python Programming - XII. File Processing
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
Introduction to Python Basics
C++ Version 2
Cpu-fundamental of C
LISP: Input And Output
Ad

Similar to Arrays Fundamentals Unit II (20)

PPTX
Arrays in C.pptx
PDF
CP PPT_Unit IV computer programming in c.pdf
PDF
Cunit3.pdf
PPTX
ppt on arrays in c programming language.pptx
PDF
Data-Structure-using-C-Rajesh-Pandey.pdf
PDF
CSEG1001Unit 3 Arrays and Strings
PDF
Unit ii data structure-converted
PPTX
placement preparation for Array Searching.pptx
PPTX
ARRAY in python and c with examples .pptx
PPTX
Searching and Sorting Algorithms in Data Structures
PDF
35001622067_SOUMYADIP MAITY .pdf C programming
PDF
Array data structure
PDF
PPT
358 33 powerpoint-slides_5-arrays_chapter-5
PPTX
2 Arrays & Strings.pptx
PPTX
Various Operations Of Array(Data Structure Algorithm).pptx
PDF
Array data structure
PPTX
DATA STRUCTURE linear search ALGORITHM-PROGRAM
PDF
1D Array
PPTX
DSA - Array.pptx
Arrays in C.pptx
CP PPT_Unit IV computer programming in c.pdf
Cunit3.pdf
ppt on arrays in c programming language.pptx
Data-Structure-using-C-Rajesh-Pandey.pdf
CSEG1001Unit 3 Arrays and Strings
Unit ii data structure-converted
placement preparation for Array Searching.pptx
ARRAY in python and c with examples .pptx
Searching and Sorting Algorithms in Data Structures
35001622067_SOUMYADIP MAITY .pdf C programming
Array data structure
358 33 powerpoint-slides_5-arrays_chapter-5
2 Arrays & Strings.pptx
Various Operations Of Array(Data Structure Algorithm).pptx
Array data structure
DATA STRUCTURE linear search ALGORITHM-PROGRAM
1D Array
DSA - Array.pptx
Ad

More from Arpana Awasthi (10)

PDF
Unit 5 Part 1 Macros
PDF
Unit 2 Part 1.2 Data Types.pdf
PDF
Introduction To Python.pdf
PDF
Unit 2 Part 1 POLYMORPHISM.pdf
PDF
Unit 2 Part 1 Constructors.pdf
PDF
Eclipse - GUI Palette
PDF
Machine Learning: Need of Machine Learning, Its Challenges and its Applications
PDF
File Handling in C Part I
PDF
Programming language
PDF
Role of machine learning in detection, prevention and treatment of cancer
Unit 5 Part 1 Macros
Unit 2 Part 1.2 Data Types.pdf
Introduction To Python.pdf
Unit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 Constructors.pdf
Eclipse - GUI Palette
Machine Learning: Need of Machine Learning, Its Challenges and its Applications
File Handling in C Part I
Programming language
Role of machine learning in detection, prevention and treatment of cancer

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Empathic Computing: Creating Shared Understanding
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Approach and Philosophy of On baking technology
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
cuic standard and advanced reporting.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
sap open course for s4hana steps from ECC to s4
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation theory and applications.pdf
A Presentation on Artificial Intelligence
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Empathic Computing: Creating Shared Understanding
MIND Revenue Release Quarter 2 2025 Press Release
Digital-Transformation-Roadmap-for-Companies.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectral efficient network and resource selection model in 5G networks
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Approach and Philosophy of On baking technology
Spectroscopy.pptx food analysis technology
Programs and apps: productivity, graphics, security and other tools
cuic standard and advanced reporting.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Mobile App Security Testing_ A Comprehensive Guide.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
sap open course for s4hana steps from ECC to s4

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
  • 2. Subject: Programming In C Topic: Unit II- Part I Arrays @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.
  • 7. Processing Data in an Array @Dr. Arpana Chaturvedi
  • 8. Declaration of an Arrays in C @Dr. Arpana Chaturvedi
  • 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 */
  • 11. Output of an Arrays in C @Dr. Arpana Chaturvedi
  • 12. Exchanging the Array elements: Wrong Way @Dr. Arpana Chaturvedi One array cannot be copied to another using assignment.
  • 13. Exchanging the values of Element: Right Way @Dr. Arpana Chaturvedi
  • 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
  • 17. Searching an Element in C @Dr. Arpana Chaturvedi
  • 18. Searching Data in Unsorted Array @Dr. Arpana Chaturvedi
  • 19. Unsuccessful Search in Unordered Array in C @Dr. Arpana Chaturvedi
  • 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
  • 21. Sequential Search in an Array in C @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);
  • 23. Output of Linear Search @Dr. Arpana Chaturvedi
  • 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.
  • 26. Binary Search in C @Dr. Arpana Chaturvedi
  • 27. Unsuccessful Binary Search in C @Dr. Arpana Chaturvedi
  • 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++) {
  • 29. Output of Binary Search @Dr. Arpana Chaturvedi
  • 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);
  • 32. Ouput after Insertion in an Array in C @Dr. Arpana Chaturvedi
  • 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
  • 35. Output of Deletion Example @Dr. Arpana Chaturvedi
  • 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