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

Programming Paradigms-1-79

The document outlines a course on Programming Paradigms, detailing five units covering topics such as the role of programming languages, data types, subprograms, object-oriented programming, and functional programming languages. It emphasizes the importance of programming languages for productivity, portability, automation, and data analysis. Additionally, it includes references to key texts and introduces fundamental concepts in programming, such as syntax, control structures, and memory layout.

Uploaded by

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

Programming Paradigms-1-79

The document outlines a course on Programming Paradigms, detailing five units covering topics such as the role of programming languages, data types, subprograms, object-oriented programming, and functional programming languages. It emphasizes the importance of programming languages for productivity, portability, automation, and data analysis. Additionally, it includes references to key texts and introduces fundamental concepts in programming, such as syntax, control structures, and memory layout.

Uploaded by

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

Programming Paradigms

by
Dr. Amit Biswas
Assistant Professor
Dept. of Computer Science and Engineering
MNNIT Allahabad

12-02-2024 Dr. Amit Biswas Programming Paradigms


Course Outline

UNIT 1: Introduction: Role of Programming Languages: Why Programming Languages,


Towards Higher-Level Languages, Programming Paradigms, Programming Environments
Language Description: Syntactic Structure, Language Translation Issues: Programming
Language Syntax, Stages in Translation, Formal Translation Models

UNIT II: Data, Data Types, and Basic Statements: Names, Variables, Binding, Type
Checking, Scope, Scope Rules, Lifetime and Garbage Collection, Primitive Data Types, Strings,
Array Types, Associative Arrays, Record Types, Union Types, Pointers and References,
Arithmetic Expressions, Overloaded Operators, Type Conversions, Relational and Boolean
Expressions, Assignment Statements, Mixed Mode Assignments, Control Structures, Selection,
Iterations, Branching, Guarded Statements

12-02-2024 Dr. Amit Biswas Programming Paradigms 2


Course Outline
UNIT III: Subprograms and Implementations: Subprograms, Design Issues, Local
Referencing, Parameter Passing, Overloaded Methods, Generic Methods, Design Issues for
Functions, Semantics of Call and Return, Implementing Simple Subprograms, Stack and Dynamic
Local Variables, Nested Subprograms, Dynamic Scoping.

UNIT IV: Object-Orientation, Concurrency, and Event handling: Grouping of Data and
Operations — Constructs for Programming Structures, Abstraction Information Hiding, Program
Design with Modules, Defined Types, Object Oriented Programming — Concept of Object,
Inheritance, Derived Classes and Information Hiding — Templates, Semaphores, Monitors,
Message Passing, Threads, Statement Level Concurrency Exception Handling
(Using C++ and Java as Example Language).

UNIT V: Functional and Logic Programming Languages: Fundamentals of Functional


Programming Languages, Programming with ML, Introduction to Logic and Logic Programming
— Introduction to Programming with HASKELL, SCHEME& SCALA
12-02-2024 Dr. Amit Biswas Programming Paradigms 3
Reference Books

1. Programming Languages: Design and Implementations by Terrance Pratt,


Marvin V. Zelkowitz, T.V.Gopa1, Fourth ed., Prentice Hall.
2. Programming in HASKELL by Graham Hutton , Cambridge University Press.
3. The Scheme Programming language by R.Kent , Dybvig.
4. Programming in SCALA by Bill Venners & Martin Odersky , ARTIMA Press,
Mount View, California.
5. Concepts of Programming Languages by Robert W. Sebesta, 10th Ed.,Pearson.
6. Programming Language Design Concept by David A. Watt, Willey India.

12-02-2024 Dr. Amit Biswas Programming Paradigms 4


Introduction

Programming: It refers to a technological process for telling a computer which tasks


to perform in order to solve problems. In other words, programming can be
considered as a collaboration between humans and computers, in which humans
create instructions for a computer to solve problems.

12-02-2024 Dr. Amit Biswas Programming Paradigms 5


Introduction

Importance of Programming Languages

➢ Increased Productivity: Programming languages provide a set of abstractions that


allow developers to write code more quickly and efficiently.
➢ Portability: Programs written in a high-level programming language can run on
many different operating systems and platforms.
➢ Automation: Programming languages allow us to automate tedious and repetitive
tasks. From scheduling automated backups to controlling smart home devices,
programming enables us to simplify our lives and save time.
➢ Data Analysis: In the era of big data, programming languages provide the tools and
frameworks needed to analyze vast amounts of information. From data visualization
to machine learning algorithms, programming languages empower us to gain insights
and make data-driven decisions.

12-02-2024 Dr. Amit Biswas Programming Paradigms 6


Introduction
o Problem solving is the process of defining a problem, identifying its root
cause, prioritizing and selecting potential solutions, and implementing the
chosen solution.
o In Computer Science, we try to solve various real-life problems
programmatically.

Average Calculation Travelling Salesman Problem Prime Number

12-02-2024 Dr. Amit Biswas Programming Paradigms 7


Introduction

Some of the key features of programming languages –

1. Syntax: The specific rules and structure are used to write code in a
programming language.

2. Data Types: The type of values that can be stored in a program, such as
numbers, strings, and booleans.

3. Variables: Named memory locations that can store values.

4. Operators: Symbols used to perform operations on values, such as addition,


subtraction, and comparison.

12-02-2024 Dr. Amit Biswas Programming Paradigms 8


Introduction

5. Control Structures: Statements used to control the flow of a program, such as


if-else statements, loops, and function calls.

6.Libraries: Collections of pre-written code that can be used to perform common


tasks and speed up development.

7. Paradigms: The programming style or philosophy used in the language, such


as procedural, object-oriented, or functional.

Some popular programming languages are C, Python, Java, C++, JavaScript, and
Ruby. Each language has its own strengths and weaknesses and is suited for
different types of projects.

12-02-2024 Dr. Amit Biswas Programming Paradigms 9


Introduction

The programming languages are broadly classified into three types,

➢ Low-level language
➢ Middle-level language
➢ High-level language

Low-level language
o It is machine readable.
o It works based on the binary number 0’s and 1’s.
o The processor runs low-level programs directly without the need of a
compiler or interpreter so the program written in low-level language can be
run very fast.

12-02-2024 Dr. Amit Biswas Programming Paradigms 10


Introduction

Middle-level language

o Assembly language is designed for specific processors.


o It represents the set of instructions in a symbolic and human-understandable
form.
o It uses an assembler to convert the assembly language to machine language.

High-level language

o High-level programming language (HLL) is designed for developing user-


friendly software programs and websites.
o This programming language requires a compiler or interpreter to translate the
program into machine language (execute the program).

12-02-2024 Dr. Amit Biswas Programming Paradigms 11


Von Neumann Architecture

✓ This computer architecture design consists of a Control Unit, Arithmetic and


Logic Unit (ALU), Memory Unit, and Inputs/Outputs.

12-02-2024 Dr. Amit Biswas Programming Paradigms 12


Execution Flow of a C Program

12-02-2024 Dr. Amit Biswas Programming Paradigms 13


Memory Layout of a Process

12-02-2024 Dr. Amit Biswas Programming Paradigms 14


Memory Layout of a Process

Text / Code Segment: It contains program instructions for execution. It is a read only section
of a program and cannot be changed during the execution.

Data Segment: This segment is divided in to two sub-segments i.e., initialized data segment
and uninitialized data segment. Initialized data segment contains global and static variables
that are explicitly initialized and uninitialized data segment contains global and static
variables that are not initialized.

Heap Segment: Memory allocated dynamically during the program execution occurs in the
heap section.

Stack Segment: It contains activation records / stack frames. Stack frames are created for
each function call and pushed in the stack. All the local variables of function are stored in its
stack frame. A stack frame is popped from stack when corresponding function is returned.

12-02-2024 Dr. Amit Biswas Programming Paradigms 15


Memory Layout of a Process

#include <stdio.h>
int x = 10; int main()
float y; {
char a = 'A';
void fun() static int b;
{ int c;
int p = 30; y = 20.5;
int *ptr = (int*) malloc(sizeof(int)*2); printf("a = %c, x = %d, y = %f, b =
*ptr = 50, *(ptr+1) = 60; %d",a,x,y,b);
static float q = 40.9; fun();
printf("\n p = %d, q = %f, %d, return 0;
%d",p,q, *ptr, *(ptr+1)); }
free(ptr);
}

12-02-2024 Dr. Amit Biswas Programming Paradigms 16


Memory Representation

RAM is split into partitions. Each


partition consists of an address and
its contents (both in binary form).

12-02-2024 Dr. Amit Biswas Programming Paradigms 17


Instruction Execution
Fetch: The processor
copies the instruction
data captured from the
RAM.

Decode: Decoded
captured data is
transferred to the unit for
execution.

Execute: Instruction is
finally executed and
result is stored in the
register or RAM.
12-02-2024 Dr. Amit Biswas Programming Paradigms 18
Communication

Wants to communicate

12-02-2024 Dr. Amit Biswas Programming Paradigms 19


Communication

A compiler is a computer program that translates a programming language's


source code into the machine code.

12-02-2024 Dr. Amit Biswas Programming Paradigms 20


Phase of Compiler

12-02-2024 Dr. Amit Biswas Programming Paradigms 21


Phase of Compiler

a = b + c * 60;

Symbol Table
1 a float Lexical Analyzer
2 b float
3 c float
<id, 1> = <id, 2> + <id, 3> * 60;

12-02-2024 Dr. Amit Biswas Programming Paradigms 22


Phase of Compiler

<id, 1> = <id, 2> + <id, 3> * 60;

Syntax Analyzer

12-02-2024 Dr. Amit Biswas Programming Paradigms 23


Phase of Compiler

Semantic Analyzer

12-02-2024 Dr. Amit Biswas Programming Paradigms 24


Phase of Compiler

temp1 = int_to_float(60)
Intermediate code temp2 = id3 * temp1
generation temp3 = id2 + temp2
id1 = temp3

12-02-2024 Dr. Amit Biswas Programming Paradigms 25


Phase of Compiler

temp1 = int_to_float(60)
temp2 = id3 * temp1 temp1 = id3 * 60.0
Code optimization id1 = id2 + temp1
Temp3 = id2 + temp2
id1 = temp3

12-02-2024 Dr. Amit Biswas Programming Paradigms 26


Phase of Compiler

MOV id3, R2
temp1 = id3 * 60.0 MUL #60.0, R2
Target code generator
id1 = id2 + temp1 MOV id2, R1
ADD R2, R1
MOV R1, id1

12-02-2024 Dr. Amit Biswas Programming Paradigms 27


Token

Token: It is a sequence of characters that are treated as a unit as it cannot be


further broken down. There are seven types of tokens.

12-02-2024 Dr. Amit Biswas Programming Paradigms 28


Token

Keyword: int, return

Identifier: main, A, B, sum, printf int main()


{ int A = 10, B = 20, sum = 0;
Separator: (, ), {, , , ; } sum = A + B;
printf(“ Addition of A and B is %d”, sum);
Operator: =, + return 0;
}
Constant: 10, 20, 0,

Literal: “ Addition of A and B is %d”

12-02-2024 Dr. Amit Biswas Programming Paradigms 29


Grammar

Grammar in theory of computation is a finite set of formal rules that are generating
syntactically correct sentences. Formally, it is defined as four tuples –

G=(V,T,P,S)

o G is a grammar and it is used to generate the strings of a language.


o T is a finite set of terminal symbols. It is denoted by lower case letters.
o V is a finite set of non-terminal symbols. It is denoted by capital letters.
o P is a set of production rules, which is used for replacing non-terminal symbols (on the left
side of production) in a string with other terminals (on the right side of production).
o S is the start symbol used to derive the string.

12-02-2024 Dr. Amit Biswas Programming Paradigms 30


Grammar

G=(V,T,P,S) where

V = { S , A} ⇒ Non-Terminal symbols,
T={a,b} ⇒ Terminal symbols,
Production rules P = { S → aA, A → aA, A → bA , A→ ε },
S={S} ⇒ Start symbol.
a, b
L = {a, aa, ab, aba, aab, aaa, abb ……..}
a q2
q0 a, b
b
q1

12-02-2024 Dr. Amit Biswas Programming Paradigms 31


Grammar
According to Chomsky hierarchy, grammar is divided into 4 types as follows:
o Type 0 is known as unrestricted grammar.
o Type 1 is known as context-sensitive grammar.
o Type 2 is known as a context-free grammar.
o Type 3 Regular Grammar.

12-02-2024 Dr. Amit Biswas Programming Paradigms 32


Parse Tree

A parse tree or derivation tree is a tree that represents the syntactic structure of
a string according to some context-free grammar.

o The root of a parse tree is the start symbol.


o All leaf nodes need to be terminals.
o All interior nodes need to be non-terminals.
o In-order traversal gives the original input string.

12-02-2024 Dr. Amit Biswas Programming Paradigms 33


Parse Tree

Production rules P = { E → E + E, E → E * E, E → E = E, E→ id},


Start symbol S = { E }

x+y*z

12-02-2024 Dr. Amit Biswas Programming Paradigms 34


Data

Data: Data can be defined as a representation of facts or a collection of discrete or


continuous values that convey information and are used for analysis or a survey.

Information: Information refers to data that has been processed, organized, or


presented in a structured or meaningful way to the recipient.

12-02-2024 Dr. Amit Biswas Programming Paradigms 35


Data Type

A computer program that calculates the total payable amount of a product. Here, the
programming language should know the type of data to perform the operation on it.

12-02-2024 Dr. Amit Biswas Programming Paradigms 36


Data Type

Data Type: A data type is a homogeneous collection of values, effectively


presented, equipped with a set of operations which manipulate these values.

Data types are present in programming languages for at least three different reasons:

o At the design level, as support for the conceptual organization;


o At the program level, as support for correctness;
o At the translation level, as support for the implementation.

12-02-2024 Dr. Amit Biswas Programming Paradigms 37


Data Type

12-02-2024 Dr. Amit Biswas Programming Paradigms 38


Primitive Data Type

Primitive or Built-in Data types are those data types that are pre-defined by the
programming language. Most languages have native data types that are created for
ease of execution of data.

12-02-2024 Dr. Amit Biswas Programming Paradigms 39


Boolean

A boolean is a data type with two possible values: true (1) or false (0). The two
values help represent truth conditions found in logic control structures. The name
comes from a branch of mathematics called Boolean algebra, named after George
Bool.

Logical and, or, not etc. operations can be performed on Boolean data type.

12-02-2024 Dr. Amit Biswas Programming Paradigms 40


Text Type

Character Data Type: The char type is used to store single characters (letters,
digits, symbols, etc...). We have to remember, when values of variables are stored in
the computer's memory, they must ultimately be stored in terms of only 1's and 0’s.

Encoding refers to how something (like a char) is converted to its binary


representation in the computer's memory.

A character set is a collection of characters along with an encoding scheme to


convert them to their binary representations.

12-02-2024 Dr. Amit Biswas Programming Paradigms 41


Text Type

There are two popular character sets used frequently in programming languages:

ASCII
o Originally required 7 bits of memory to store, but modern extensions of
ASCII require 1 full byte (8 bits) of storage space.
o Has a base set of 128 characters, including all of the English letters (upper
and lower case), digits 0-9, some common symbols, and several unprintable
characters like tab, return, backspace, etc... The extended versions of ASCII
add several more printable characters.

12-02-2024 Dr. Amit Biswas Programming Paradigms 42


Text Type

Unicode

o Requires 2 bytes (16 bits) of storage space.


o Consists of 65,536 characters, including characters and symbols from many
different languages.
o ASCII is a subset of Unicode. That is to say, if the letter 'A', for example, is
encoded to a decimal value of 65 in ASCII, then the Unicode version of 'A'
will also be encoded to the same value.
o Java uses Unicode.

12-02-2024 Dr. Amit Biswas Programming Paradigms 43


ASCII

12-02-2024 Dr. Amit Biswas Programming Paradigms 44


Numeric Data

Numeric data types are types of data that consist of numbers, which can be computed
mathematically with various standard operators such as add, minus, multiply, divide,
and more. Examples of numeric data types are examination marks, height, weight,
the number of students in a class, share values, price of goods, monthly bills, fees,
and others.

Numeric data types can be divided into the following categories –


o Integer data type
o Float data type

12-02-2024 Dr. Amit Biswas Programming Paradigms 45


Number Representation

There are three ways to represent the number. They are –

o Signed Magnitude Representation


o 1’s Complement Representation
o 2’s Complement Representation

12-02-2024 Dr. Amit Biswas Programming Paradigms 46


Nothing

A nothing data type is a feature of some programming languages which allow the
setting of a special value to indicate a missing or uninitialized value or absence of
data. For example – null or none.

12-02-2024 Dr. Amit Biswas Programming Paradigms 47


Non-primitive Data Type in C

Derived Data Type: The data types that are derived from the primitive or built-in
datatypes are referred to as Derived Data Types. For example – function, array,
pointer etc.

User Defined Data Type: The data types that are defined by the user are known
as user-defined data types. For example: class, structure, union, Enumeration etc.

12-02-2024 Dr. Amit Biswas Programming Paradigms 48


Primitive Data Type in C

12-02-2024 Dr. Amit Biswas Programming Paradigms 49


Primitive Data Type in Python

12-02-2024 Dr. Amit Biswas Programming Paradigms 50


Primitive Data Type in Python

12-02-2024 Dr. Amit Biswas Programming Paradigms 51


Storage Class

The Storage Class is used to describe the features of a variable.

Storage Class in C
A storage class is used to describe the following things:
o The variable scope.
o The location where the variable will be stored.
o The initial value of a variable.
o A lifetime of a variable.
o Who can access a variable?

12-02-2024 Dr. Amit Biswas Programming Paradigms 52


Auto Storage Class

The features of the auto storage class in C:

Storage: Main memory.

Default initial value: An unpredictable value, which is often called a garbage value.

Scope: Local to the block in which the variable is defined.

Life: Till the control remains within the block in which the variable is defined.

12-02-2024 Dr. Amit Biswas Programming Paradigms 53


Register Storage Class

The features of the register storage class in C:

Storage: CPU registers.

Default initial value: Garbage value.

Scope: Local to the block in which the variable is defined.

Life: Till the control remains within the block in which the variable is defined.

12-02-2024 Dr. Amit Biswas Programming Paradigms 54


Static Storage Class

The features of the static storage class in C:

Storage: Main memory.

Default initial value: Zero.

Scope: Local to the block in which the variable is defined.

Life: Value of the variable persists between different function calls.

12-02-2024 Dr. Amit Biswas Programming Paradigms 55


External Storage Class

The features of the external storage class in C:

Storage: Main memory.

Default initial value: Zero.

Scope: Global.

Life: As long as the program’s execution doesn’t come to an end.

12-02-2024 Dr. Amit Biswas Programming Paradigms 56


Scope and Lifetime

The scope of a variable is the region in which it can be used. Beyond that area, we
cannot use that variable. A variable may be in the memory but may not be accessible
though. So, the area of our program where we can actually access our entity (variable
in this case) is the scope of that variable.

o Global scope: When variable is defined outside all functions. It is then available to
all the functions as well as all the blocks of the program.

o Local scope: When variable is defined inside a function or a block, then it is locally
accessible within that function or block.

Lifetime of any variable is the time for which the particular variable outlives in
memory during running of the program.
12-02-2024 Dr. Amit Biswas Programming Paradigms 57
Scope
#include<stdio.h>

int main()
{
int x = 100, y = 50;
if(x = = y){
int z = 1;
}
else {
int z = 0;
}
printf(“%d”, z);
return 0;
}
12-02-2024 Dr. Amit Biswas Programming Paradigms 58
Example

12-02-2024 Dr. Amit Biswas Programming Paradigms 59


Binding

In programming languages, binding refers to the association of information or


attributes with a variable or entity. For example –

int a = 10;
float fun()
{
float b = 20.5;
return b*b;
}

12-02-2024 Dr. Amit Biswas Programming Paradigms 60


Binding

Static Binding: When the compiler acknowledges all the information required to
call a function or all the values of the variables during compile time, it is called
static binding. As all the required information is known before runtime, it increases
the program efficiency and it also enhances the speed of execution of a program.
Static Binding makes a program very efficient, but it declines the program’s
flexibility, as ‘values of the variable’ and ‘function calling’ are predefined in the
program.

12-02-2024 Dr. Amit Biswas Programming Paradigms 61


Binding

Dynamic Binding: Calling a function or assigning a value to a variable, at run-time


is called dynamic binding. Dynamic binding can be associated with run time
‘polymorphism’ and ‘inheritance’ in OOP. Dynamic binding makes the execution of
a program flexible as it can be decided, what value should be assigned to the
variable and which function should be called, at the time of program execution. But
as this information is provided at run time it makes the execution slower as
compared to static binding.

12-02-2024 Dr. Amit Biswas Programming Paradigms 62


Type Checking

Type checking is the process of verifying the compatibility of data types in a


program, ensuring that operations are performed on data of the appropriate types. It
is typically performed during compilation in statically typed languages or at runtime
in dynamically typed languages.

➢ In statically typed languages like C, C++, and Java, the data types of variables are
explicitly defined during declaration. The compiler performs type checking
during the compilation phase to detect type errors, such as assigning incompatible
data types or using incompatible operations.

➢ In dynamically typed languages like Python and JavaScript, variables can change
their data type during runtime. Type checking occurs at runtime, and errors
related to type incompatibility are identified when the program is executed.

12-02-2024 Dr. Amit Biswas Programming Paradigms 63


Type Conversions

Type conversion, also known as type casting, is the process of converting data from
one data type to another. It allows developers to perform operations between
different data types or to store data of one type into a variable of a different type.
Data type conversions are two types –

o Implicit Type Conversion


o Explicit Type Conversion

12-02-2024 Dr. Amit Biswas Programming Paradigms 64


Importance of Type checking and Conversions

1.Program Correctness: Type checking ensures that operations are performed on


compatible data types, reducing the risk of runtime errors and improving program
correctness.
2.Data Consistency: Type conversions allow data to be represented in different
formats while ensuring that operations between different data types can be
performed without errors.
3.Memory Optimization: Type conversions can be used to optimize memory usage
by converting data to the most appropriate data type for specific calculations or
operations.
4.Flexibility: Type conversions enable developers to work with data in different
formats, facilitating tasks like input/output processing and data formatting.
5.Interoperability: Type conversions are essential when working with external data
sources or interacting with other systems that may use different data representations.

12-02-2024 Dr. Amit Biswas Programming Paradigms 65


Array

An array is a linear data structure and simply a collection of data items of the same
data type, which you can access using a common name. In an array, data items are
stored in contiguous memory locations.

12-02-2024 Dr. Amit Biswas Programming Paradigms 66


Array

Arrays can be –
Single dimensional
Multidimensional

To create an array, define the data type (like int) and specify the name of the array
followed by square brackets [].

12-02-2024 Dr. Amit Biswas Programming Paradigms 67


Array

int arr[5];
for (int i = 0; i < 5; i++)
{
arr[i] = value;
}

12-02-2024 Dr. Amit Biswas Programming Paradigms 68


Array

12-02-2024 Dr. Amit Biswas Programming Paradigms 69


Array

For 1D array

Address of Arr[k] = B + W*(k-LI)

Where, k is the index of the element whose address needs to be found


B is the base address
W is the number of bytes required to store an element
LI is the lower index or starting index of the array

12-02-2024 Dr. Amit Biswas Programming Paradigms 70


Array

➢ Given that the base address of an array A[5:20] is 1020 and the size of
each element is 2 bytes in the memory, find the address of A[17].

12-02-2024 Dr. Amit Biswas Programming Paradigms 71


Array

Syntax of 2D array declaration:


array_name[No. of row] [No. of column];

int arr[2][3] = { {1, 4, 2}, {3, 6, 8} };

12-02-2024 Dr. Amit Biswas Programming Paradigms 72


Array

12-02-2024 Dr. Amit Biswas Programming Paradigms 73


Array

For 2D array (Row major):

Address of arr[I][J] = B + W * ((I – LR) * N + (J – LC))


I = Row number of the element whose address is to be found,
J = Column number of the element whose address is to be found,
B = Base address,
W = Storage size of one element stored in an array(in byte),
LR = Lower index of row/start row index of the matrix
LC = Lower index of column/start column index of the matrix
N = Number of columns given in the matrix.

12-02-2024 Dr. Amit Biswas Programming Paradigms 74


Array

➢ Given that an array, Arr[1:10][1:15] with base value 100 and the size of each
element is 2 Byte in memory. Find the address of Arr[8][6] with the help of
row-major order.
12-02-2024 Dr. Amit Biswas Programming Paradigms 75
Array

For 2D array (Row major):

Address of arr[I][J] = B + W * ((J – LC) * M + (I – LR) )


I = Row number of the element whose address is to be found,
J = Column number of the element whose address is to be found,
B = Base address,
W = Storage size of one element stored in an array(in byte),
LR = Lower index of row/start row index of the matrix
LC = Lower index of column/start column index of the matrix
M = Number of rows given in the matrix.

12-02-2024 Dr. Amit Biswas Programming Paradigms 76


Array

➢ Given that an array, Arr[1:10][1:15] with base value 100 and the size of each
element is 2 Byte in memory. Find the address of Arr[8][6] with the help of
column-major order.

12-02-2024 Dr. Amit Biswas Programming Paradigms 77


Array

Syntax of 3D array declaration:


array_name[No. of 2D array][No. of row] [No. of column];

12-02-2024 Dr. Amit Biswas Programming Paradigms 78


Array
int arr[2][3][4] = {
{{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
{{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}
};

• C program to check whether a given matrix is a sparse matrix or not.

• C program to print the upper triangular matrix.

12-02-2024 Dr. Amit Biswas Programming Paradigms 79

You might also like