Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

UNIT-II

CHAPTER
Data Structures

ARRAYS

Syllabus
 Introduction to data structure, primitive and non-primitive data structure, linear and non-linear
structure, static and dynamic data structure.
 Arrays : One and two Dimensional arrays : Sequential allocation and address calculation;
 One dimensional array : Traversal, Searching (Linear, Binary Search), Insertion of an element in
an array, deletion of an element from an array, Sorting (Insertion, Se lection)
 Two-dimensional arrays : Traversal Finding suml difference of two NxM arrays containing
numeric values, Interchanging Row and Column elements in a two dimensional array;

TOPIC-1
Data Structures and One-dimensional Arrays
TOPIC - 1
Data Structures and One-dimensional
Arrays
Quick Review TOPIC - 2
1. Introduction
A data structure is a named group of data of different data types which can be processed as a single unit. In other
words, a data structure is a logical method of representing data in memory using simple and complex data types
provided by the language.
Organized collection of data is called data structure.
The data structure can be classified into following two types :
A. Simple Data Structure: These are build from fundamental data types i.e., int, float, char etc.
Example: Array, Structure
B. Compound Data Structure: Simple data structure can be combined in various ways to form more complex
structure called compound data structures. It is of two types:
(i) Linear Data Structure: A data structure is said to be linear if its elements or items are stored sequentially.
Example: Stack, Queue, Linked List.
(ii) Non-Linear Data Structure: These are multilevel data structure, in which elements are not stored
sequentially. Example: Tree, Graph.

Data Structure

Simple Data Compound Data


Structure Structure

Array Structure Linear Non Linear

Stack Queue Linked List Tree Graph

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
LEARNING CENTERS FOR CLASSES VI -XII EduSuccess : Delhi's #1 Tuition Center for Computer Science, Class — 12

2. Arrays
It is a collection of similar type of data elements. Its elements are allocated in continuous memory. Arrays are of
different types:
(i) One-dimensional array
(ii) Two-dimensional array
(i) One Dimensional Array : The simplest form of an array is one-dimensional array. The array itself is given a
name and its elements are referred to by their subscripts. The notation of array can be:
Array_name [lower bound L, upper bound U]
In C+ +, its like
Arrayname [size ]
where, size specifies the number of elements in the array and the subscript (index) value ranges
from 0 through size-1.
Length of the array = U - L + 1
Address of element with subscript I = B + S*(I-L)
where,
B : Base Address (Address of the very first element)
S : Size of an array element
L : Lower Bound of array
Basic Operations on 1-D Array :
1. Traversal : Visiting each element (from start to end) is called traversal.
2. Searching : Searching in a 1-D array can be done in following two ways: (i) Linear Search, (ii) Binary search
Linear Search : In linear search (sequential search), each element of the array is compared with given item
to be searched for, one by one. This searching te chnique can work for both unsorted and sorted array.
(ii) Binary Search : The binary search technique can work only for sorted arrays. To search an element say
ITEM, in a sorted array (suppose in ascending order), the ITEM is compared with the middle element.
If the ITEM is greater than the middle element, second half of the array becomes new segment to be
scanned, when an ITEM is less than middle element, first half becomes the new segment to be scanned.
The same process is repeated until the ITEM is found or the segment is reduced to the single element
and if still ITEM is not found.
3. Insertion : Insertion of new element can be done at a specific position and if the array is sorted, insertion of
the element will be at appropriate place. Insertion is not possible, if the array is already full which is called
"OVERFLOW" but replacement of an existing element is possible.
4. Deletion : Deletion of an element means its removal from the array. Deletion may not be possible if the
element does not exist. Deletion can be done in any one of the following ways:
0 Deletion of an element from a specific position.
(ii) Deletion of an element from an unsorted array.
(iii) Deletion of an element from a sorted array.
5. Sorting : Sorting means arranging the elements in some specific order, i.e., either ascending or descending
order. The various sorting techniques available are :
(i) Insertion Sort : Initially, the first element is assumed to be sorted. In the first pass, 2nd element is
inserted into its proper place in the sorted part of the array. Similarly, in the next pass, the 3rd element is
placed and so on. Given below is the insertion sort for ascending order.
Array at beginning : 42 29 74 11 65 58
After Pass 1 29 42 74 11 65 58
After pass 2 29 42 74 11 65 58
After pass 3 11 29 42 74 65 58
After pass 4 11 29 42 65 74 58
After pass 5 11 29 42 58 65 74
Sorted Array 11 29 42 58 65 74

// function for Insertion Sort


void InsertionSort (int a[], int n)
{ int i j, temp; ,

for (i=1; i<n; i + + )


{ temp=a [ ;
j=-1;
while (temp<a [ j] ) && j>=0)
{a [j +1] =a [j ;

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
AR RAYS LEARNING CENTERS FOR CLASSES VI -XII

a [j+1] =temp;
cout«"\n After pass" «i;
for (k=0; k<n; k++)
cout«a[k];
}

(ii) Selection Sort : The element with the smallest value (if found) is swapped with the first element. As a
result of this interchange, the smallest element is placed in the 1st position of the array. In the second
pass, second smallest element is searched and swapped with second element and so on. Given below is
the selection sort for ascending order.
Array at beginning : 42 29 74 11 65 58
After Pass 1 11 29 74 42 65 58
After pass 2 11 29 74 42 65 58
After pass 3 11 29 42 74 65 58
After pass 4 11 29 42 58 65 74
After pass 5 11 29 42 58 65 74
Sorted Array 11 29 42 58 65 74
//function for Selection Sort
void SelectionSort (int a[], int n)
{ int i, small, pos, temp;
for (i = 0; i<n; i++)
small=a[i];
pos=i;
for (j=i+1; j<n; j++)
{ if (a[j]<small)
small=a [j ;
Pos=j ; }
}
temp = a[i];
a[i]=a[pos];
a [pos] =temp;
cout«"\n After Pass" «i+1;
for (j=0; j<n; j++)
cout«a[j];
}

(iii) Bubble Sort : In this technique, two adjacent values are compared and they are exchanged, if not in
proper order. In every pass, the larger element settles at its appropriate position in the bottom. Given
below is the bubble sort for ascending order.
Array at beginning: 42 29 74 11 65 58
After Pass 1 29 42 11 65 58 74
After pass 2 29 11 42 58 65 74
After pass 3 11 29 42 58 65 74
After pass 4 11 29 42 58 65 74
After pass 5 11 29 42 58 65 74
Sorted Array 11 29 42 58 65 74
//function for Bubble sort
void Bubblesort (int a[ ], int n)
{ int temp;
for (int i=0; i<n; i++)
(for (int j=0; j<n-1; j++)
(if (a[j]>a[j+1])

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
LEARNING CENTERS FOR CLASSES VI -XII EduSuccess : Delhi's #1 Tuition Center for Computer Science, Class - 12

{ te mp = a [j] ;
A[j] =a [j+l];
A[j+1].temp;
}
}

cout<,"\n After Pass"«i+1;


for (int k = 0; k<n; k++)
cout«a[k];
}
}

Know the Terms


 aggregate : An array or object of a class with no constructors, no private or protected members, no base
classes, and no virtual functions.
 array : Contiguous sequence of elements. An array doesn't know its own size; the programmer must take
care to avoid range errors. Where possible use the standard library vector.
 constant expression : A C+ + expression that can be evaluated by the compiler that are used to declare
bounds for an array among other things.
 delete[] operator : Used to delete array objects.
 global variable : A variable that is accessible throughout the whole program, whose lifetime is that of the
program.
 initialization : To give an initial value to an object.
 new[] operator : Used to allocate dynamic storage for array objects.
 parameter : Refers to the variables passed into a function.
 rvalue : A value that may appear on the right-hand side of an assignment.
 vector : A one-dimensional array.

Short Answer Type Questions-I [2 marks each]


Q. 4. Observe the following C+ + code and find
out, which out of the given options (i) to (iv) (i) 100$$75
are the expected correct output. Also assign ( i i ) 75$$10$$125$$
the maximum and minimum value that can be (iii) 75$$10$$
assigned to the variable 'Go'. (iv) 10$$125$
[CBSE SQP, 2016]
void main() Ans. (iv) is correct option
{int X [4] = {100, 75, 10, 125}; Max. Value = 3
int Go = random(2) + 2; Min. Value = 2
for (int i = Go; i<4; i ++) (1/2 mark each for minimum and Maximum value)
cout«X[i]«"$$"; [1 mark for correct option]
}

[CBSE Marking Scheme 2016]

Short Answer Type Questions-II


[3 marks each]

Q. 1. Write the difintion of a function grace_score (int {

score [], int size) in C+ +, which should check all if (score[i] <40)
the elements of the array and give an increase of score[i].score[i]+5;
5 to those scores which are less than 40. II .
cout«score[i]«"
Example : if an array of seven integers is as }

follows : }

45,35,85,80,33,27,90 [%2 mark for correct function header]


After executing the function, the array content [1 mark for correct loop]
should be changed as follows : [Y2 mark for correct checking of array elements for
45,40,85,80,38,32,90 less than 40]
Ans. void grace score (int score M . int size) [ 1 /2 mark each for Adding vlaue 5 to the array
{

elements which has value less than 40]


for (int i=0;i<size;i++)

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
ARRAYS LEARNING CENTERS FOR CLASSES VI -XII

Q. 2 Find the output of the following C+ + program : column in the memory. If the element A[20][25]
#include<iostream.h> is stored at 50000, find out the location of A[25]
void repch(char s[]) { [CBSE SQP, 2016] [30]. [Delhi, 2015]
for (int i=0;s[i]!='\0';i++)
{
Ans. A [i] [j ] = B+W x [No. of rows x (I+
(J-Lc)
if (((i%2) ! =0) &&(s[i]!=s[i+1])) A[20] [25] = B + 2x[30x(20-0)+(25-0)]
{

50000 = B+2x[30x(20-0)+(25-0)]
s [1] =‘@/ ; B = 48750
cout«"Hello"; A [7] [10] = 48750+ 2x[30x(7-0)+
}

(10-0) ]
else if (s[i]==s[i+1])
{ = 49190
s[i+1]="!"; [1 mark for writing correct formula or
substitution with correct values]
} [1 mark for calculating correct base address.]
}
[1 mark for calculating correct address of A[7][10].
void main() Q. 5 An array P[30][20] is stored along the column in
{

the memory with each element requiring 2 bytes


char str [1 =" SUCCESS" ; of storage. If the base address of the array P is
cout«"Original String"«str; 26500, find out the location of P[20][10].
repch ( str) ;
[CBSE SQP, 2016]
cout«"Changed String"«str;
}

Ans. Total number of rows = 30


Ans. Original String SUCCESS Total size = 2 bytes
Changed String S@C!ES! Base Address = 26500
[1/2 mark for original String] LOC(P[I][j]) = BaseAddress + ((I-LBR) + (J-
[1/2 mark for '@' in place of U] LBC)*R)W
[1/2 mark for '!' in place of second C] Assuming
[1/2 mark for '!' in place of second S] Lower
[CBSE Marking Scheme 2016] Bound of Row(LBR)=0
Q. 3 Find the output of the following : [CBSE SQP, 2016] Lower Bound of Column(LBC)=0
#include<iostream.h> Total number of Rows(R)=30
void switchover (int A[],int N, Size of each element(W)=2
int split) LOC(P[20][10]) = 26500 + ((20-0)+(10-0)*30)*2
{

LOC(P[20][10]) = 26500 + 640


for (int K = 0; K<N; K++) LOC(P[20][10]) = 27140
if(K<split) [1 mark for using correct formula for column
A[K] + = K; major]
else [1 mark for substituting formula with correct
A [K] *=K; 1 values]
void display (int A [],int N) [1 mark for correct final answer]
[CBSE Marking Scheme 2016]
{

for (in K = 0; K<N; K++) Q. 6 Write a function to sort any array of n elements
(K%2==0) ?cout«A [K] «"%" cout«A using insertion sort. Array should be passed as
[K] «endl ; argument to the function. [Delhi, 2015]
}

Ans.
void main() int p,ptr; void
{ int H[ //Assuming a[0]=int mini n s e r t s o
10, 5 ; ] = {30, 40, 50, 20,
}
smallest integer rt (int a
[] , int
switchover(H, 6, 3); n)
display(H, 6);
{

Ans. 30%41 i.e.


52%60 for (p = 1; p<=n;p++)
{

40%25
temp=a [p]
[1/2 mark for each value in the above order] p t r =p - l ;
[deduct 1/2 mark for not putting '%' in between while(temp<a[ptr])
values] An integer array A [30] [40] is stored {

along the a [ptr+1] =a [ptr] ; //Move Element


Forward a[ptr +1] = temp; //Insert Element in Proper Place
ptr--;
}
}

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
LEARNING CENTERS FOR CLASSES VI -XII
EduSuccess : Delhi's #1 Tuition Center for Computer Science, Class 12-

[1/2 mark for correct for loop] Ans. void NewMAT (int A [] [] tint r, int c)
{

[1/2 mark for assigning first element to temp]


for (int i = 0;i<r;i++)
[1/2 mark for setting ptr = p-1] for(j=0;j<c;j++)
[/2 mark for correct while] if ( (A [i] [j >=60) && (A [1] [j ] <=600) )
[1/2 mark for moving element forward] A [i] [j] /=6;
OR
[l/2 mark for inserting at the proper place] A[i] [j] =A[i] [j] /6;
Q. 7. Write a function NewMAT(int A[][],int r, int c) }

in C + +, which accepts a 2d array of integer [1 mark for correct loops]


and its size as parameters divide all those array [%2 mark for the condition]
elements by 6 which are not in the range 60 to [ 1 /2 mark for changing the element value by
600(both values inclusive) in the 2d Array. dividing it by 6]
[CBSE SQP, 2016] [CBSE Marking Scheme 2016]

Q. 8. Write the definition of a function Alter (intA[], int N) in C+ +, which should change all the multiples of 5 in the
array to 5 and rest of the elements as 0. For example, if an array of 10 integers is as follows : [Delhi, 2015]
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
55 43 20 16 39 90 83 40 48 25
After executing the function, the array content should be changed as follows :
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
5 0 5 0 0 5 0 5 0 5
Ans. void Alter (int A [] int N)
{

for (int i=0; i<N; i++)


if (A[i] %-5==0)
A[i] = 5;
else

A[i] = 0;
}

OR
Any other correct equivalent function definition
(1/2 Mark for correct loop)
(1/2 Mark for correct checking of divisiblity of array elements by 5)
(1/2 Mark for correct use of else OR correct checking of non divisibility of array elements by 5)
(1/2 Mark for correct assignment of 5 and 0 for multiples and non multiples of 5 respectively)
Q. 9. A two dimensional array P[20] [50] is stored = BaseAddress + 4*255
in the memory along the row with each of its = BaseAddress + 1020
element occupying 4 bytes, find the address of BaseAddress = 15000 - 1020 = 13980
the element P[10] [30], if the element P[5] [5] is
LOC (P [10] [30]) = 13980 + 4* [10 * 50 + 30]
stored at the memory location 15000.
= 13980 + 4 * 530
[Delhi, 2015]
= 13980 + 2120
Ans. Loc (P [I] [J] along the row
= 16100
=BaseAddress+W [(I-LBR) *C+ (J-LBC)]
OR LOC (P [10] [30])
(where C is the number of columns, LBR = LBC
= Loc (P[5] [5]) + W[ (I-LBR) * C+ (J-LBC)]
= 0)
= 15000 + 4 [(10 - 5) * 50 + (30 - 5)]
LOC (P [5][5])
= 15000 + 4 [5 * 50 + 25]
= BaseAddress + W* [I*C + J]
= 15000 + 4 *275
15000 = BaseAddress + 4 * [5 * 50 + 5]
= 15000 + 1100
= BaseAddress + 4 * [250 + 5]
= 16100

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
ARRAYS LEARNING CENTERS FOR CLASSES VI -XII

OR = 14184 + 4 [(10 — 1) * 50 + (30 — 1)]


(where C is the number of columns and LBR = = 14184 + 4 [9*50 + 29]
LBC = 1) = 14184 + 4*479
LOC (P [5] [5]) = 14184 + 1916
15000 = BaseAddress + W RI-1) *C + (J — 1)] = 16100
= BaseAddress + 4 [4*50 + 4] (1 Mark for writing correct formula (for row
= BaseAddress + 4 [200 + 4] major)
= BaseAddress + 4 * 204 OR
= BaseAddress + 816 substituting formula with correct values)
BaseAddress = 15000 — 816 = 14184 (1 Mark for at least one step of intermediate
LOC (P [10] [30]) calculation)
(1 Mark for final correct address)
Q. 10. Write the definition of function Change (int P[], int N) in C++, which should change all the multiples of 10 in
the array to 10 and rest of the element as 1. For example, if an arrary of 10 integers is as follows :
P[0] P[1] P[2] P[3] P[4] P[5] P[6] P[7] P[8] P[9]
100 43 20 56 32 91 80 40 45 21

After executing the function, the array content should be changed as follows : [0.D, 2015]
P[0] P[1] P[2] P[3] P[4] P[5] P[6] P[7] P[8] P[9]
10 1 10 1 1 1 10 10 1 1

Ans. void Change ( int P [ ] , int N)


{

for (int i = 0; i<N; i++)


if (P [i] % 10 ==0)
P[ = 10;
else
}
P [i] = 1

OR
Any other correct equivalent function definition
(1/2 Mark for correct loop)
(1/2 Mark for correct checking of divisiblity of array elements by 10)
(1/2 Mark for correct use of else OR correct checking of non divisibility of array elements by 10)
(1/2 Mark for correct assignment of 10 and 1 for multiple and non multiples of 10 respectively)
Q. 11. Write code for a function oddEven (int s[], int N) +, w hich takes integer arrays, each
in C+ +, to add 5 in all the odd values and 10 in
all the even values of the array S.
Example : If the original content of the array S is
50 11 19 24 I 28 containing 6 elements as parameters. The
The modified content will be :
60 16 24 34 I 38

[Delhi, 2014] function should exchange all odd places (1st,


Ans. void oddEven ( int s [] , int N)
{

for (int i=0; i<N; i++)


{
[1]

if (s [i] %2 ! =0)
s [i] =s [i] +5; [1] 3rd and 5th) of the two arrays, for example
else
=s [i] +10;
} Q. 12. Write the definition for a function

If the array A contains


10 12 21 52 76
void Transfer (int A[6], int 13[6]) in C+
I
And 1if
5 the array B contains
41 67 83 13 53 15 41 12 83 52 53
I 23 the function should make the contents of
Then And the contents f array B as
the array A as
I 23 10 67 21 13 76
} [1] [CBSE Comptt., 2013]

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
LEARNING CENTERS FOR CLASSES VI -XII EduSuccess : Delhi's #1 Tuition Center for Computer Science, Class - 12

Ans. void Transfer (int A [6] , int B [6] ) P [j] = temp;


}
{

int temp; } [1]


for(int i=;i<6;i+=2) [ 1 ]

{
Q. 15. Write code for a function void Convert (int T[],
temp=A[i] int Num) in C+ +, which re-positions all the
A[i]=B[i] elements of the array by shifting each of them
B [i] +temp; one to one position before and by shifting the
}
first element to the last position.
} [2] For example : If the content of the array is :
Q. 13 Write a user-defined function DisTen (int A[] [4], 0 1 2 3 4
int N, int M) in C++ to find and display all the 22 25 70 32 12
numbers, which are divisible by 10. For example
if the content of array is : The changed content will be :
12 20 13 0 1 2 3 4
2 10 30 25 70 32 12 22

The output should be [Delhi, 2013]


20 10 30 [0.D, 2013] Ans. void Convert (int TH, int Num)
Ans. void DisTen (int A H [4] , int N, int {

M) int i, j, temp;
{ for (i=Num; i > 10; i-=1)
int i j ; ,
{
for (i=0; i<M; i++) j = i - 1;
{
temp = P [1] ; [1]
for (j=0; j<N; j++) P [i] = P [j] ;
{

P [j] = temp;
if (A[i] [j]9610-0) }

cout « A[i] [j];


}
} [1]
}
Q. 16 Write a user-defined function DispNTen (int L[]
[4], int R, int C) in C+ + to find the display all
} [3] the numbers, which are not divisible by 10. For
Q. 14 Write code for a function void ChangeOver (int example if the content of array is :
P[], int N) in C+ +, which re-positions all the 20 17 30
elements of the array by shifting each of them to
12 19 10
the next position and by shifting the last element
to the first position. The output should be
For example: If the content of the array is : 17 12 19 [Delhi, 2013]
0 1 2 3 4 Ans. void DispNTen (int L H [4] , int R,
12 15 17 13 21 int C)
{

The changed content will be : int i, j;


0 1 2 3 4 for (i=0; i<C; i++)
{for (j=0; j<R; j++)
21 12 15 17 13 {

[0.D., 2013] if (A[i] [j] 010= =0)


Ans. void ChangeOver ( int P , int N) cout «A[i][j]«" ";
{ }

int i, j, temp; [1] }

for (i=0; i<N; i=i+1) [3]


{

j=j+1; [1] Q. 17 Write a function Transfer (int A[], int B[J, int
Size) in C+ + to copy the elements of array A
temp = P [i] ;
into array B in such a way that all the negative
P [i] = P[j]; elements of A appear in the beginning of 13,
followed by all the positive elements, followed

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
ARRAYS LEARNING CENTERS FOR CLASSES VI -XII

by all the zeroes maintaining their respective Where C is the number of columns and
orders in array A. For example : LBR=LBC=1
If the contents of array A are : LOC (ARR[10] [5])
7, -23, 3, 0, -8, -3, 4, 0 15000 = BaseAddress + W [(I - I) * C + (J-1)]
The contents of array B should be = BaseAddress + 4 [9*20 + 4]
-23, -8, -3, 7, 3, 4, 0 = BaseAddress + 4[180 + 4]
= BaseAddress + 4 * 184
[CBSE SQP, 2013]
Ans. void Transfer (int A[], int B[], int size) = BaseAddress + 736
{
BaseAddress = 15000 - 736
for (int i = 0; i<size; = 14264
{

LOC (ARR [30] [10])


if (A[i]<0)
= 14264 + 4 [(30 - 1) * 20 + (10 - 1)]
B[i]=A[i] ; = 14264 + 4[29 * 20 + 9]
if (A [i] > 0)
= 14264 + 4 [580 + 9]
B [i+1] =A [i] ; = 14264 + 4*589
else = 14264 + 2356
B[i/2]=A[i] ; = 16620
}

(1 Ma r k fo r w riting co rr ect for m ula (for r o w


3
[] m a jo r ) O R s u b s t it u ti n g f o r m u la w i t h c o r r e ct
Q. 18. A two dimensional array ARR[50] [20] is stored values)
in the memory along the row with each of its (1 M a r k f o r a t l e a s t o n e s t e p o f i n t er m ed ia te
elements occupying 4 bytes. Find the address of calculation)
the element ARR[30][10], if the element ARR[10]
(1 Mark for final correct address)
[5] is stored at the memory location 15000.
[OD, 2015J Q. 19 Write a function SORTSCORE() in C++ to sort
Ans. Loc (ARR[I] [J] along the row an array of structure IPL in descending order of
= BaseAddress + W [(I - LBR) *C + (J - LBC)] score using selection sort . [CBSE SQP 2015]
(where C is the number of columns, LBR = LBC No te : Assume the follow ing definition of
=0 structure IPL :
struct IPL
LOC (ARR [10] [5]) {

= BaseAddress + W [I *C + J] i n t S c o re ;
15000 = BaseAddress + 4 [10 * 20 + 5] ch ar Teamn a me [20 ] ;
= BaseAddress + 4 [200 + 5] );
= BaseAddress + 4 x 205 An s . v o i d S O R T S C O R E ( I P L a [] , i nt n)
= BaseAddress + 820 int largest;
IPL temp ;
BaseAddress = 15000 - 820
= 14180 for ( int K = 0; K < n-1; K++)
{ largest = K;
LOC (ARR [30] [10] ) = 14180 + 4 [30 * 20 + 10] for ( int j = K+1; j < n; j++)
= 14180 + 4 * 610 { if ( a [j] . score > a [largest]
= 14180 + 2440 score)
{ largest = j;
= 16620 }

OR }

LOC (ARR [30] [10] ) temp = a [K] ;


= LOC (ARR [10] [5] + W [(I - LBR) * C + (J - a [K] = a [largest] ;
LBC)] a [largest] = temp;
}

= 15000 + 4 [(30 -10) * 20 + (10 - 5)] }

= 15000 + 4 [20*20 + 5]
(3 marks for correct program)
= 15000 + 4 *405
= 15000 + 1620 Q. 20 Write a function in C+ + TWOTOONE() which
accepts two array X[ ], Y[ ] and their size n as
= 16620 OR argument. Both the arrays X[ ] and Y[ ] have

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
LEARNING CENTERS FOR CLASSES VI -XII EduSuccess : Delhi's #1 Tuition Center for Computer Science, Class - 12

the same number of elements. Transfer the for (int i=0; i<N; i++)
content from two arrays X[ ], 1([ ] to array Z[ ]. {

The even places (0,2,4...) of array Z[ ] should get if (P [i]% l 0 = = 0 )


the contents from the array X[ ] and odd places {

(1,3,5...) of array Z[ ] should get the contents


int temp=0;
from the array Y[ ].
temp=P [i] ;
Example : If the X[ ] array contains 30,60,90 and
the Y[ array contains 10,20,50. Then Z[ ] should P [i] =P [i+1] ;
contain 30,10,60,20,90,50. [CBSE Comptt.2014] P[i+1].temp;
Ans. void TWOTOONE ( int x int n) i++; //Go to next element
}
{

int z [n] ; }

for (int I =0;i<n;i++)


{
} [3]
Q. 23. Write a function SWAP2BEST (int ARR[], int
if (i%2 == 0)
Size) in C+ + to modify the content of the array
z [i] = x [i] ;
in such a way that the elements, which are
else
multiples of 10 swap with the value present in
z [i] = Y [i] ; the very next position in the array.
}

[0.D, 20121
}

For example :
(3 marks for correct program)
If the content of array ARR is
Q. 21. Write code for a function void EvenOdd (int T[], 90, 56, 45, 20, 34, 54
int C) in C++, to add 1 in all the odd values and
2 in all the even values of the array T. The content of array ARR should become
Example : If the original content of the array T is 56, 90, 45, 34, 20, 54
T[0] T[1] T[2] T[3] T[4] Ans. void SWAP2BEST (int ARR [] , int Size)
{

35 12 16 69 26
for (int n=0 ; n<size ; n++)
The modified content will be: {

if (ARR [n] %10 = =0)


T[0] T[1] T[2] T[3] T[4] {

36 14 18 70 28 int temp = 0;
temp = ARR [n] ;
Ans. void EvenOdd(intT int(C) ARR [n] = ARR [n+1] ;
{

ARR [n+1] = temp;


for (int i=0; i<C; i++) n++;
}

if (T[i] %2==0) T[i]+ =


2; else [3]
T[i]+ = 1; Q. 24. Write the definition of a function
}

} AddTax(float Amt[], int N) in C+ +, which


}

} [1] should modify each element of the array Amt


having N elements, as
Q. 22. Write a function SWAP2 CHANGE (int P[], int
per the following rules : [CBSE Comptt. 2016]
N) in C+ + to modify the content of the array
Existing Value of Amt Amt to be changed to
in such a way that the elements, which are
multiples of 10 swap with the value present in If less than 50,000 Add 25% in the
the very next position in the array. existing value
[Delhi, 2012] If> =50,000 and Add 20% in the
For example : < 1,00,000 existing value
If the content of array P is If> =1,00,000 Add 15% in the
91, 50, 54, 22, 30, 54 existing value
The content of array P should become
91, 54, 50, 22, 54, 30 Ans. void SWAP 2CHANGE Ans. void AddTax (Float Amt [] int N)
{

(int P [] , int N)
{

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
ARRAYS LEARNING CENTERS FOR CLASSES VI -XII

for (int i=0; i<N; i++) A mt [ i ] + = 0 .1 5 * A mt [ i ] ;


}

iF (Amt [i] <50,000)


Amt[i]+=0.25*Amt[i] I (1/2 mark for correctly writing the loop)
else if (Amt [i] >=50000&&Amt [i] (1/2 mark for checking atleast one or all conditions
<100000) correctly)
A mt [ i] +=0 - 2*A mt [ i] ; (1 mark for correct increment of Amt for all
else if(Amt[i]>=1,00000) conditions)

TOPIC-2
Two-dimensional Arrays

Quick Review
1. Two Dimensional Array
A two dimensional array is an array in which each element is itself an array. Two dimensional arrays are called
matrices. A matrix resembles a table with rows and columns. In C+ +, 2-D array notation is
Arrayname[Row] [Column]
No of elements = Row * Column
columns
[0][0] [0][1] [0][2] [0][3] [0][4]
rows [1][O] [1][1] [1][2] [1][3] [1][4]
2
[ ][O] [2][1] [2][2] [2][3] [2][4]
[3][O] [ 3 ] [ 1 ] [3][2] [3][3] [3][4]

2. Implementation of 2-D array in Memory Elements of a 2-D array in memory are allocated contiguous
locations. The array elements are stored linearly

using one of the following two methods :


(1) Row Major : Using this method, a 2-D array is stored with all the elements of first row in sequence follow ed
by elements of second row and so on.
Address of the [I, J]th element = B+S*[(I-Lr)N + (J-Lc)]
where,
B : Base Address (address of very first element).
S : Size of an array element.
Lr : Lower bound of row (first row number).
Lc : Lower bound of column (first column number).
N : No of columns.
(2) Column Major : Using this method, a 2-D array is stored with all the elements of first column in sequence
followed by elements of second column and so on.
Address of the [I, J]th element = B+S*[(I-Lr)+ (J-Lc)*M]
where,

B : Base Address (address of very first element).


S : Size of an array element.

Lr : Lower bound of row (first row number).


Lc : Lower bound of column (first column number).
M : No of rows.

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
LEARNING CENTERS FOR CLASSES VI -XII EduSuccess : Delhi's #1 Tuition Center for Computer Science, Class 12
-

Short Answer Type Questions-I [2 marks each]

Q. 1 Write a function Alternate (int A[] [3], int N, int c o u t « " S u m o f al l th e n o n - n eg at iv e


M) in C+ + to display all alternate elements from elements on both the diagonals=
two-dimensional array A(starting from A[0][0]). "«Sdiag;
} [1]
For example :
If the array is containing : Q. 3 Write a user-defined function swap_row(int
23 54 76 ARR[][3],int R,int C) in C+ + to swap the first
37 19 28 row values with the last row values :
62 13 19 For example :
The output will be 10 20 30
23 76 19 62 19
40 50 60
Ans. void Alternative (int AM [3], int
N, int N) 70 80 90
{
Then after function call, the content of the arrray
should be :
int T.0 ; I<N; I++)
for (int J=0 ; J<M ; J++) 70 80 90
{ 40 50 60
if (T%2 = 0) 10 20 30
cout«A[I] [J]«" II .

T++ ; [CBSE S.Q.P. 20161


Ans. voidswap_row (intARR [3] , int R, int ( ) )
} {

for(int i=0 J=0: J<C;J++)


OR {

void Alternative (int AM [3] , int int temp=ARR[i] [j ] ;


N, int M) ARR [i] [j =ARR [R-1] [j ]
{

ARR [R-1] [j ] =temp;


int *P.&A[0] [0] ;
}

for (int I = 0; I<N*M ; I+.2)


{ (1 Mark for correct loop)
cout«*p«" (1 Mark for correct swapping)
{ I+.2 ; Q. 4 Write a user-defined function SumLast 3 (int A []
} [4], int N, int M) in C+ + to find and display the
} sum of all the values, which are ending with 3
OR (i.e., units place is 3). For example if the content
of array is :
Any other equivalent correct answer acceptable 33 13 92
(1 Mark for writing Correct loops starting for
location [0110]) 99 3 12

eh Mark of logic of checking alternate elements) The output should be 49 [Delhi, 20141
Mark for displaying the alternate elements
12 Ans. void SumLast 3 (int A [] [4] , int N,
Q. 2 Write a function in C+ + to print the sum of all int M)
the non-negative elements present on both the {

diagonal of a two dimensional array passed as int Sum=0;


the argument to the function.
for(int i=0;i<N;i++)
[CBSE SQP 2015]
Ans. void Diagonal ( int A[] [20] , int N) for(int j=0;j<M;j++) [1]
{

int K, Sdiag=0; If ( ( (A[i] [j] -3)% 10.0))


for(int K.O;K<N;K++) Sum-Sum+A [i] [j ] ;
if ( A [K] [K] > 0) }

Sdiag+=A [K] [K] ; [1] COUt<<SUM; [1]


for(int K=O;K<N;K++) }

if ( A[N-K-1] [K] > 0 S d i a g + = A [ N - K - 1 ]


[K];

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
AR RAYS LEARNING CENTERS FOR CLASSES VI -XII

Q. 5. Write a user-defined function AddEnd2(int


[4], int N, int M) in C+ + to find and display the
Q. 7. Write a user-defined function int Sum
sum of all the values, which are ending with 2
Single(int A[4][4])in C+ +, which finds and
(i.e., units place is 2).
returns the sum of all numbers present in the
For example, if the content of arrray is : first row of the array, for example, if the array
22 16 12 contains
19 5 2 1 13 10 9
29 17 2 21
The output should be 36 [0.D, 2014]
Ans. void AddEnd2 (int A H [4] , int N, int 14 3 12 31
M) 15 16 25 52
{

int Sum=O; Then the function should return 33.


for (int 1=0; i<N; i++) [CBSE Comptt., 2013]
for (int j=0; j<m;j++) [1] Ans. int SumSingle (int A[4] [4] )
{

if ( ( (A [i] [j] -2) %10-0) )


S u m= S u m + A [ i ] [ j ; int Sum=O;
}
for(int i=0; i<1;i++)
COUt<<SUM; for (int j=0 ; j <4 ; j ++) [1]
} [1] Su m= Su m+ A [ i] [ j ]
Q. 6. Write a function in C+ + which accepts a return Sum;
2D array of integers and its size arguments } [1]
and displays the elements which lie on minor Q. 8. Write a function SKIPEACH(int H[] [3], int C, int
diagonal. [Top right to bottom left diagonal] R) in C++ to display all alternate elements from
[Assuming the 2D array to be square matrix with
two-dimensional array H (starting from H [ 0 [
odd dimension
0 ] ) . [ D e l h i , 2 0 1 2 ]
i.e. 3 3, 5 5, 7, 7, etc ...]
For example:
. . .

For example :
If the 2D array is If the array is containing:
678 136 793 The following 12 45 67 33 90 76
should be displayed : 21 43 59 The
8 output will be
3 12 67 90 21 59
7 [CBSE Comptt.2014] Ans. void SKIPEACH (int H [ ] [3] , int C,
Ans. void diag (int x [] H , int m, int n) int R)
{
{

for (int i=m;i>0;i– –) for (int 0; i < C; i++)


{
{

for (int j=0;j<n;j++) for (int j = 0, j < R; j++)


{

if (i==j ) if ( (i+j)%2 = =0)


cout«" "«n [i] [j]
}
cout«H [i] [j ] « V i I /

}
}

[2]
(2 marks for correct program)
}

Short Answer Type Questions-II [3 marks each]

Q. 1. An array P[151[10] is stored along the column in Ans. Loc (P[8][5]) = B+W((I-Lr)+ (J-Lc)M)
the memory with each element requiring 4 bytes = 1400 +4((8-0)+ (5-0)15)
of storage. If the base address of array P is 1400,
find out the location of P[8][5]. = 1732. [3]
[Delhi, 2013]

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
LEARNING CENTERS FOR CLASSES VI -XII
EduSuccess : Delhi's #1 Tuition Center for Computer Science, Class - 12

Q. 2 Given an array A[10][12] whose base address is element requiring 2 bytes of storage. If
10000. Calculate the memory location of A[2][5] if the base address of array T is 42000,
each element occupies 4 bytes and array is stored find out the location of T[10][15]. Also,
columnwise. [CBSE Comptt.2014] find the total number of elements present
Ans. w=4 in this array.
b=10000 [Delhi, 2014]
m=10 Ans. Array A[25][20]
n[i][i] = [b+ + m(j-12) Base Address = 42000
n[2][5] = [10000+ 4(2-0) + 10(5-0)] Size of element(W) = 2 bytes
= [10000 +8 +50 ] A[I][J]=A[10][15] hence I=10 and J=15
n[2][5] = 10058 Total Row (R) = 25 [1]
(1 mark for steps) Total Column (C) = 20
Q. 3 An array T[4..35][-2..15] is stored in the memory A [I][J]=B+ W[C(I-0)+ (J-0)]
along the row with each element occupying 4 A[10][15]=42000 +2[20(10-0)+(15-0)] [1]
bytes. Find out the base address and address of
element T[20][5], if an element T[2][2] is stored at = 42000+2[20(10+15)]
the memory location 3000. Find the total number = 42000+2[200+15]
of elements stored in T and number of bytes = 42000+2[215]
allocated to T. = 42000+430]
[CBSE SQP 2015] = 42430 [1]
Ans. Take T[2][2] as the base address [BA],
Q. 5 An array S[10][15] is stored in the memory with
sr = starting row, sc = starting column; each elements requiring 2 bytes of storage. If the
Along the row base address of array S is 25000, determine the
A[i][j] = BA + size[nc*(i - sr) + (j - sc)] location of S[5] [10] if the array is S stored along
T[20] [5] = 3000 + 4 [ 18 * (20 - 2) + (5 - 2)]
the column.
= 3000 + 4[(18 * 18) + (3)]
[CBSE Comptt., 2013]
= 3000 + 4[324 + 3] [1]
Ans. S[10][15] //Base address; 25000
= 3000 + 4(327)
W=2 bytes
= 3000 + 1308
= 4308 S[5][10] =?
nc = no. of columns sr =2 sc=2 m(row)=0 i =5
Total number of elements stored in T = 37*18 = n(column)=15 j =10 [1]
666 Array is stored in the memory along the column:
Total number of bytes allocated to T = 666 * 4 = P[i][j] = Base address + u[i+mxj]
2664 [2] s[5][10] = 25000+2[5+10x10]
Q. 4 An array A[20] [30] is stored along the row in the = 25000+2[5+100]
memory with each element requiring 4 bytes of = 25000+2[105]
storage. If the base address of array A is 32000, = 25000+210
find out the location of A[15][10]. Also, find the = 25210
total number of elements present in this array.
[0.D, 2014] Q. 6 An array T[15][10] is stored along the row in the
Ans. Array A[20][30] memory with each element requiring 8 bytes of
Base Address = 32000 storage. If the base address of array T is 14000,
Size of element(W)=4 bytes find out the location of T[101[7]. [0.D, 2013]
A[I][J]=A[15][10] hence I=15 and J=10 Ans. Base Address = 14000 = B
Total Row (R) = 20 T [I][J] = B + W [C[I-Ir] + (J-Jc)]
Total Column (C) = 30
= 14000 + 8 [10(10-0)+(7-0)]
A [I][J]=B + W[C(I-0)+(J-0)] [1]
= 14000 + 8 x 107
A[15][10]=32000 +4[30(15-0) + (10-0)] [1]
= 32000+4[30(15)+10] = 14000 + 856
= 32000+4[450+10] = 14856 [ 3]
= 32000+4[460] Q. 7 Each element of the array A[8][6] is stored using
= 32000+1840 4 bytes of memory. If the element A[2][4] is
= 33840 [1] An array T[25][20] is stored stored at location 936, find the address of A[5][1].
along the row in the memory with each Assume that the array is stored in Row Major.
[CBSE SQP 2013]

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
ARRAYS LEARNING CENTERS FOR CLASSES VI -XII

Ans. nr = 8 nc = 6 size = 4 bytes display the content of a two dimensional


Al21[4] = 936 A[5][1] = ? array, with each row content in reverse order.
Formula for row-major [Delhi, 2015]
A[i] ll = BA + size * ((i — sr) * nc + (j — sc)) For example, if the content of array is as follows :
A151[1] = A[2][4] + 4 * ((5 — 2) * 6 + (1 — 4)) 15 12 56 45 51
= 936 + 4 * ((3) * 6 — 3) 13 91 92 87 63
= 936 + 4 * 15 11 23 61 46 81
= 936 + 60
The function should display output as
A[5][1] = 996 [3 1
Q. 9. Write a function REVCOL (in PH [5], int N, int 51 45 56 12 15
int M) in C+ + to display the content of a two 63 87 92 91 13
dimensional array, with each column content in 81 46 61 23 81
reverse order. [Delhi, 20151 Ans. void REVROW (int P[] [5], int N, int
Note : Array may contain any number of rows. M)
For example, if the content of array is as follows : {

15 12 56 45 51 for (int I =O;I<NjI++)


13 91 92 87 63 {for (int J=M-1; J>=0; J- )
cout«P[I] [J];
11 23 61 46 81
cout«endl;
}

The function should display output as :


11 23 61 46 81 }
OR
13 91 92 87 63
void REVROw (int P H [5], int N,
15 12 56 45 51 int M)
Ans. void REVCOL (int P [ ] [5] , int N, {

int M)
{
for (int I = 0; I<N; I++)
for (int J=0; J<M/2; J++)
for (int I = N - 1; I>=0; I- -)
{
{

for (int J = 0; J<M; J++) int T = P[I] [J];


cout«P[1] [J]; P [I] [J] = P [I] - J - 1 ] ;

cout«endl; P [I] [M - J - 1] = T;
} }

OR
void REVCOL (int P [] [5], int N, int M) for (I = 0; I<N; I++)
{
{

for (int I = 0; I<N/2; I++) for (int J = 0; J<M; J++)


{

cout«P [I] [J] ;


for (int J = 0; J<M; J++)
{
cout«endl;
}

int T = P [T] [LT] ; }

P [1] [LT] = P [N - I - 1] [J] ;


P [N-I-1] [J] = T; (1 Mark for correct nesting of loop(s))
} (11/2 Mark for correct logic for reversing the
} content of each row)
for (I = 0; I<N; I++) (1/2 Mark for correctly displaying the content)
{
for (int J=0; J<M; J++) Note : N and M can be written interchangeably for
cout«P[I] [J]; number of rows and columns
cout«endl; Q. 11. S[50][20] is a two dimensional array, which is
}

}
stored in the memory along the row with each of
(1 Mark for correct nesting of loop(s) its element occupying 4 bytes, find the address
(1 1 /2 Mark for correct logic for reversing the of the element S[5][15], if the element S[8][10] is
content of each column) stored at the memory location 62,000.
Note :
• N and M can be written interchangeably for [CBSE Comptt., 2016]
number of rows and columns Q. 10. Write a
function REVROW (int N, int M) in C+ + to
EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
LEARNING CENTERS FOR CLASSES VI -XII
EduSuccess : Delhi's #1 Tuition Center for Computer Science, Class - 12

Ans. LOC (S [I] [J] ) 515 102 113 801 145


= Reference Address+W [ (I-Lr)*C+ (J-Lc)] 303 141 129 202 121
LOC (S [5] [15] )
285 189 100 760 112
= LOC(5 [8] [10] +4 [ (15-10)*50+ (5-8) ]
= 62000 + 4 [5*50-3] The function should display the following as
output :
= 62000 + 988
= 62988 515 303 285
113 129 100
(I/2 mark for writing correct formula)
145 121 112 [CBSE Comptt., 2016]
(1 mark for correct calculation) Ans. void SHOWMID (intP [] [5] , intR, int C)
(1 mark for final correct address)
{

for(int J=0; J<C; J++)


Q.12. Write definition for a function SHOWMID cout«P[R/2] [J]«"
(int P[][5], int R, int C) in C+ + to display the cout«endl;
elements of first, third and fifth column from a
for (int I=0;I<R;I++)
two dimenstional array P having R number of
cout«P[I] [C/2]«" I I
rows and C number of columns.
.

For e xa mpl e , i f t he c onte nt of a rray i s as (1/2+1/4marks for correct loop)


follows : (1/2+1/2 mark for correct statement)

Long Answer Type Questions [4 marks each]

Q. 1 T[20][50] is a two dimensional array, which is For example, If the content of array is as
stored in the memory along the row with each of follows:
its element occupying 4 bytes, find the address 115 112 116 101 125
of the element T[15][5], if the element T[10][8] is 103 101 121 102 101
stored at the memory location 52000. 185 109 109 160 172
[Delhi, 2016]
The function should display the following as
Ans. T [20] [50] output :
Along the row 103 101 121 102 101
116 121 109
A [i] [j] = BA + size [((i - sr)*nc + j- sc)]
Ans. void SHOWMID (int P [] [5] , int R, int C)
T [10] [8] = 52000 {

for (int J=0 ;J<C;J++)


T [15] [5] =52000 + 4* ((15 - 10)*50 + (5 - 8))
cout«P[R/2] [J]«" II .
= 52000 + 4* (250 + - 3)
cout«endl;
= 52000 + 4 * 247 for(int I=0;I<R;I++)
= 52000 + 988 cout«P[I] [C/2]«"
}

= 52988 ( 1 /2 Mark for correct loop for displaying middle


Address = 52988 141 row elements)
Q. 2 Write definition for a function SHOWMID (1 Mark for correct statement to display middle
(int P[][5]), int R, int C) in C+ + to display the row elements)
elements of middle row and middle column (1/2 Mark for correct loop for displaying middle
from a two dimensional array P having R column elements)
number of rows and C number of columns. (1 Mark for correct statement to display middle
[Delhi, 2016] column elements)
Q. 3. R[20][50] is a two dimensional array, which is stored in the memory along the row with each of its element
occupying 8 bytes, find the address of the element R[5][15], if the element R[8][10] is stored at the memory
location 45,000. [0.D, 2016]
Ans. R[20][50]
Along the row
A[i][j] = BA + size [(i - Sr)nc + (j-Sc)]
R[8][10] = 45000

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
ARRAYS LEARNING CENTERS FOR CLASSES VI -XII

R[5][15] = 45000 + 8* ((5 - 8)* 50 + (15 - 10))


= 45000 + 8* (- 3*50 + 5)
= 45000 + 8* (-145)
= 45000 - 1160
R[5][15] = 43840 141

Know the Links


Download Chapterwise notes, study materials, Ten Year Solved Papers, Important Questions
of Physics, Chemistry, Maths, Biology, Computer Science from www.edusuccess.in

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252

You might also like