Chapter 8: Arrays Class 12 Computer Science NCERT Solutions
Arrays are an essential part of programming, especially when handling multiple data elements of the same type. In Chapter 8 – Arrays, students learn how to use arrays for storing and manipulating large sets of data efficiently. The chapter explains both one-dimensional and two-dimensional arrays in C++ and how they are declared, initialized, accessed, and processed through loops and functions.
What You Will Learn in Chapter 8
This chapter builds a solid foundation in using arrays to store structured data. Students will learn about the memory representation of arrays, how to perform operations like searching and sorting, and how arrays simplify repetitive tasks. The chapter also introduces passing arrays to functions and multidimensional array handling.
Key Topics Covered
Introduction to Arrays
An array is a collection of similar data types stored under a single variable name. Arrays allow random access to data and are indexed starting from 0.
Types of Arrays
- One-Dimensional Arrays (1D Arrays)
Syntax:
int arr[10];
Initialization:
int arr[5] = {10, 20, 30, 40, 50};
Accessing Elements:
arr[2]
refers to the 3rd element. - Two-Dimensional Arrays (2D Arrays)
Syntax:
int matrix[3][3];
Initialization:
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };
Accessing Elements:
matrix[1][2]
refers to the element in the 2nd row, 3rd column.
Array Memory Representation
Arrays are stored in contiguous memory locations. The index is used as an offset from the base address. Arrays do not check bounds; accessing out-of-range indices may cause unexpected behavior.
Traversal and Processing
Using loops (for, while) to iterate over array elements. Example:
for (int i = 0; i < 5; i++) { cout << arr[i] << \" \"; }
Searching in Arrays
Linear Search: Checks each element one by one.
for (int i = 0; i < n; i++) { if (arr[i] == key) { // Found } }
Sorting Arrays
Simple sorting algorithms like Bubble Sort, Selection Sort.
Example (Bubble Sort):
for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { swap(arr[j], arr[j+1]); } } }
Passing Arrays to Functions
Arrays can be passed by reference to functions.
- 1D Syntax:
void display(int arr[], int size);
- 2D Syntax:
void display(int arr[][3], int rows);
Multidimensional Arrays
Arrays with more than two dimensions: not typically covered at this level but introduced. Mainly focus on 1D and 2D arrays.
Common Array Programs
- Find maximum/minimum in array.
- Calculate sum and average.
- Count even/odd numbers.
- Matrix addition, subtraction.
Download Chapter 8 Solutions PDF – Arrays
Our PDF includes:
- Complete NCERT solutions with well-explained C++ code.
- Memory layout and dry-run tables.
- Output-based questions with complete answers.
- Sorting and searching program templates.
- Visual explanation of 2D arrays.
Highlights of Our NCERT Solutions
- Clear and concise array logic with loop tracing.
- Differentiation between 1D and 2D arrays with diagrams.
- Real exam-style questions and sample code.
- Flowcharts for search and sort logic.
- Step-by-step array program walkthroughs.
Recommended Preparation Tips
- Practice basic loop-based problems using arrays.
- Memorize the syntax for declaring and initializing arrays.
- Write and debug small programs for searching, sorting, and matrix operations.
- Dry-run array programs manually to understand index-based logic.
- Understand passing arrays to functions and write reusable code blocks.
Additional Study Resources
- Array Flashcards: Syntax, declaration, memory layout.
- Worksheets: Fill-in-the-blank, trace-the-code exercises.
- MCQs and error correction problems on arrays.
- Previous Year Questions with solutions.
- Practice programs for sorting and matrix operations.
Mastering Chapter 8 – Arrays
Mastery of arrays is vital for writing efficient, organized C++ programs. Arrays are used in almost all real-world applications and are foundational for data structures like stacks, queues, and linked lists. A strong understanding of arrays prepares students to tackle more advanced programming challenges and competitive coding problems.
Consistent practice, logic development, and hands-on coding will ensure thorough preparation for board exams and beyond.
Class 12 Computer Science (C++) – Chapter 8 Arrays
TOPIC-1 Data Structures and One-dimensional Arrays Short Answer Type Questions-I[2 Marks Each]
Question 1: Observe the following C++ code and find out, which out of the given options (i) to (iv) are the expected correct output. Also, assign the maximum and minimum value that can be assigned to the variable ‘Go’.
void main() {
int X[4] = {100, 75, 10, 125};
int Go = random(2) + 2;
for (int i = Go; i < 4; i++)
cout << X[i] << "$$";
}
Answer:
Correct option: (iv) 10$$125$
Max. Value = 3
Min. Value = 2
Question 2:
Write the definition of a function grace_score(int score[], int size)
in C++, which should give an increase of 5 to those scores which are less than 40.
Answer:
void grace_score(int score[], int size) {
for(int i = 0; i < size; i++) {
if (score[i] < 40)
score[i] += 5;
}
}
Question 3: Find the output of the following C++ program:
void repch(char s[]) {
for(int i = 0; s[i] != '\0'; i++) {
if(((i % 2) != 0) && (s[i] != s[i + 1])) {
s[i] = '@';
cout << "Hello";
} else if (s[i] == s[i + 1]) {
s[i + 1] = '!';
i++;
}
}
}
void main() {
char str[] = "SUCCESS";
cout << "Original String: " << str;
repch(str);
cout << "Changed String: " << str;
}
Answer:
Original String: SUCCESS
Changed String: S@C!ES!
Question 4: Find the output of the following program:
void switchover(int A[], int N, int split) {
for(int K = 0; K < N; K++) {
if(K < split)
A[K] += K;
else
A[K] *= K;
}
}
void display(int A[], int N) {
for(int K = 0; K < N; K++) {
(K % 2 == 0) ? cout << A[K] << "%" : cout << A[K] << endl;
}
}
void main() {
int H[] = {30, 40, 50, 20, 10, 5};
switchover(H, 6, 3);
display(H, 6);
}
Answer:
30%41
52%60
40%25
Question 5: An integer array A[30][40] is stored along the column in memory. If the element A[20][25] is stored at address 50000, find out the location of A[25][30].
Answer: Using the formula:Address = B + W * [No. of rows * (j - Lc) + (i - Lr)]
Given: W = 2 bytes, Lr = Lc = 0
A[20][25] = 50000 → 50000 = B + 2 × [30 × 25 + 20] = B + 2 × 770 = B + 1540
So, B = 50000 – 1540 = 48460
A[25][30] = 48460 + 2 × [30 × 30 + 25] = 48460 + 2 × 925 = 48460 + 1850 = 50310
Question 6: An array P[30][20] is stored along the column in memory, with each element requiring 2 bytes of storage. If the base address of the array P is 26500, find out the location of P[20][10].
Answer:Address = Base + ((i - LBR) + (j - LBC) * Rows) * W
= 26500 + ((20 – 0) + (10 – 0) × 30) × 2
= 26500 + (20 + 300) × 2 = 26500 + 320 × 2 = 27140
Question 7:
Write a function NewMAT(int A[][], int r, int c)
in C++, which accepts a 2D array of integers and its size as parameters. Divide all those array elements by 6 which are not in the range 60 to 600 (both values inclusive) in the 2D array.
void NewMAT(int A[][100], int r, int c) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (A[i][j] < 60 || A[i][j] > 600) {
A[i][j] /= 6;
}
}
}
}
Question 8:
Write the definition of a function Alter(int A[], int N)
in C++, which should change all the multiples of 5 in the array to 5 and rest of the elements as 0.
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;
}
}
Question 9:
A two-dimensional array P[20][50]
is stored in the memory along the row with each of its elements occupying 4 bytes. Find the address of the element P[10][30]
, if the element P[5][5]
is stored at the memory location 15000.
Answer:
LOC(P[i][j] along the row) = Base + W * [(i - LBR) * C + (j - LBC)]
Given:
LOC(P[5][5]) = 15000
15000 = Base + 4 * [(5 * 50) + 5] = Base + 4 * 255 = Base + 1020
=> Base = 13980
LOC(P[10][30]) = 13980 + 4 * [(10 * 50) + 30] = 13980 + 4 * 530 = 13980 + 2120 = 16100
Answer: 16100
Question 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 the rest of the elements as 1.
void Change(int P[], int N) {
for (int i = 0; i < N; i++) {
if (P[i] % 10 == 0)
P[i] = 10;
else
P[i] = 1;
}
}
Question 11: Write code for a function oddEven(int s[], int N) in C++, to add 5 in all the odd values and 10 in all the even values of the array s.
void oddEven(int s[], int N)
{
for (int i = 0; i < N; i++)
{
if(s[i] % 2 != 0)
s[i] = s[i] + 5;
else
s[i] = s[i] + 10;
}
}
Question 12: Write the definition for a function Transfer(int A[6], int B[6]) in C++, which takes integer arrays, each containing 6 elements as parameters. The function should exchange all odd places (1st, 3rd, and 5th) of the two arrays.
void Transfer(int A[6], int B[6])
{
int temp;
for (int i = 0; i < 6; i += 2)
{
temp = A[i];
A[i] = B[i];
B[i] = temp;
}
}
Question 13: Write a user-defined function DisTen(int A[][4], int N, int M) in C++ to find and display all the numbers which are divisible by 10.
void DisTen(int A[][4], int N, int M)
{
int i, j;
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
if (A[i][j] % 10 == 0)
cout << A[i][j] << " ";
}
}
}
Question 14: Write code for a function ChangeOver(int P[], int N) in C++, which re-positions all the elements of the array by shifting each of them to the next position and shifting the last element to the first position.
void ChangeOver(int P[], int N)
{
int temp;
temp = P[N-1];
for (int i = N - 1; i > 0; i--)
{
P[i] = P[i - 1];
}
P[0] = temp;
}
Question 15: Write code for a function Convert(int T[], int Num) in C++, which re-positions all the elements of the array by shifting each of them one to one position before and shifting the first element to the last position.
void Convert(int T[], int Num)
{
int temp = T[0];
for (int i = 0; i < Num - 1; i++)
{
T[i] = T[i + 1];
}
T[Num - 1] = temp;
}
Question 16: Write a user-defined function DispNTen(int L[][4], int R, int C) in C++ to find and display all the numbers which are not divisible by 10.
void DispNTen(int L[][4], int R, int C)
{
int i, j;
for (i = 0; i < C; i++)
{
for (j = 0; j < R; j++)
{
if (L[i][j] % 10 != 0)
cout << L[i][j] << " ";
}
}
}
Question 17: Write a function Transfer(int A[], int B[], int Size) in C++ to copy the elements of array A into array B in such a way that all the negative elements of A appear in the beginning of B, followed by all the positive elements, followed by all the zeroes maintaining their respective orders in array A.
void Transfer(int A[], int B[], int size)
{
int negIndex = 0, posIndex = size - 1, zeroIndex = size / 2;
for (int i = 0; i < size; i++)
{
if (A[i] < 0)
B[negIndex++] = A[i];
else if (A[i] > 0)
B[posIndex--] = A[i];
else
B[zeroIndex++] = A[i];
}
}
Question 18: A two-dimensional array ARR[50][20] is stored in memory along the row with each of its elements occupying 4 bytes. Find the address of the element ARR[30][10], if the element ARR[10][5] is stored at memory location 15000.
// Calculation:
// BaseAddress = 15000 - (4 * (10 * 20 + 5)) = 14180
// LOC(ARR[30][10]) = 14180 + (4 * (30 * 20 + 10)) = 16620
Question 19: Write a function SORTSCORE() in C++ to sort an array of structure IPL in descending order of score using selection sort.
struct IPL
{
int Score;
char Teamname[20];
};
void SORTSCORE(IPL a[], int n)
{
int largest;
IPL temp;
for (int K = 0; K < n - 1; K++)
{
largest = K;
for (int j = K + 1; j < n; j++)
{
if (a[j].Score > a[largest].Score)
{
largest = j;
}
}
temp = a[K];
a[K] = a[largest];
a[largest] = temp;
}
}
Question 20: Write a function in C++ TWOTOONE() which accepts two arrays X[] and Y[] and their size n as arguments. Both arrays X[] and Y[] have the same number of elements. Transfer the content from two arrays X[], Y[] to array Z[]. The even places (0, 2, 4…) of array Z[] should get the contents from array X[] and the odd places (1, 3, 5…) of array Z[] should get the contents from array Y[].
void TWOTOONE(int X[], int Y[], int n)
{
int Z[2*n];
for (int i = 0; i < n; i++)
{
Z[2 * i] = X[i];
Z[2 * i + 1] = Y[i];
}
// Optionally, display Z[]
for (int i = 0; i < 2 * n; i++)
{
cout << Z[i] << " ";
}
}
Question 21: Write code for a function void EvenOdd(int T[], int C) in C++, to add 1 in all the odd values and 2 in all the even values of the array T. Example: T[0] T[1] T[2] T[3] T[4] 35 12 16 69 26 The modified content will be: T[0] T[1] T[2] T[3] T[4] 36 14 18 70 28
void EvenOdd(int T[], int C)
{
for (int i = 0; i < C; i++)
{
if (T[i] % 2 == 0)
T[i] += 2;
else
T[i] += 1;
}
}
Question 22: Write a function SWAP2CHANGE(int P[], int N) in C++ to modify the content of the array in such a way that the elements, which are multiples of 10 swap with the value present in the very next position in the array. Example: Original Array: 91, 50, 54, 22, 30, 54 Modified Array: 91, 54, 50, 22, 54, 30
void SWAP2CHANGE(int P[], int N)
{
for (int i = 0; i < N; i++)
{
if (P[i] % 10 == 0)
{
int temp = 0;
temp = P[i];
P[i] = P[i + 1];
P[i + 1] = temp;
i++; // Go to the next element
}
}
}
Question 23: Write a function SWAP2BEST(int ARR[], int Size) in C++ to modify the content of the array in such a way that the elements, which are multiples of 10 swap with the value present in the very next position in the array. Example: Original Array: 90, 56, 45, 20, 34, 54 Modified Array: 56, 90, 45, 34, 20, 54
void SWAP2BEST(int ARR[], int Size)
{
for (int n = 0; n < Size; n++)
{
if (ARR[n] % 10 == 0)
{
int temp = 0;
temp = ARR[n];
ARR[n] = ARR[n + 1];
ARR[n + 1] = temp;
n++; // Skip the next element
}
}
}
Question 24: Write the definition of a function AddTax(float Amt[], int N) in C++, which should modify each element of the array Amt having N elements, as per the following rules: – If less than 50,000, add 25% to the existing value. – If greater than or equal to 50,000 but less than 100,000, add 20% to the existing value. – If greater than or equal to 100,000, add 15% to the existing value.
void AddTax(float Amt[], int N)
{
for (int i = 0; i < N; i++)
{
if (Amt[i] < 50000)
Amt[i] += 0.25 * Amt[i];
else if (Amt[i] >= 50000 && Amt[i] < 100000)
Amt[i] += 0.2 * Amt[i];
else if (Amt[i] >= 100000)
Amt[i] += 0.15 * Amt[i];
}
}
TOPIC-2 Two-dimensional Arrays Short Answer Type Questions-I[2 marks each]
Question 1: An array P[15][10] is stored column-wise in memory. Each element occupies 4 bytes. If the base address is 1400, find the address of P[8][5].
Base Address (B) = 1400 Element Size (W) = 4 bytes Rows (R) = 15, Columns (C) = 10 Formula for column-wise storage:Loc(P[i][j]) = B + W × [(i - Lr) + (j - Lc) × R]
Assuming lower bounds Lr = 0, Lc = 0
Substitute values:
Loc(P[8][5]) = 1400 + 4 × (8 + 5 × 15) = 1400 + 4 × 83 = 1732
Answer: The memory location of P[8][5] is 1732.
Question 2: Given array A[10][12] is stored column-wise. Each element uses 4 bytes. Base address is 10000. Find address of A[2][5].
Base Address (B) = 10000 Element Size (W) = 4 bytes Rows (R) = 10 Using column-major formula:Loc(A[2][5]) = 10000 + 4 × (2 + 5 × 10) = 10000 + 4 × 52 = 10208
Answer: Memory location of A[2][5] is 10208.
Question 3: An array T[-1..35][-2..15] is stored row-wise. Each element is 4 bytes. T[2][2] is at address 3000. Find address of T[20][5]. Also, calculate total elements and bytes used.
Rows = 37 (from -1 to 35), Columns = 18 (from -2 to 15) Element size = 4 bytes Known: Loc(T[2][2]) = 3000 Row-major formula:Loc(T[i][j]) = BA + W × [nc × (i - sr) + (j - sc)]
Where: sr = -1, sc = -2, nc = 18
Loc(T[20][5]) = 3000 + 4 × [18 × (20 - (-1)) + (5 - (-2))]
= 3000 + 4 × (378 + 7) = 3000 + 4 × 385 = 3000 + 1540 = 4540
Total elements = 37 × 18 = 666
Total bytes = 666 × 4 = 2664
Answer: Address of T[20][5] is 4540.
Total elements = 666, Total bytes = 2664.
Question 4: Array A[20][30] is stored row-wise. Each element is 4 bytes. Base address is 32000. Find address of A[15][10]. Also, calculate total elements.
Base Address = 32000 Rows = 20, Columns = 30, Element Size = 4 bytes Row-major formula:Loc(A[i][j]) = B + W × [C × (i - Lr) + (j - Lc)]
Assuming Lr = 0, Lc = 0
Loc(A[15][10]) = 32000 + 4 × [30 × 15 + 10] = 32000 + 4 × 460 = 33840
Total elements = 20 × 30 = 600
Answer: Address of A[15][10] is 33840.
Total number of elements = 600.
Question 5: An array B[15][20] is stored column-wise. Each element occupies 2 bytes. Base address is 2000. Find the address of B[10][5].
Base Address (B) = 2000 Element Size (W) = 2 bytes Rows (R) = 15, Columns (C) = 20 Using column-major formula:Loc(B[i][j]) = B + W × [(i - 0) + (j - 0) × R]
Loc(B[10][5]) = 2000 + 2 × (10 + 5 × 15) = 2000 + 2 × 85 = 2170
Answer: The address of B[10][5] is 2170.
Reference Table: Address Calculation Summary
Storage Type | Formula |
---|---|
Row-wise | Loc(A[i][j]) = B + W × [C × (i - Lr) + (j - Lc)] |
Column-wise | Loc(A[i][j]) = B + W × [(i - Lr) + (j - Lc) × R] |
Question 6: Array M[5][8] is stored row-wise. Each element takes 4 bytes. Base address is 5000. Find the address of M[3][6].
Base Address = 5000
Rows = 5, Columns = 8, Element size = 4 bytes
Row-wise formula:
Loc(M[i][j]) = B + W × [(i - 0) × C + (j - 0)]
Loc(M[3][6]) = 5000 + 4 × (3 × 8 + 6) = 5000 + 4 × 30 = 5120
Answer: The address of M[3][6] is 5120.
Question 7: Array X[10][5] is stored column-wise. Each element is 2 bytes. Base address is 7000. Find the address of X[6][2].
Base Address = 7000
Rows = 10, Element size = 2 bytes
Column-wise formula:
Loc(X[i][j]) = B + W × [i + j × R]
Loc(X[6][2]) = 7000 + 2 × (6 + 2 × 10) = 7000 + 2 × 26 = 7052
Answer: The address of X[6][2] is 7052.
Question 8: Array A[6][4] is stored row-wise. Each element takes 2 bytes. Base address is 1200. Find the address of A[4][3].
Base Address = 1200
Rows = 6, Columns = 4, Element size = 2 bytes
Row-wise formula:
Loc(A[i][j]) = B + W × [(i × C) + j]
Loc(A[4][3]) = 1200 + 2 × (4 × 4 + 3) = 1200 + 2 × 19 = 1238
Answer: The address of A[4][3] is 1238.
Question 9: An array D[25][30] is stored column-wise. Each element uses 4 bytes. Base address is 6000. Find the address of D[10][5].
Base Address = 6000
Rows = 25, Element size = 4 bytes
Column-wise formula:
Loc(D[i][j]) = B + W × [i + j × R]
Loc(D[10][5]) = 6000 + 4 × (10 + 5 × 25) = 6000 + 4 × 135 = 6540
Answer: The address of D[10][5] is 6540.
Question 10: Array P[10][10] is stored row-wise. Each element occupies 2 bytes. Base address is 1500. Find the address of P[5][4].
Base Address = 1500
Rows = 10, Columns = 10, Element size = 2 bytes
Row-wise formula:
Loc(P[i][j]) = B + W × [(i × C) + j]
Loc(P[5][4]) = 1500 + 2 × (5 × 10 + 4) = 1500 + 2 × 54 = 1608
Answer: The address of P[5][4] is 1608.
Question 11: S[50][20] is a two-dimensional array stored in memory row-wise. Each element occupies 4 bytes. Find the address of the element S[5][15], if S[8][10] is stored at memory location 62,000.
Formula:
LOC(S[i][j]) = Base_Address + W × [(i - Lr) × C + (j - Lc)]
Given:
W = 4 bytes, C = 20
Assuming Lr = 0, Lc = 0
Offset = (5 – 8) × 20 + (15 – 10) = -60 + 5 = -55
Address difference = -55 × 4 = -220
Address of S[5][15] = 62000 – 220 = 61780
Answer: The address of S[5][15] is 61780.
Question 12: Write a function SHOWMID(int P[][5], int R, int C) in C++ to display the first, third, and fifth columns of a 2D array.
Example Input:
515 102 113 801 145
303 141 129 202 121
285 189 100 760 112
Output:
515 303 285
113 129 100
145 121 112
void SHOWMID(int P[][5], int R, int C) {
for (int i = 0; i < R; i++) {
cout << P[i][0] << " " << P[i][2] << " " << P[i][4] << endl;
}
}
Long Answer Type Questions [4 marks each]
Question 1: T[20][50] is a 2D array stored row-wise. Each element takes 4 bytes. Find the address of T[15][5] if T[10][8] is at address 52000.
Offset = (15 – 10) × 50 + (5 – 8) = 250 – 3 = 247
Address difference = 247 × 4 = 988
Address of T[15][5] = 52000 + 988 = 52988
Answer: The address of T[15][5] is 52988.
Question 2: Write a function SHOWMID(int P[][5], int R, int C) in C++ to display the middle row and middle column of a 2D array.
Example Input:
115 112 116 101 125
103 101 121 102 101
185 109 109 160 172
Output:
103 101 121 102 101
116 121 109
void SHOWMID(int P[][5], int R, int C) {
for (int j = 0; j < C; j++)
cout << P[R / 2][j] << " ";
cout << endl;
for (int i = 0; i < R; i++)
cout << P[i][C / 2] << " ";
}
Question 3: R[20][50] is a 2D array stored row-wise. Each element takes 8 bytes. Find the address of R[5][15] if R[8][10] is stored at location 45000.
Offset = (5 – 8) × 50 + (15 – 10) = -150 + 5 = -145
Address difference = -145 × 8 = -1160
Address of R[5][15] = 45000 – 1160 = 43840
Answer: The address of R[5][15] is 43840.