CBSE Coding Class 8 Solution – Programming with Arrays
CBSE Coding Class 8 Solution Student Handbook Chapter 4 Programming with Arrays all Questions and Answers Solution by Coding Teacher. Net Ex. Here provided all Coding Solution for Class 8.
(1) Which statement from below best describes Arrays?
Answer: Container of objects of similar data types
Reason: Arrays are collection of similar data type variables.
(2) Which of the following are advantages of arrays?
Answer: Easier to store elements of similar data type
Reason: you can make an Array of Integers as well as another Array of Strings. However, you cannot make an Array having Integer and Strings in same Collection.
(3) Which of the following are disadvantages of arrays?
Answer: There are chances of wastage of memory space if elements inserted in an array are lesser than the allocated size
Reason: Index value of an array can’t be negative neither we have to sequentially access elements in arrays.
Standard Questions
(1) What are arrays in programming?
Answer: Arrays are collection of similar data type variables. Array can store multiples values which can be referenced by single name. Arrays are always stores in specific memory location. Arrays do not support different data types in same collection. Arrays improve readability of code by using a single variable for a large set of data.
For Example: you can make an Array of Integers as well as another Array of Strings. You cannot make an Array having Integer and Strings in same Collection.
Ex: Color = [“red”, “blue”, “green”]
(2) Explain how arrays are indexed in programming.
Answer: The variables in an Array are always ordered sequentially with index starting with 0.
Example: Array with variables of Integer Data Type stored in it.
10 | 20 | 30 | 40 | 50 | 60 |
0 | 1 | 2 | 3 | 4 | 5 |
Here, Length of Array = 6
First Index = 0
Last Index = 5
(3) Explain how you can sort an array {67, 23, 98, 19} using Python?
Answer: Python has inbuilt sort function to order an array. The sort() method sorts the list ascending by default.
Numbers = [67, 23, 98, 19]
Numbers.Sort()
Print (Numbers)
Output: [19, 23, 67, 98]
(4) How do you search a particular value from an array in Python?
Answer: Python uses index method to search for an element in an array. For Example –
X = [34,45,3,23,22,78,65]
Print(X.index(23))
If you run the above code, you would get 3 as output. As 23 is located on index 3.
Higher Order Thinking Skills (HOTS)
(1) Make an array of animals (10 animals in the array) and use for loop which runs as many times as the length of the array to drop horses from the sky.
Answer:
(2) Make an array of animals (10 animals in the array) and use for loop which runs as many times as the length of the array to drop animal present on the 4th position (Tip: The 4th position will be indexed as 3) from the sky
Answer:
Applied Project
(1) Problem Statement: There is a robot which can read the numbers written on the cards only when kept close to its eyes. It can pick only one card containing numbers in its left hand and another in its right hand. Can you help it to arrange the cards in ascending order using bubble sort? Let’s try it on the numbers 1, 5, 4, 3, 2.
Answer: The bubble sort algorithm works as follows
Step 1) Get the total number of items in the given list.
Step 2) Create a function to sort the array and pass the list as argument. And declare and initialize variable Flag to 0 to check if the statement becomes false.
Step 3) Calculate the number of list and run the outer loop till length of list minus one.
Step 4) Perform inner passes n-1 times for outer pass. Get the first element value and compare it with the second value. If the second value is less than the first value, then swap the positions.
Step 5) Repeat step 3 passes until you reach the outer pass (n – 1). Get the next element in the list then repeat the process that was performed in step 3 until all the values have been placed in their correct ascending order. And change the value of flag to 1.
Step 6) Check the value of Flag if changed, if so then break the loop .
Step 7) Return the result when all passes have been done. Return the results of the sorted list.
Step 8) Optimize Algorithm.
defbubbleSort(array):
n = len(array)
for i in range( n – 1 ) :
flag = 0
for j in range(n – 1) :
if array[j] > array[j + 1] :
tmp = array[j]
array [j] = array [j + 1]
array [j + 1] = tmp
flag = 1
if flag == 0:
break
return array
Number= [1, 5, 4, 3, 2]
result = bubbleSort(Number)
print (result)