CBSE Class 12 Computer Science Chapter 8: Arrays NCERT Solutions
This chapter provides comprehensive NCERT Solutions for Class 12 Computer Science, focusing on Arrays. It covers the fundamental concepts of data structures, specifically one-dimensional arrays in C++. The solutions explain how to declare, initialize, and manipulate arrays, including iterating through them and accessing elements. Key topics include understanding array indexing, the importance of array size, and common operations. The provided solutions offer step-by-step explanations for various programming problems, helping students grasp the practical application of arrays in C++. These solutions are designed to aid students in understanding array manipulation techniques, debugging code, and preparing effectively for their board examinations by reinforcing theoretical knowledge with practical examples.
Quick info
| Board | CBSE |
|---|---|
| Class | Class 12 |
| Subject | Computer Science |
| Session | 2026 |
| Language | English |
| Type | NCERT Solutions |
| Chapter | 8. Arrays |
Chapter summary
Chapter 8 on Arrays for CBSE Class 12 Computer Science delves into the essential data structure of one-dimensional arrays. The NCERT Solutions cover array declaration, initialization, and traversal using loops. It addresses practical programming problems involving array manipulation, such as modifying elements based on conditions and predicting program output. The solutions emphasize understanding array indexing and element access, crucial for efficient data handling in C++.
Learning outcomes
- Understand the concept of one-dimensional arrays in C++.
- Learn to declare, initialize, and access elements of an array.
- Apply loops for traversing and manipulating array elements.
- Analyze and predict the output of C++ programs involving arrays.
- Implement functions to perform operations on arrays.
Topics covered
Paper topics
- Data Structures
- One-dimensional Arrays
- Array Declaration and Initialization
- Array Traversal
- Array Element Access
- C++ Programming
- Functions with Arrays
- String Manipulation in C++
- Conditional Array Operations
- Output Prediction
Important topics
- One-dimensional Arrays in C++
- Array Declaration and Initialization
- Looping through Arrays
- Functions Operating on Arrays
- Predicting C++ Program Output with Arrays
PDF preview
Read page by page below. PDF is streamed from the official NCERT website — no download button on this page.
Questions and Solutions
Question 1.
void main() { int X[4] = {100, 75, 10, 125}; int Go = random(2) + 2; for (int i = Go; i < 4; i++) cout
Options:
100$$75
75$$10$$125$$
75$$10$$
10$$125
The variable Go is assigned a value using random(2) + 2. The random(2) function typically returns either 0 or 1. Therefore, Go can be either 0 + 2 = 2 or 1 + 2 = 3.
The for loop starts with i = Go and continues as long as i < 4. Let's analyze the two possible values for Go:
- If Go = 2: The loop will execute for
i = 2andi = 3. The elements printed will beX[2](which is 10) andX[3](which is 125). The output would be10$$125. - If Go = 3: The loop will execute for
i = 3. The element printed will beX[3](which is 125). The output would be125.
Looking at the options provided, none exactly match the outputs derived from the possible values of Go. However, if we assume the question implies a scenario where the loop runs for at least two iterations, option (iv) 10$$125 is the closest if Go was intended to be 2. Option (iii) 75$$10$$ is incorrect as it starts from index 1. Option (ii) 75$$10$$125$$ is incorrect as it starts from index 1. Option (i) 100$$75 is incorrect as it starts from index 0.
Revisiting the problem, it's possible there's a misunderstanding of the `random` function or the options. If we strictly follow the code and assume `random(2)` can yield 0 or 1, then `Go` is 2 or 3. If `Go` is 2, output is `10$$125`. If `Go` is 3, output is `125`. Neither matches options (i), (ii), or (iii). Option (iv) `10$$125` matches the case when `Go` is 2.
Let's assume option (iv) is the intended correct output, implying Go must be 2.
Maximum value for 'Go': 3 (when random(2) returns 1)
Minimum value for 'Go': 2 (when random(2) returns 0)
Expected Correct Output: Option (iv) 10$$125 (assuming Go = 2)
Maximum Value Assigned to 'Go': 3
Minimum Value Assigned to 'Go': 2
Question 1.
grace_score (int score [], int size) in C++, which should check all the elements of the array and give an increase of 5 to those scores which are less than 40.
Example: if an array of seven integers is as follows: 45, 35, 85, 80, 33, 27, 90 After executing the function, the array content should be changed as follows: 45, 40, 85, 80, 38, 32, 90
The function grace_score takes an integer array score and its size as input. It iterates through each element of the array using a for loop. Inside the loop, it checks if the current element score[i] is less than 40. If the condition is true, it adds 5 to that element, effectively increasing the score by 5. This process ensures that only scores below 40 receive the grace marks.
Here is the C++ function definition:
void grace_score(int score[], int size) {
for (int i = 0; i < size; i++) {
if (score[i] < 40) {
score[i] = score[i] + 5; } } } The example provided demonstrates this: 35 becomes 40 (35+5), 33 becomes 38 (33+5), and 27 becomes 32 (27+5), while scores 45, 85, 80, and 90 remain unchanged as they are not less than 40.
Question 2.
#include<iostream.h>
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;
}
Let's trace the execution of the repch function with the input string "SUCCESS".
The initial string is "SUCCESS". The program first prints "Original StringSUCCESS".
Now, the repch function is called:
- i = 0:
s[0]is 'S'.i%2is 0. The firstifcondition is false. Theelse ifconditions[0]==s[1]('S'=='U') is false. - i = 1:
s[1]is 'U'.i%2is 1 (odd).s[1]!=s[2]('U'!='C') is true. Both conditions for the firstifare true. So,s[1]becomes '@'. The program prints"Hello". The string is now"S@CCESS". - i = 2:
s[2]is 'C'.i%2is 0. The firstifcondition is false. Theelse ifconditions[2]==s[3]('C'=='C') is true. So,s[3]becomes '!'. Theni++is executed, makingibecome 3. The string is now"S@C!ESS". - i = 3: The loop increments
ito 4.s[4]is 'E'.i%2is 0. The firstifcondition is false. Theelse ifconditions[4]==s[5]('E'=='S') is false. - i = 4:
s[4]is 'E'.i%2is 0. The firstifcondition is false. Theelse ifconditions[4]==s[5]('E'=='S') is false. - i = 5:
s[5]is 'S'.i%2is 1 (odd).s[5]!=s[6]('S'!='S') is false. The firstifcondition is false. Theelse ifconditions[5]==s[6]('S'=='S') is true. So,s[6]becomes '!'. Theni++is executed, makingibecome 6. The string is now"S@C!E!". - i = 6: The loop increments
ito 7.s[7]is the null terminator'\0'. The loop conditions[i]!='\0'is false, and the loop terminates.
The function finishes. The program then prints "Changed String" followed by the modified string.
Output:
Original StringSUCCESSHelloChanged StringS@C!E!
Question 3.
#include<iostream.h>
void switchover (int A[],int N, int split) {
for (int K=0; K
Let's trace the execution step by step.
1. Initialization: An integer array H is initialized with values: {30, 40, 50, 20, 10, 5}. The size N is 6, and the split point is 3.
2. Calling switchover(H, 6, 3):
- K = 0:
K < split(0 < 3) is true.H[0] = H[0] + 0 = 30 + 0 = 30. - K = 1:
K < split(1 < 3) is true.H[1] = H[1] + 1 = 40 + 1 = 41. - K = 2:
K < split(2 < 3) is true.H[2] = H[2] + 2 = 50 + 2 = 52. - K = 3:
K < split(3 < 3) is false.H[3] = H[3] * 3 = 20 * 3 = 60. - K = 4:
K < split(4 < 3) is false.H[4] = H[4] * 4 = 10 * 4 = 40. - K = 5:
K < split(5 < 3) is false.H[5] = H[5] * 5 = 5 * 5 = 25.
After switchover, the array H becomes: {30, 41, 52, 60, 40, 25}.
3. Calling display(H, 6):
- K = 0:
K%2 == 0(0%2 == 0) is true. PrintH[0](30) followed by '%'. Output:30%. - K = 1:
K%2 == 0(1%2 == 0) is false. PrintH[1](41) followed by endl. Output:41(on a new line). - K = 2:
K%2 == 0(2%2 == 0) is true. PrintH[2](52) followed by '%'. Output:52%. - K = 3:
K%2 == 0(3%2 == 0) is false. PrintH[3](60) followed by endl. Output:60(on a new line). - K = 4:
K%2 == 0(4%2 == 0) is true. PrintH[4](40) followed by '%'. Output:40%. - K = 5:
K%2 == 0(5%2 == 0) is false. PrintH[5](25) followed by endl. Output:25(on a new line).
Final Output:
30%41
52%60
40%25
Common mistakes
- Off-by-one errors in loop conditions when accessing array elements.
- Incorrectly handling array index out of bounds.
- Misunderstanding the effect of array element modifications within functions.
- Errors in predicting program output due to complex array manipulations.
Revision tips
- Practice tracing the execution of array-based C++ programs manually.
- Focus on understanding loop conditions and array indexing for accurate output prediction.
- Rewrite the provided C++ code snippets and experiment with different inputs.
- Pay close attention to how functions modify array elements passed as arguments.
Practice MCQs
Q1. In the given C++ code, what is the purpose of the `random(2) + 2` statement for the variable `Go`?
Explanation: The `random(2)` function typically returns 0 or 1. Adding 2 to this result means `Go` will be assigned either 2 (0+2) or 3 (1+2).
Q2. Which loop is used to iterate through the array `X` in the first question's code?
Explanation: The code explicitly uses a `for` loop with the structure `for (int i = Go; i < 4; i++)` to iterate through the array elements.
Q3. In the `grace_score` function, what condition triggers an increase in the score?
Explanation: The function is designed to give a grace score of 5 to elements that are strictly less than 40, as indicated by the condition `if (score[i] < 40)`.
Q4. What is the purpose of the `i++` within the `else if` block in the `repch` function?
Explanation: When two consecutive characters are identical (`s[i] == s[i + 1]`), `s[i+1]` is replaced, and `i++` is executed to advance the loop counter past the processed pair, effectively skipping the next character in the subsequent iteration.
Q5. In the `switchover` function, what operation is performed on array elements when `K` is less than `split`?
Explanation: The code `A[K] += K;` indicates that `K` is added to the element `A[K]` when the condition `K < split` is true.
Frequently asked questions
What is a one-dimensional array in C++?
A one-dimensional array is a linear data structure that stores a collection of elements of the same data type in contiguous memory locations. Elements are accessed using a single index.
How are elements accessed in a C++ array?
Elements in a C++ array are accessed using an index, which starts from 0. For an array named `arr` and an index `i`, the element is accessed as `arr[i]`.
What is the significance of the `size` parameter in the `grace_score` function?
The `size` parameter indicates the total number of elements in the array `score`. This is crucial for the loop to iterate through all elements correctly without going out of bounds.
How does the `repch` function modify the string `SUCCESS`?
The `repch` function replaces characters based on their position and relation to adjacent characters. It changes 'C' to '@' because it's at an odd index (1-based) and not followed by the same character. It changes 'E' to '!' because it's followed by the same character 'S' (after 'S' was potentially modified), and it skips the next character.
What is the difference between `A[K] += K;` and `A[K] *= K;` in the `switchover` function?
`A[K] += K;` adds the value of `K` to the current value of `A[K]`. `A[K] *= K;` multiplies the current value of `A[K]` by `K`.
How can these NCERT Solutions help in exam preparation?
These solutions provide clear, step-by-step explanations for common array-related problems, helping students understand the logic, practice C++ coding, and improve their ability to predict program outputs, which are common exam question types.
Content reviewed by the NCERT Help team. Editorial Team and update policy
NCERT Solutions PDF PDF on NCERT Help. URL unchanged for search indexing.