0% found this document useful (0 votes)
24 views28 pages

CH 2 Arrays and Structure

The document provides an overview of arrays and structures in programming, detailing their definitions, types, memory representation, and address calculation methods. It also explains the concept of structures for storing different data types and introduces polynomial representation and evaluation using arrays and structures. Key algorithms for polynomial addition and evaluation are included to illustrate practical applications.

Uploaded by

Hari Kalu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views28 pages

CH 2 Arrays and Structure

The document provides an overview of arrays and structures in programming, detailing their definitions, types, memory representation, and address calculation methods. It also explains the concept of structures for storing different data types and introduces polynomial representation and evaluation using arrays and structures. Key algorithms for polynomial addition and evaluation are included to illustrate practical applications.

Uploaded by

Hari Kalu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Ch 2.

Arrays and
Structure
Introduction to Arrays
• Array is a collection of items of the same variable type that are
stored at contiguous memory locations. It is one of the most popular
and simple data structures used in programming.
• Basic terminologies of Array
• Array Index: In an array, elements are identified by their indexes.
Array index starts from 0.

• Array element: Elements are items stored in an array and can be


accessed by their index.

• Array Length: The length of an array is determined by the number


of elements it can contain.
Memory representation of Array
Declaration of Array

• // This array will store integer type element


• int arr[5];

• // This array will store char type element


• char arr[10];

• // This array will store float type element


• float arr[20];
Initialization of Array

• int arr[] = { 1, 2, 3, 4, 5 };
• char arr[5] = { 'a', 'b', 'c', 'd', 'e' };
• float arr[10] = { 1.4, 2.0, 24, 5.0, 0.0 };
Types of Arrays

• 1. One-dimensional Array(1-D Array): You can imagine


a 1d array as a row, where elements are stored one after
another.
• 2. Multi-dimensional Array: A multi-dimensional array is
an array with more than one dimension. We can use
multidimensional array to store complex data in the form
of tables, etc. We can have 2-D arrays, 3-D arrays, 4-D
arrays and so on.
Array Representation
• 1. Row-major representation: In this representation, the
elements are arranged row-wise, i.e., elements of row 0
are stored first, elements of row 1 are stored next and so
on.
• 2.Column-major representation: Elements are arranged
column-wise, i.e., elements of column 0 are stored first,
elements of column 1 are stored next and so on.
Address Calculation
• For an array int a[r][c]; where r = number of rows and c = number of
columns, the location of an element a[i][j] in the array can be calculated
as:
i. Row Major Representation
address of a[i][j] = Base-address+i*c*elementsize+j*elementsize
= Base-address + (i*c+j) elementsize
ii. Column Major Representation
address of a[i][j] = Base-address+j*r*elementsize + i*elementsize
= Base-address+(j*r + i) elementsize
• Example 1
• Consider the integer array a[4][3] shown above. The element
a[1][2] is 6. Its address is calculated as shown below. Here, r=4,
c=3, i=1, j=2. Let us assume base address = 1000.
• In Row major representation,
• address of a[1][2]= 1000+(1*3+2) *sizeof (int)
• = 1000+5*2
• =1010
• In Column major representation,
• address of a[1][2] = 1000+(2*4+1) *sizeof (int)
• = 1000+9*2
• = 1018
General Address Calculation
• We can generalize the above for an 'n' dimensional array
stored in row-major order.
• int a[s1] [s2]. . . .[sn];
• To access element a [il] [i2]... [in]; we first have to pass
through i1 arrays of size s2 s3......sn. then through i2
arrays of size s3*s4*… sn and so forth. Finally we have to
pass 'in' elements.
• address of a[i1][i2]...[in] =baseaddress + i1*s2* +...+ sn
*esize +i2 *s3…. sn
* esize+…..+in * esize
• = base address + [il*s2*...
*sn+i2*s3*...sn + ... + in]
*esize
• Example 2:
• Given an integer array inta[5][4][3] whose base address is
1000, calculate the address of element a[2] [3] [1].
• SolutionGiven s1=5, s2=4, s3=3, i1=2, i2-3, i3=1
• address of a[2] [3] [1] =1000+ (i1*s2*s3+i2* s3
+i3)*sizeof(int)
• = 1000+ (2*4*3+3*3+1) *sizeof(int)
• = 1068
Q1. Consider the float array f[5][7]. Calculate the address of
f[3][4] in the row and column major representation. Use
base address 100.
Q2. Calculate the address of m[4][2][1] for an integer array
[5][3][4] in row and column major representation.
Structures
• An array allows us to store a group of elements of the
same data type together. In many cases, we need to store
data items of different types as a group. A structure allows
to store a set of elements of different types as a group.
• Example:
struct student
{
char name[20]
int rollno;
float percentage;
};
• We can create variables of this structure:
• struct student stud1, stud2;
• name Rollno
percentage
• Stud1
2000 2020
2022
stud2
5010 5030 5032
• Structure members can be accessed using the structure member
operator (.) also called the dot operator.
stud1.name
stud1.rollno
stud1.percentage
• They can be read and displayed using the scanf and printf functions.
• scanf(“%s%d%d", stud1.name, &stud1.rollno, &stud1.percentage);
• printf(“%s%d%d", stud1.name, stud1.rollno, stud1.percentage);
• Self referential structure
• In this type of structures, the structure contains a pointer to itself. These
type of structures are widely used in data structures like linked lists,
trees etc.
• Example
• struct node{
• int info;
• struct node *next; /* pointer to struct node*/
• };
• In this example, the structure node contains a pointer to
itself. We can see its use in a linked list. A linked list is a
collection of nodes each linked to one another. Each node
of the list stores a data element along with an address to
the next node. Hence, the node structure is a self-
referential structure.
Polynomial Representation using
Array
• A polynomial is a mathematical expression of more than two algebraic
terms, especially the sum of several terms that contain different powers of
one or more variables.
• For example: 7x4+3x²-10
• Polynomials are frequently used in several applications. Various operations
like addition. evaluation, finding roots etc. are carried out on polynomials.
In order to perform operations, the polynomial has to be represented in
some way. Since a polynomial contains one or more terms, we can use an
array to represent the polynomial. However, each term contains a
coefficient and an éxponent. Hence, a two dimensional array can be used as
shown.
• int p[3] [2];
• [0] [1]
7 4

3 2

-10 0
• Here, p[0][0] and p[0][1] are the coefficient and exponent respectively
of the first term.
• Another way to represent the polynomial is by using a structure. Each
term contains two parts coefficient and exponent. The structure for
term will be:
• struct poly{
• int coeff;
• int exp;
• };
• The polynomial contains more than one term. Hence, we
make an array of this structure.
• Struct poly p[3];
coeff exp
p[0] 7 4

p[1] 3 2

p[2] -10 0

• Here, p[0], p[1] and p[2] are the three terms. To access
the coefficient and exponent of each term, we use the
dot(.) operator.
• For example: p[0].coeff, p[0].exp and so on.
Polynomial Evaluation
• Polynomial evaluation means finding the value of the
polynomial by evaluating each term and finding the sum.
For example, the value of polynomial 7x4 + 3x²-10 if x=2 is
calculated as:
• 7*2+3*22-10=114.
• The algorithm to evaluate a polynomial in one variable is
given below:
Algorithm
1. Start
2. Accept n terms of polynomial p
3. Accept value of variable x
4. i=0, sum=0
5. while (i<n)
term_value = p[i].coeff* power(x,p[i].exponent)
sum = sum + term_value
i=i+1
6. Display sum
7.Stop
Addition of Polynomials
• Polynomial addition is one common operation performed
on polynomials. Two polynomials are added by adding
coefficients of terms having the same coefficient. For
example, addition of
• 7x4+3x²-10 and 5x3+6x²+4 is 7x4+5x3+9x²-6
• To add two polynomials, we have to compare the
exponents and add coefficients only if the exponents are
equal.
• Algorithm
1. Start
2. Accept m terms of polynomial p1 and n terms of p2
3. i=0, j=0, k=0;
4. while i<m and j<n
if(exponent of pl[i] = exponent of p2[j])
Add the two terms and store in p3[k]
Increment i, j, k
else
if(exponent of pl [i]> exponent of p2[j])
Copy term from p1[i] to p3[k]
Increment i, k
else
Copy term from p2[j] to p3[k]
Increment j, k
5. If (i<m)
Copy remaining terms from pl[i] to p3[k]
6. If (j<n)
Copy remaining terms from p2[j] to p3[k]
7. Display polynomial p3
8. Stop

You might also like