Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
45 views

Lecture Notes DSA Unit-I

The document discusses data structures and algorithms using C. It covers topics like data types, abstract data types, data structures, arrays, and more. Arrays are presented as the first data structure, with details on representation, addressing, and operations. The relationship between algorithms, data structures, and programs is also examined.

Uploaded by

likeicannot4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Lecture Notes DSA Unit-I

The document discusses data structures and algorithms using C. It covers topics like data types, abstract data types, data structures, arrays, and more. Arrays are presented as the first data structure, with details on representation, addressing, and operations. The relationship between algorithms, data structures, and programs is also examined.

Uploaded by

likeicannot4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

‭Data Structures & Algorithms Using C‬

‭Unit 1: Data Structures Overview and Arrays (8 Lectures)‬


‭Concepts‬ ‭of‬ ‭Data‬ ‭Types,‬ ‭Abstract‬ ‭Data‬ ‭Type,‬ ‭Data‬ ‭Structure,‬ ‭Fundamental‬ ‭and‬
‭Derived‬ ‭Data‬ ‭Types.‬ ‭Importance‬ ‭of‬ ‭data‬ ‭structures.‬ ‭Array‬ ‭as‬ ‭a‬ ‭data‬ ‭structure‬
‭(characteristics,‬ ‭advantages,‬ ‭disadvantages).Representation‬ ‭of‬ ‭arrays‬ ‭–‬ ‭single‬ ‭and‬
‭multidimensional.‬ ‭Address‬ ‭calculation‬ ‭of‬ ‭array‬ ‭element‬ ‭using‬ ‭column‬ ‭and‬ ‭row‬ ‭major‬
‭ordering.‬ ‭Address‬ ‭translation‬ ‭functions‬ ‭for‬ ‭one‬ ‭&‬ ‭two‬ ‭dimensional‬ ‭arrays.‬ ‭Insertion‬ ‭and‬
‭deletion in arrays. Use of arrays for large number representation.‬

m‬
‭Lecture No. 1-8‬

co
‭ he term data structure is used to describe the way data is stored, and the term‬
T

l.
‭algorithm‬‭is‬‭used‬‭to‬‭describe‬‭the‬‭way‬‭data‬‭is‬‭processed.‬‭Data‬‭structures‬‭and‬‭algorithms‬‭are‬
‭interrelated.‬ ‭Choosing‬ ‭a‬ ‭data‬ ‭structure‬ ‭affects‬ ‭the‬ ‭kind‬ ‭of‬ ‭algorithm‬ ‭you‬ ‭might‬ ‭use,‬ ‭and‬
‭choosing‬‭an‬‭algorithm‬‭affects‬‭the‬‭data‬‭structures‬‭we‬‭use.An‬‭Algorithm‬‭is‬‭a‬‭finite‬‭sequence‬‭of‬
ai
‭instructions,‬‭each‬‭of‬‭which‬‭has‬‭a‬‭clear‬‭meaning‬‭and‬‭can‬‭be‬‭performed‬‭with‬‭a‬‭finite‬‭amount‬
‭of‬ ‭effort‬ ‭in‬ ‭a‬ ‭finite‬ ‭length‬ ‭of‬ ‭time.‬ ‭No‬ ‭matter‬ ‭what‬ ‭the‬ ‭input‬ ‭values‬ ‭may‬ ‭be,‬ ‭an‬ ‭algorithm‬
gm

‭terminates after executing a finite number of instructions.‬


c@

‭1.1 Introduction to Data Structures:‬ ‭(Lecture-1)‬


cb

‭ ata‬ ‭Structure‬‭is‬‭a‬‭way‬‭of‬‭organising,‬‭managing,‬‭and‬‭storing‬‭data‬‭in‬‭a‬‭computer‬‭so‬
D
‭that‬‭it‬‭can‬‭be‬‭accessed‬‭and‬‭used‬‭efficiently.‬‭It‬‭provides‬‭a‬‭systematic‬‭way‬‭of‬‭organising‬‭and‬
.l

‭accessing‬‭data‬‭that‬‭enables‬‭fast‬‭and‬‭efficient‬‭operations‬‭on‬‭the‬‭data.‬‭The‬‭main‬‭goal‬‭of‬‭using‬
‭Data Structure is to reduce the complexity of accessing and manipulating data.‬
fi

‭Data‬‭structure‬‭is‬‭a‬‭representation‬‭of‬‭logical‬‭relationships‬‭existing‬‭between‬‭individual‬
‭elements‬‭of‬‭data.‬‭In‬‭other‬‭words,‬‭a‬‭data‬‭structure‬‭defines‬‭a‬‭way‬‭of‬‭organising‬‭all‬‭data‬‭items‬
‭that‬ ‭considers‬ ‭not‬ ‭only‬ ‭the‬ ‭elements‬ ‭stored‬ ‭but‬ ‭also‬ ‭their‬ ‭relationship‬ ‭to‬ ‭each‬ ‭other.‬ ‭The‬
r‭o

‭term‬ ‭data‬ ‭structure‬ ‭is‬ ‭used‬ ‭to‬ ‭describe‬ ‭the‬‭way‬‭data‬‭is‬‭stored.To‬‭develop‬‭a‬‭program‬‭of‬‭an‬


‭algorithm‬ ‭we‬ ‭should‬‭select‬‭an‬‭appropriate‬‭data‬‭structure‬‭for‬‭that‬‭algorithm.‬‭Therefore,‬‭data‬
‭structure is represented as:‬
‭Algorithm + Data structure = Program‬

‭ ‬ ‭data‬ ‭structure‬ ‭is‬ ‭said‬ ‭to‬ ‭be‬ ‭linear‬ ‭if‬‭its‬‭elements‬‭form‬‭a‬‭sequence‬‭or‬‭a‬‭linear‬‭list.‬


A
‭The‬ ‭linear‬ ‭data‬ ‭structures‬ ‭like‬ ‭an‬ ‭array,‬ ‭stacks,‬ ‭queues‬ ‭and‬ ‭linked‬ ‭lists‬ ‭organise‬ ‭data‬ ‭in‬
‭linear‬ ‭order.‬ ‭A‬ ‭data‬ ‭structure‬ ‭is‬ ‭said‬ ‭to‬ ‭be‬ ‭non‬ ‭linear‬ ‭if‬ ‭its‬ ‭elements‬ ‭form‬ ‭a‬ ‭hierarchical‬
‭classification‬ ‭where‬ ‭data‬ ‭items‬ ‭appear‬‭at‬‭various‬‭levels.Trees‬‭and‬‭Graphs‬‭are‬‭widely‬‭used‬
‭non-linear‬ ‭data‬ ‭structures.‬ ‭Tree‬ ‭and‬ ‭graph‬ ‭structures‬ ‭represent‬ ‭hierarchical‬ ‭relationships‬
‭between‬ ‭individual‬ ‭data‬ ‭elements.‬ ‭Graphs‬ ‭are‬ ‭nothing‬ ‭but‬ ‭trees‬ ‭with‬ ‭certain‬ ‭restrictions‬
‭removed.‬
‭Data structures are divided into two types:‬
‭• Primitive data structures.‬

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

‭• Non-primitive data structures.‬

‭Primitive Data‬‭Structures are the basic data structures that directly operate upon the‬
‭ achine instructions.‬‭They have different representations on different computers. Integers,‬
m
‭floating point numbers, character constants, string constants and pointers come under this‬
‭Category.‬
‭Non-primitive‬‭data‬‭structures‬‭are‬‭more‬‭complicated‬‭data‬‭structures‬‭and‬‭are‬‭derived‬
‭from‬ ‭primitive‬ ‭data‬ ‭structures.‬ ‭They‬ ‭emphasise‬ ‭on‬ ‭grouping‬ ‭same‬ ‭or‬ ‭different‬ ‭data‬ ‭items‬
‭with‬ ‭relationship‬ ‭between‬ ‭each‬ ‭data‬ ‭item‬‭.‬ ‭Arrays,‬ ‭lists‬ ‭and‬ ‭files‬‭come‬‭under‬‭this‬‭category.‬
‭Figure 1.1 shows the classification of data structures.‬

m‬
co
l.
ai
gm
c@
cb

‭1.2 Data structures: Organization of data‬


.l

‭The‬ ‭collection‬ ‭of‬ ‭data‬ ‭a‬ ‭program‬ ‭have‬ ‭some‬ ‭kind‬ ‭of‬ ‭structure‬ ‭or‬ ‭organization.No‬
‭matter‬ ‭how‬ ‭complex‬ ‭data‬ ‭structures‬ ‭are‬ ‭they‬ ‭can‬ ‭be‬ ‭broken‬ ‭down‬ ‭into‬ ‭two‬ ‭fundamental‬
fi

‭types:‬
‭• Contiguous‬
r‭o

‭• Non-Contiguous.‬

I‭n‬‭contiguous‬‭structures‬‭,‬‭terms‬‭of‬‭data‬‭are‬‭kept‬‭together‬‭in‬‭memory‬‭(either‬‭RAM‬‭or‬
‭in‬‭a‬‭file).An‬‭array‬‭is‬‭an‬‭example‬‭of‬‭a‬‭contiguous‬‭structure.‬‭Since‬‭each‬‭element‬‭in‬‭the‬‭array‬‭is‬
‭located‬‭next‬‭to‬‭one‬‭or‬‭two‬‭other‬‭elements.‬‭In‬‭contrast,‬‭items‬‭in‬‭a‬‭non-contiguous‬‭structure‬
‭are‬ ‭scattered‬ ‭in‬ ‭memory,‬ ‭but‬ ‭they‬ ‭are‬ ‭linked‬‭to‬‭each‬‭other‬‭in‬‭some‬‭way.‬‭A‬‭linked‬‭list‬‭is‬‭an‬
‭example of a non-contiguous data structure (as shown in figure 1.2).‬

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

‭1.3 Abstract Data Type (ADT):‬
‭The‬‭design‬‭of‬‭a‬‭data‬‭structure‬‭involves‬‭more‬‭than‬‭just‬‭its‬‭organisation.‬‭You‬‭also‬‭need‬
‭to‬ ‭plan‬ ‭for‬‭the‬‭way‬‭the‬‭data‬‭will‬‭be‬‭accessed‬‭and‬‭processed‬‭–‬‭that‬‭is,‬‭how‬‭the‬‭data‬‭will‬‭be‬
‭interpreted‬ ‭actually,‬ ‭non-contiguous‬ ‭structures‬ ‭–‬ ‭including‬ ‭lists,‬ ‭tree‬ ‭and‬ ‭graphs‬ ‭–‬ ‭can‬ ‭be‬
‭implemented‬ ‭either‬ ‭contiguously‬ ‭or‬ ‭non-‬ ‭contiguously‬ ‭likewise,‬ ‭the‬ ‭structures‬ ‭that‬ ‭are‬
‭normally‬ ‭treated‬ ‭as‬ ‭contiguously‬ ‭-‬ ‭arrays‬ ‭and‬ ‭structures‬ ‭–‬ ‭can‬ ‭also‬ ‭be‬ ‭implemented‬
‭non-contiguously.‬
‭The‬ ‭notion‬ ‭of‬ ‭a‬ ‭data‬ ‭structure‬ ‭in‬ ‭the‬ ‭abstract‬ ‭needs‬ ‭to‬ ‭be‬ ‭treated‬ ‭differently‬ ‭from‬
‭whatever‬ ‭is‬ ‭used‬ ‭to‬ ‭implement‬ ‭the‬ ‭structure.‬ ‭The‬ ‭abstract‬ ‭notion‬ ‭of‬ ‭a‬ ‭data‬ ‭structure‬ ‭is‬
‭defined in terms of the operations we plan to perform on the data.‬
‭Considering‬ ‭both‬ ‭the‬‭organisation‬‭of‬‭data‬‭and‬‭the‬‭expected‬‭operations‬‭on‬‭the‬‭data,‬
‭leads‬ ‭to‬ ‭the‬ ‭notion‬ ‭of‬ ‭an‬ ‭abstract‬ ‭data‬ ‭type.‬ ‭An‬ ‭abstract‬ ‭data‬ ‭type(ADT)‬ ‭is‬ ‭a‬ ‭theoretical‬
‭construct‬ ‭that‬ ‭consists‬ ‭of‬‭data‬‭as‬‭well‬‭as‬‭the‬‭operations‬‭to‬‭be‬‭performed‬‭on‬‭the‬‭data‬‭while‬

m‬
‭hiding implementation.‬
‭Some‬‭common‬‭abstract‬‭data‬‭types‬‭are‬‭linked‬‭lists,‬‭stack,‬‭queue,‬‭trees,‬‭trie,‬‭graphs‬

co
‭etc.‬

l.
‭1.4 Fundamental and Derived Data Types‬
ai
‭The‬ ‭basic‬ ‭data(also‬ ‭known‬ ‭as‬ ‭fundamental‬ ‭data‬ ‭types)‬ ‭types‬ ‭are‬ ‭defined‬ ‭in‬ ‭the‬
‭programming‬‭language‬‭C‬‭or‬‭C++‬‭form‬‭the‬‭fundamental‬‭basis‬‭of‬‭any‬‭program,‬‭as‬‭the‬‭name‬
‭suggests. e.g : int, char, float, double are fundamental types in C/C++.‬
gm

‭Derived‬ ‭data‬ ‭types:‬ ‭The‬ ‭data‬ ‭types‬ ‭are‬ ‭formed‬ ‭by‬ ‭using‬ ‭a‬ ‭collective‬ ‭of‬ ‭a‬ ‭few‬
‭fundamental‬‭data‬‭types,‬‭hence‬‭are‬‭derived‬‭data‬‭types,‬‭as‬‭the‬‭name‬‭suggests.‬‭e.g.‬ ‭arrays,‬
c@

‭structures, unions etc are some examples of derived data types.‬


‭Note‬‭:‬‭For more clear understanding refer to figure 1.1‬
cb

‭1.5 Importance of data structures‬


‭Data‬ ‭structures‬ ‭are‬ ‭essential‬ ‭tools‬ ‭for‬ ‭storing‬ ‭and‬ ‭organising‬ ‭data‬ ‭in‬ ‭computer‬
.l

‭programs.‬‭They‬‭provide‬‭an‬‭efficient‬‭way‬‭to‬‭access‬‭and‬‭manipulate‬‭data,‬‭making‬‭it‬‭easier‬‭for‬
‭programmers‬ ‭to‬ ‭write‬ ‭code‬ ‭that‬ ‭is‬ ‭both‬ ‭efficient‬ ‭and‬ ‭easy‬ ‭to‬ ‭understand.‬ ‭Here‬ ‭are‬ ‭some‬
‭reasons why data structures are important:‬
fi

‭a.‬ ‭Efficient‬ ‭Data‬ ‭Access:‬ ‭Data‬ ‭structures‬ ‭provide‬ ‭efficient‬ ‭methods‬ ‭for‬
‭accessing‬ ‭and‬ ‭searching‬ ‭data.‬ ‭By‬ ‭organising‬‭data‬‭in‬‭a‬‭specific‬‭way,‬‭we‬‭can‬
r‭o

‭access it faster and more efficiently.‬


‭b.‬ ‭Resource‬‭Optimization:‬‭Data‬‭structures‬‭can‬‭help‬‭optimise‬‭the‬‭use‬‭of‬‭system‬
‭resources‬‭such‬‭as‬‭memory,‬‭disk‬‭space,‬‭and‬‭processing‬‭power.‬‭By‬‭using‬‭data‬
‭structures‬ ‭that‬ ‭are‬ ‭appropriate‬ ‭for‬ ‭the‬ ‭task‬ ‭at‬ ‭hand,‬ ‭we‬ ‭can‬ ‭reduce‬ ‭the‬
‭amount of memory and processing power required to manipulate the data.‬
‭c.‬ ‭Code‬‭Reusability:‬‭Data‬‭structures‬‭can‬‭be‬‭reused‬‭across‬‭multiple‬‭programs,‬
‭saving‬ ‭time‬ ‭and‬ ‭effort.‬ ‭Once‬ ‭a‬ ‭data‬ ‭structure‬ ‭is‬ ‭implemented‬ ‭and‬ ‭tested,‬ ‭it‬
‭can‬ ‭be‬ ‭used‬ ‭in‬ ‭different‬ ‭programs‬ ‭and‬ ‭contexts,‬ ‭reducing‬ ‭the‬ ‭need‬ ‭to‬‭write‬
‭new code from scratch.‬
‭d.‬ ‭Scalability:‬ ‭As‬ ‭programs‬ ‭grow‬ ‭and‬ ‭data‬ ‭volumes‬ ‭increase,‬ ‭data‬ ‭structures‬
‭help‬ ‭ensure‬ ‭that‬ ‭the‬ ‭program‬ ‭remains‬ ‭scalable.‬ ‭By‬ ‭using‬ ‭the‬ ‭right‬ ‭data‬
‭structures,‬ ‭we‬ ‭can‬ ‭ensure‬ ‭that‬ ‭our‬ ‭programs‬ ‭can‬ ‭handle‬ ‭larger‬ ‭and‬ ‭more‬
‭complex data sets without sacrificing performance or stability.‬
‭In‬‭summary,‬‭data‬‭structures‬‭are‬‭important‬‭tools‬‭for‬‭storing,‬‭organising,‬‭and‬‭accessing‬
‭data‬ ‭in‬ ‭computer‬ ‭programs.‬ ‭They‬‭help‬‭optimise‬‭performance,‬‭reduce‬‭code‬‭complexity,‬‭and‬
‭ensure scalability, making them essential for modern software development.‬

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

‭1.6 Array as a data structure (characteristics, advantages, disadvantages) (Lecture 2)‬

‭An‬ ‭array‬ ‭is‬ ‭a‬ ‭collection‬ ‭of‬ ‭similar‬ ‭types‬ ‭of‬ ‭data‬‭items‬‭stored‬‭at‬‭contiguous‬‭memory‬
l‭ocations.‬‭The‬‭idea‬‭is‬‭to‬‭store‬‭multiple‬‭items‬‭of‬‭the‬‭same‬‭type‬‭together.‬‭This‬‭makes‬‭it‬‭easier‬
‭to‬‭calculate‬‭the‬‭position‬‭of‬‭each‬‭element‬‭by‬‭simply‬‭adding‬‭an‬‭offset‬‭to‬‭a‬‭base‬‭value,‬‭i.e.,‬‭the‬
‭memory‬ ‭location‬ ‭of‬ ‭the‬ ‭first‬ ‭element‬ ‭of‬ ‭the‬ ‭array‬ ‭(generally‬ ‭denoted‬ ‭by‬ ‭the‬ ‭name‬ ‭of‬ ‭the‬
‭array).‬

m‬
co
l.
ai
gm

‭Applications:‬
c@

‭Storing‬‭and‬‭accessing‬‭data:‬‭Arrays‬‭are‬‭used‬‭to‬‭store‬‭and‬‭retrieve‬‭data‬‭in‬‭a‬‭specific‬
‭order.‬‭For‬‭example,‬‭an‬‭array‬‭can‬‭be‬‭used‬‭to‬‭store‬‭the‬‭scores‬‭of‬‭a‬‭group‬‭of‬‭students,‬
cb

‭or the temperatures recorded by a weather station.‬


‭Sorting‬‭:‬ ‭Arrays‬ ‭can‬‭be‬‭used‬‭to‬‭sort‬‭data‬‭in‬‭ascending‬‭or‬‭descending‬‭order.‬‭Sorting‬
.l

‭algorithms such as bubble sort, merge sort, and quicksort rely heavily on arrays.‬
‭Searching:‬ ‭Arrays‬ ‭can‬ ‭be‬ ‭searched‬‭for‬‭specific‬‭elements‬‭using‬‭algorithms‬‭such‬‭as‬
fi

‭linear search and binary search.‬


‭Matrices:‬‭Arrays‬‭are‬‭used‬‭to‬‭represent‬‭matrices‬‭in‬‭mathematical‬‭computations‬‭such‬
r‭o

‭as matrix multiplication, linear algebra, and image processing.‬


‭Stacks‬ ‭and‬ ‭queues:‬ ‭Arrays‬ ‭are‬ ‭used‬ ‭as‬ ‭the‬ ‭underlying‬ ‭data‬ ‭structure‬ ‭for‬
‭implementing‬ ‭stacks‬ ‭and‬ ‭queues,‬‭which‬‭are‬‭commonly‬‭used‬‭in‬‭algorithms‬‭and‬‭data‬
‭structures.‬
‭Graphs:‬‭Arrays‬‭can‬‭be‬‭used‬‭to‬‭represent‬‭graphs‬‭in‬‭computer‬‭science.‬‭Each‬‭element‬
‭in‬‭the‬‭array‬‭represents‬‭a‬‭node‬‭in‬‭the‬‭graph,‬‭and‬‭the‬‭relationships‬‭between‬‭the‬‭nodes‬
‭are represented by the values stored in the array.‬
‭Dynamic‬‭programming:‬‭Dynamic‬‭programming‬‭algorithms‬‭often‬‭use‬‭arrays‬‭to‬‭store‬
‭intermediate results of subproblems in order to solve a larger problem.‬

‭Characteristics:‬
‭An array is always stored in consecutive memory locations.‬
‭It‬ ‭can‬ ‭store‬ ‭multiple‬ ‭values‬ ‭of‬ ‭similar‬ ‭type,‬ ‭which‬ ‭can‬ ‭be‬ ‭referred‬ ‭to‬ ‭with‬ ‭a‬ ‭single‬
‭name.‬

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

‭ he‬‭pointer‬‭points‬‭to‬‭the‬‭first‬‭location‬‭of‬‭the‬‭memory‬‭block,‬‭which‬‭is‬‭allocated‬‭to‬‭the‬
T
‭array name.‬
‭An‬‭array‬‭can‬‭either‬‭be‬‭an‬‭integer,‬‭character,‬‭or‬‭float‬‭data‬‭type‬‭that‬‭can‬‭be‬‭initialised‬
‭only during the declaration.‬
‭The‬ ‭particular‬ ‭element‬ ‭of‬‭an‬‭array‬‭can‬‭be‬‭modified‬‭separately‬‭without‬‭changing‬‭the‬
‭other elements.‬
‭All elements of an array can be distinguished with the help of index number.‬

‭Advantages:‬

‭ fficient‬ ‭access‬ ‭to‬ ‭elements:‬ ‭Arrays‬ ‭provide‬ ‭direct‬ ‭and‬ ‭efficient‬ ‭access‬ ‭to‬ ‭any‬
E
‭element‬ ‭in‬ ‭the‬ ‭collection.‬ ‭Accessing‬ ‭an‬ ‭element‬ ‭in‬ ‭an‬ ‭array‬ ‭is‬ ‭an‬ ‭O(1)‬ ‭operation,‬
‭meaning‬ ‭that‬ ‭the‬ ‭time‬ ‭required‬ ‭to‬ ‭access‬ ‭an‬ ‭element‬ ‭is‬ ‭constant‬ ‭and‬ ‭does‬ ‭not‬
‭depend on the size of the array.‬

m‬
‭Fast‬‭data‬‭retrieval:‬‭Arrays‬‭allow‬‭for‬‭fast‬‭data‬‭retrieval‬‭because‬‭the‬‭data‬‭is‬‭stored‬‭in‬
‭contiguous‬‭memory‬‭locations.‬‭This‬‭means‬‭that‬‭the‬‭data‬‭can‬‭be‬‭accessed‬‭quickly‬‭and‬

co
‭efficiently without the need for complex data structures or algorithms.‬
‭Memory‬‭efficiency:‬‭Arrays‬‭are‬‭a‬‭memory-efficient‬‭way‬‭of‬‭storing‬‭data.‬‭Because‬‭the‬

l.
‭elements‬‭of‬‭an‬‭array‬‭are‬‭stored‬‭in‬‭contiguous‬‭memory‬‭locations,‬‭the‬‭size‬‭of‬‭the‬‭array‬
‭is‬ ‭known‬ ‭at‬ ‭compile‬ ‭time.‬ ‭This‬ ‭means‬ ‭that‬ ‭memory‬ ‭can‬ ‭be‬ ‭allocated‬ ‭for‬ ‭the‬‭entire‬
ai
‭array in one block, reducing memory fragmentation.‬
‭Versatility:‬ ‭Arrays‬ ‭can‬ ‭be‬ ‭used‬ ‭to‬ ‭store‬ ‭a‬ ‭wide‬ ‭range‬ ‭of‬ ‭data‬ ‭types,‬ ‭including‬
gm

‭integers,‬‭floating-point‬‭numbers,‬‭characters,‬‭and‬‭even‬‭complex‬‭data‬‭structures‬‭such‬
‭as objects and pointers.‬
c@

‭Easy‬‭to‬‭implement:‬‭Arrays‬‭are‬‭easy‬‭to‬‭implement‬‭and‬‭understand,‬‭making‬‭them‬‭an‬
‭ideal choice for beginners learning computer programming.‬
cb

‭Compatibility‬ ‭with‬ ‭hardware:‬ ‭The‬ ‭array‬ ‭data‬ ‭structure‬ ‭is‬ ‭compatible‬ ‭with‬ ‭most‬
‭hardware‬‭architectures,‬‭making‬‭it‬‭a‬‭versatile‬‭tool‬‭for‬‭programming‬‭in‬‭a‬‭wide‬‭range‬‭of‬
.l

‭environments.‬

‭Disadvantages:‬
fi

‭Fixed‬ ‭size‬‭:‬ ‭Arrays‬ ‭have‬‭a‬‭fixed‬‭size‬‭that‬‭is‬‭determined‬‭at‬‭the‬‭time‬‭of‬‭creation.‬‭This‬


‭means‬ ‭that‬ ‭if‬ ‭the‬ ‭size‬ ‭of‬ ‭the‬ ‭array‬ ‭needs‬ ‭to‬ ‭be‬ ‭increased,‬ ‭a‬ ‭new‬ ‭array‬ ‭must‬ ‭be‬
r‭o

‭created‬‭and‬‭the‬‭data‬‭must‬‭be‬‭copied‬‭from‬‭the‬‭old‬‭array‬‭to‬‭the‬‭new‬‭array,‬‭which‬‭can‬
‭be time-consuming and memory-intensive.‬
‭Memory‬‭allocation‬‭issues:‬‭Allocating‬‭a‬‭large‬‭array‬‭can‬‭be‬‭problematic,‬‭particularly‬
‭in‬‭systems‬‭with‬‭limited‬‭memory.‬‭If‬‭the‬‭size‬‭of‬‭the‬‭array‬‭is‬‭too‬‭large,‬‭the‬‭system‬‭may‬
‭run out of memory, which can cause the program to crash.‬
‭Insertion‬ ‭and‬ ‭deletion‬ ‭issues:‬ ‭Inserting‬ ‭or‬ ‭deleting‬‭an‬‭element‬‭from‬‭an‬‭array‬‭can‬
‭be‬ ‭inefficient‬ ‭and‬ ‭time-consuming‬ ‭because‬ ‭all‬ ‭the‬ ‭elements‬ ‭after‬ ‭the‬ ‭insertion‬ ‭or‬
‭deletion point must be shifted to accommodate the change.‬
‭Wasted‬ ‭space‬‭:‬ ‭If‬ ‭an‬ ‭array‬ ‭is‬ ‭not‬‭fully‬‭populated,‬‭there‬‭can‬‭be‬‭wasted‬‭space‬‭in‬‭the‬
‭memory allocated for the array. This can be a concern if memory is limited.‬
‭Limited‬‭data‬‭type‬‭support:‬‭Arrays‬‭have‬‭limited‬‭support‬‭for‬‭complex‬‭data‬‭types‬‭such‬
‭as‬‭objects‬‭and‬‭structures,‬‭as‬‭the‬‭elements‬‭of‬‭an‬‭array‬‭must‬‭all‬‭be‬‭of‬‭the‬‭same‬‭data‬
‭type.‬

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

‭ ack‬ ‭of‬ ‭flexibility:‬ ‭The‬ ‭fixed‬ ‭size‬ ‭and‬ ‭limited‬ ‭support‬ ‭for‬ ‭complex‬ ‭data‬ ‭types‬ ‭can‬
L
‭make‬ ‭arrays‬ ‭inflexible‬ ‭compared‬ ‭to‬ ‭other‬ ‭data‬ ‭structures‬ ‭such‬ ‭as‬ ‭linked‬ ‭lists‬ ‭and‬
‭trees.‬

m‬
co
l.
ai
gm
c@
cb
.l
fi
r‭o

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

‭1.7 Representation of arrays in memory – single and multidimensional (Lecture 3)‬

‭One Dimensional Array (1D Array):‬


‭Representation of 1D array:‬

m‬
co
l.
‭Multi Dimensional Array (2D Array):‬
ai
‭Representation of 2 D array:‬
‭1.‬ ‭Row Major Ordering‬
gm

‭2.‬ ‭Column Major Ordering.‬


c@
cb
.l
fi
r‭o

‭Fig.‬‭2D array in memory‬

‭ .8‬‭Address‬‭calculation‬‭of‬‭array‬‭element‬‭using‬‭column‬‭and‬‭row‬‭major‬‭ordering(1D,2D‬
1
‭and 3D):‬
‭Address Translation Function(ATF):‬
‭Address of any element In the 1-D array:‬

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

‭Address of A[I] = B + W * (I – LB)‬‭where,‬
I‭‬‭= Subset of element whose address to be found,‬
‭B‬‭= Base address,‬
‭W‬‭= Storage size of one element store in any array(in byte),‬
‭LB‬‭= Lower Limit/Lower Bound of subscript(If not specified assume zero).‬

‭Fig.‬‭1D Array address‬

‭ xample:‬‭Given‬‭the‬‭base‬‭address‬‭of‬‭an‬‭array‬‭A[1300‬‭…………‬‭1900]‬‭as‬‭1020‬‭and‬‭the‬‭size‬
E
‭of each element is 2 bytes in the memory, find the address of A[1700].‬
‭Solution:‬
‭ iven,‬
G

m‬
‭Base address‬‭B‬‭= 1020‬
‭Lower Limit/Lower Bound of subscript‬‭LB‬‭= 1300‬

co
‭Storage size of one element store in any array‬‭W‬‭= 2 Byte‬
‭Subset of element whose address to be found‬‭I‬‭= 1700‬
‭Formula used:‬

l.
‭Address of‬‭A[I] = B + W * (I – LB)‬
‭Solution:‬
ai
‭Address of‬‭A[1700]‬‭= 1020 + 2 * (1700 – 1300)‬
‭= 1020 + 2 * (400)‬
gm

‭= 1020 + 800‬
‭Address of‬‭A[1700]‬‭= 1820‬
c@
cb

‭Address of any element In the 2-D array:‬ ‭(Lecture 4)‬


.l

‭The‬ ‭2-dimensional‬ ‭array‬ ‭can‬ ‭be‬ ‭defined‬ ‭as‬ ‭an‬ ‭array‬ ‭of‬ ‭arrays.‬ ‭The‬
‭2-Dimensional‬ ‭arrays‬ ‭are‬ ‭organised‬ ‭as‬ ‭matrices‬ ‭which‬ ‭can‬ ‭be‬ ‭represented‬ ‭as‬ ‭the‬
‭collection‬‭of‬‭rows‬‭and‬‭columns‬‭as‬‭array[M][N]‬‭where‬‭M‬‭is‬‭the‬‭number‬‭of‬‭rows‬‭and‬
fi

‭N is the number of columns‬‭.‬


r‭o

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

‭ o‬ ‭find‬ ‭the‬ ‭address‬ ‭of‬ ‭any‬ ‭element‬ ‭in‬ ‭a‬ ‭2-Dimensional‬ ‭array‬ ‭there‬ ‭are‬ ‭the‬
T
‭following two ways-‬
‭Row Major Order‬
‭Column Major Order‬
‭Row Major Order:‬
‭Row‬ ‭major‬ ‭ordering‬ ‭assigns‬ ‭successive‬ ‭elements,‬ ‭moving‬ ‭across‬ ‭the‬ ‭rows‬
‭and‬ ‭then‬ ‭down‬ ‭the‬ ‭next‬ ‭row,‬ ‭to‬ ‭successive‬ ‭memory‬ ‭locations.‬ ‭In‬ ‭simple‬ ‭language,‬
‭the elements of an array are stored in a Row-Wise fashion.‬
‭To‬ ‭find‬ ‭the‬ ‭address‬ ‭of‬ ‭the‬‭element‬‭using‬‭row-major‬‭order‬‭uses‬‭the‬‭following‬
‭formula:‬

‭ ddress of A[I][J] = B + W * ((I – LR) * N + (J – LC))‬ ‭where,‬


A

m‬
‭I‬‭= Row Subset of an element whose address to be found,‬
‭J‬‭= Column Subset of an element whose address to be found,‬
‭B‬‭= Base address,‬

co
‭W‬‭= Storage size of one element store in an array(in byte),‬
‭LR‬ ‭=‬ ‭Lower‬ ‭Limit‬ ‭of‬ ‭row/start‬ ‭row‬ ‭index‬ ‭of‬ ‭the‬ ‭matrix(If‬ ‭not‬ ‭given‬ ‭assume‬ ‭it‬ ‭as‬

l.
‭zero),‬
‭LC‬‭=‬‭Lower‬‭Limit‬‭of‬‭column/start‬‭column‬‭index‬‭of‬‭the‬‭matrix(If‬‭not‬‭given‬‭assume‬‭it‬
ai
‭as zero),‬
‭N‬‭= Number of column given in the matrix.‬
gm
c@

‭Fig.‬‭2D‬‭Row Major‬
cb

‭ xample:‬ ‭Given‬ ‭an‬ ‭array,‬ ‭arr[1………10][1………15]‬ ‭with‬ ‭base‬ ‭value‬ ‭100‬‭and‬‭the‬‭size‬‭of‬


E
‭each‬ ‭element‬ ‭is‬ ‭1‬‭Byte‬‭in‬‭memory.‬‭Find‬‭the‬‭address‬‭of‬‭arr[8][6]‬‭with‬‭the‬‭help‬‭of‬‭row-major‬
.l

‭order.‬
‭Solution:‬
fi

‭Given:‬
‭Base address‬‭B‬‭= 100‬
r‭o

‭Storage size of one element store in any array‬‭W‬‭= 1 Bytes‬


‭Row Subset of an element whose address to be found‬‭I‬‭= 8‬
‭Column Subset of an element whose address to be found‬‭J‬‭= 6‬
‭Lower Limit of row/start row index of matrix‬‭LR‬‭= 1‬
‭Lower Limit of column/start column index of matrix = 1‬
‭Number of column given in the matrix‬‭N‬‭= Upper Bound – Lower Bound + 1‬
‭= 15 – 1 + 1‬
‭= 15‬
‭Formula:‬
‭Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))‬
‭i.e.‬
‭Address of‬‭A[8][6]‬‭= 100 + 1 * ((8 – 1) * 15 + (6 – 1))‬
‭= 100 + 1 * ((7) * 15 + (5))‬
‭= 100 + 1 * (110)‬
‭Address of‬‭A[I][J]‬‭= 210‬

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

‭Column Major Order:‬ ‭(Lecture 5)‬

‭Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))‬‭where,‬


I‭ =‬‭Row Subset of an element whose address to be found,‬
‭J‬‭= Column Subset of an element whose address to be found,‬
‭B‬‭= Base address,‬
‭W‬‭= Storage size of one element store in any array(in byte),‬
‭LR‬‭= Lower Limit of row/start row index of matrix(If not given assume it as zero),‬
‭LC‬‭=‬‭Lower‬‭Limit‬‭of‬‭column/start‬‭column‬‭index‬‭of‬‭matrix(If‬‭not‬‭given‬‭assume‬‭it‬‭as‬
‭zero),‬

m‬
‭M‬‭= Number of rows given in the matrix.‬

co
‭Fig.‬‭2D‬‭Column Major‬

l.
‭ xample:‬‭Given‬‭an‬‭array‬‭arr[1………10][1………15]‬‭with‬‭a‬‭base‬‭value‬‭of‬‭100‬‭and‬‭the‬‭size‬
E
ai
‭of‬ ‭each‬ ‭element‬ ‭is‬ ‭1‬ ‭Byte‬ ‭in‬ ‭memory‬ ‭find‬ ‭the‬ ‭address‬ ‭of‬ ‭arr[8][6]‬ ‭with‬ ‭the‬ ‭help‬ ‭of‬
‭column-major order.‬
gm

‭Solution:‬
‭Given,‬
c@

‭Base address‬‭B‬‭= 100‬


‭Storage size of one element store in any array‬‭W‬‭= 1 Bytes‬
cb

‭Row Subset of an element whose address to be found‬‭I‬‭= 8‬


‭Column Subset of an element whose address to be found‬‭J‬‭= 6‬
.l

‭Lower Limit of row/start row index of matrix‬‭LR‬‭= 1‬


‭Lower Limit of column/start column index of matrix = 1‬
‭Number of Rows given in the matrix‬‭M‬‭= Upper Bound – Lower Bound + 1‬
fi

‭= 10 – 1 + 1‬
‭= 10‬
r‭o

‭Formula used,‬
‭Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))‬
‭Address of‬‭A[8][6]‬‭= 100 + 1 * ((6 – 1) * 10 + (8 – 1))‬
‭= 100 + 1 * ((5) * 10 + (7))‬
‭= 100 + 1 * (57)‬
‭Address of‬‭A[I][J]‬‭= 157‬

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

‭1.8 Insertion and deletion in arrays‬ ‭(Lecture 6)‬
‭Insert Operation:‬
‭1.‬‭Insert at the end:‬
‭In‬ ‭an‬ ‭unsorted‬ ‭array,‬ ‭the‬ ‭insert‬ ‭operation‬ ‭is‬ ‭faster‬ ‭as‬‭compared‬‭to‬‭a‬
‭sorted‬ ‭array‬ ‭because‬ ‭we‬ ‭don’t‬ ‭have‬ ‭to‬ ‭care‬ ‭about‬ ‭the‬‭position‬‭at‬‭which‬‭the‬
‭element is to be placed.‬

m‬
co
l.
ai
gm
c@

/‭/ C program to implement insert‬


‭// operation in an unsorted array.‬
cb

‭#include <stdio.h>‬
.l

/‭/ Inserts a key in arr[] of given capacity.‬


‭// n is current size of arr[]. This‬
‭// function returns n + 1 if insertion‬
fi

‭// is successful, else n.‬


‭int insertSorted(int arr[], int n, int key, int capacity)‬
r‭o

‭{‬

/‭/ Cannot insert more elements if n is‬


‭// already more than or equal to capacity‬
‭if (n >= capacity)‬
‭return n;‬

‭arr[n] = key;‬

‭return (n + 1);‬
‭}‬

/‭/ Driver Code‬


‭int main()‬
‭{‬
‭int arr[20] = { 12, 16, 20, 40, 50, 70 };‬
‭int capacity = sizeof(arr) / sizeof(arr[0]);‬

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

i‭nt n = 6;‬
‭int i, key = 26;‬

‭ rintf("\n Before Insertion: ");‬


p
‭for (i = 0; i < n; i++)‬
‭printf("%d ", arr[i]);‬

/‭/ Inserting key‬


‭n = insertSorted(arr, n, key, capacity);‬

‭ rintf("\n After Insertion: ");‬


p
‭for (i = 0; i < n; i++)‬
‭printf("%d ", arr[i]);‬

‭return 0;‬
‭}‬

m‬
‭2. Insert at any position:‬

co
‭Insert‬ ‭operation‬ ‭in‬ ‭an‬ ‭array‬ ‭at‬ ‭any‬ ‭position‬ ‭can‬ ‭be‬ ‭performed‬ ‭by‬
‭shifting‬ ‭elements‬ ‭to‬ ‭the‬ ‭right,‬ ‭which‬ ‭are‬ ‭on‬ ‭the‬ ‭right‬ ‭side‬ ‭of‬ ‭the‬ ‭required‬

l.
‭position.‬
ai
gm
c@
cb
.l
fi
r‭o

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

/‭/ C Program to Insert an element‬
‭// at a specific position in an Array‬

‭ include <stdio.h>‬
#
‭// Function to insert element‬
‭// at a specific position‬
‭void insertElement(int arr[], int n, int x, int pos)‬
‭{‬
‭// shift elements to the right‬
‭// which are on the right side of pos‬
‭for (int i = n - 1; i >= pos; i--)‬
‭arr[i + 1] = arr[i];‬

‭arr[pos] = x;‬
‭}‬

m‬
/‭/ Driver's code‬
‭int main()‬

co
‭{‬
‭int arr[15] = { 2, 4, 1, 8, 5 };‬
‭int n = 5;‬

l.
‭ rintf("Before insertion : ");‬
p
ai
‭for (int i = 0; i < n; i++)‬
‭printf("%d ", arr[i]);‬
gm

‭printf("\n");‬
c@

‭int x = 10, pos = 2;‬


cb

/‭/ Function call‬


‭insertElement(arr, n, x, pos);‬
‭n++;‬
.l

‭ rintf("After insertion : ");‬


p
fi

‭for (int i = 0; i < n; i++)‬


‭printf("%d ", arr[i]);‬
r‭o

‭return 0;‬
‭}‬

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

‭Deletion Operation:‬ ‭(Lecture 7)‬
‭In‬‭the‬‭delete‬‭operation,‬‭the‬‭element‬‭to‬‭be‬‭deleted‬‭is‬‭searched‬‭using‬‭the‬‭linear‬
‭search, and then the delete operation is performed followed by shifting the elements.‬

m‬
co
l.
/‭/ C program to implement delete operation in a‬
ai
‭// unsorted array‬
‭#include <stdio.h>‬
gm

/‭/ To search a key to be deleted‬


‭int findElement(int arr[], int n, int key);‬
c@

/‭/ Function to delete an element‬


cb

‭int deleteElement(int arr[], int n, int key)‬


‭{‬
‭// Find position of element to be deleted‬
.l

‭int pos = findElement(arr, n, key);‬


fi

‭if (pos == -1) {‬


‭printf("Element not found");‬
‭return n;‬
r‭o

‭}‬

/‭/ Deleting element‬


‭int i;‬
‭for (i = pos; i < n - 1; i++)‬
‭arr[i] = arr[i + 1];‬

‭return n - 1;‬
‭}‬

/‭/ Function to implement search operation‬


‭int findElement(int arr[], int n, int key)‬
‭{‬
‭int i;‬
‭for (i = 0; i < n; i++)‬
‭if (arr[i] == key)‬

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

‭return i;‬

‭return -1;‬
‭}‬

/‭/ Driver's code‬


‭int main()‬
‭{‬
‭int i;‬
‭int arr[] = { 10, 50, 30, 40, 20 };‬

i‭nt n = sizeof(arr) / sizeof(arr[0]);‬


‭int key = 30;‬

‭ rintf("Array before deletion\n");‬


p
‭for (i = 0; i < n; i++)‬

m‬
‭printf("%d ", arr[i]);‬

co
/‭/ Function call‬
‭n = deleteElement(arr, n, key);‬

l.
‭ rintf("\nArray after deletion\n");‬
p
‭for (i = 0; i < n; i++)‬
ai
‭printf("%d ", arr[i]);‬

‭return 0;‬
gm

‭}‬
c@
cb

‭1.9 Use of arrays for large number representation‬ ‭(Lecture 8)‬


.l

‭We‬ ‭can‬ ‭represent‬ ‭a‬ ‭number‬ ‭as‬ ‭a‬ ‭sequence‬ ‭of‬‭digits‬‭stored‬‭in‬‭an‬‭array‬‭of‬‭integers.‬


‭We‬ ‭can‬‭write‬‭functions‬‭to‬‭add,‬‭multiply,‬‭etc.‬‭on‬‭those‬‭arrays,‬‭and‬‭then‬‭make‬‭them‬‭as‬‭large‬
fi

‭as we want.‬

‭In‬ ‭our‬ ‭new‬ ‭representation,‬ ‭we‬ ‭have‬ ‭an‬ ‭array‬ ‭of‬ ‭"digits"‬ ‭(integers)‬ ‭in‬ ‭some‬ ‭base‬ ‭b.‬
r‭o

‭ ypically,‬ ‭b‬ ‭=‬ ‭10,‬ ‭our‬‭normal‬‭decimal‬‭number‬‭system;‬‭that‬‭makes‬‭things‬‭easy‬‭to‬‭print.‬‭The‬


T
‭0'th‬ ‭array‬ ‭element‬ ‭is‬ ‭the‬‭1's‬‭place,‬‭the‬‭#1‬‭element‬‭is‬‭the‬‭ten's‬‭place,‬‭the‬‭#2‬‭element‬‭is‬‭the‬
‭hundreds place , and so forth (really, the b0's place, the b1 place, the b2 place, etc.)‬

‭ et's‬ ‭look‬ ‭at‬‭some‬‭algorithms‬‭doing‬‭arithmetic‬‭on‬‭our‬‭new‬‭"big"‬‭integers.‬‭We'll‬‭let‬‭N‬


L
‭be‬ ‭the‬ ‭number‬ ‭of‬ ‭digits‬ ‭we‬ ‭will‬ ‭represent.‬ ‭If‬ ‭we‬ ‭need‬ ‭any‬ ‭more,‬ ‭we‬ ‭just‬ ‭increase‬ ‭N.‬ ‭Let‬
‭BASE‬‭be‬‭the‬‭base‬‭of‬‭our‬‭number‬‭system,‬‭understood‬‭to‬‭be‬‭10‬‭typically,‬‭but‬‭can‬‭be‬‭changed‬
‭if we like.‬

‭First,‬ ‭we‬ ‭need‬ ‭a‬ ‭way‬ ‭of‬ ‭making‬ ‭a‬ ‭"normal"‬ ‭integer‬ ‭into‬ ‭a‬ ‭"big"‬ ‭integer;‬ ‭we'd‬ ‭like‬ ‭a‬
f‭unction‬‭called‬‭make_int‬‭such‬‭that‬‭e.g.‬‭make_int‬‭(A,‬‭123)‬‭would‬‭put‬‭3‬‭in‬‭A[0],‬‭2‬‭in‬‭A[1],‬‭1‬‭in‬
‭A[2],‬ ‭and‬ ‭zeros‬‭in‬‭A[3..N-1].‬‭We'll‬‭do‬‭these‬‭in‬‭C‬‭rather‬‭than‬‭pseudocode‬‭because‬‭the‬‭code‬
‭works out very easily:‬

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

/‭* put the normal int n into the big int A */‬
‭void make_int (int A[], int n) {‬
‭int‬ ‭i;‬

‭/* start indexing at the 0's place */‬

‭i = 0;‬

/‭* while there is still something left to the number‬


‭* we're encoding... */‬

‭while (n) {‬

‭/* put the least significant digit of n into A[i] */‬

m‬
‭A[i++] = n % BASE;‬

co
/‭* get rid of the least significant digit,‬
‭* i.e., shift right once‬

l.
‭*/‬ ai
‭n /= BASE;‬
‭}‬
gm

‭/* fill the rest of the array up with zeros */‬


c@

‭while (i < N) A[i++] = 0;‬


‭}‬
cb

‭ ow‬‭let's‬‭look‬‭at‬‭the‬‭more‬‭general‬‭case‬‭of‬‭addition‬‭of‬‭two‬‭big‬‭integers.‬‭Here,‬‭we‬‭want‬
N
.l

‭to‬‭add‬‭two‬‭big‬‭ints‬‭in‬‭arrays‬‭called‬‭A[0..N-1]‬‭and‬‭B[0..N-1],‬‭and‬‭put‬‭the‬‭result‬‭into‬‭C[0..N-1].‬
‭We'll‬‭use‬‭the‬‭algorithm‬‭we‬‭learned‬‭in‬‭grade‬‭school:‬‭add‬‭corresponding‬‭digits,‬‭plus‬‭a‬‭"carry"‬
fi

‭generated by previous overflows.‬


‭/* C = A + B */‬
‭void add (int A[], int B[], int C[]) {‬
r‭o

‭int‬ ‭i, carry, sum;‬

‭/* no carry yet */‬

‭carry = 0;‬

‭/* go from least to most significant digit */‬

‭for (i=0; i<N; i++) {‬

/‭* the i'th digit of C is the sum of the‬


‭* i'th digits of A and B, plus any carry‬
‭*/‬
‭sum = A[i] + B[i] + carry;‬

‭/* if the sum exceeds the base, then we have a carry. */‬

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

‭if (sum >= BASE) {‬

‭carry = 1;‬

‭/* make sum fit in a digit (same as sum %= BASE) */‬

‭sum -= BASE;‬
‭} else‬
‭/* otherwise no carry */‬

‭carry = 0;‬

‭/* put the result in the sum */‬

‭C[i] = sum;‬
‭}‬

m‬
/‭* if we get to the end and still have a carry, we don't have‬

co
‭* anywhere to put it, so panic!‬
‭*/‬
‭if (carry) printf ("overflow in addition!\n");‬

l.
‭}‬ ai
gm
c@
cb
.l
fi
r‭o

LN-BCA/DSA/2024/Jan-Aug‬
‭ Masud Alam Rofi‬
‭ LCBC/IT‬

You might also like