UP Board Solutions for Class 10 Computer Science Chapter 8 Subscripted Variables
UP Board Solutions for Class 10 Computer Science Chapter 8 Subscripted Variables
Subscripted Variables Long Answer Type Questions (8 Marks)
Question 1.
What are subscripted variables? What is an Array? Explain. (UP 2007, 19)
Or
What is an Array? What are its different types? Explain. (UP 2009, 12)
Or
Explain the subscripted variable. (UP 2017)
Answer:
Subscript: A subscript may be a numeric constant or an integer-valued variable, is enclosed in parenthesis which identifies the position of a given element in the array. For example, the first element of array stu_age (containing the value 14) is referred to as stu_age [0].
Similarly,
stu_age [1] = 16
stu_age [2] = 18
stu_age [3] = 17
stu_age [4] = 15
Here, 0, 1, 2, 3, 4 within parenthesis are known as subscript. In ‘C’ first element is in the Oth place and last element in the (n -l)th place in the array of n elements.
Subscripted Variable: A variable with a subscript is known as subscripted variable and it refers to one value in the array, e.g., stu_age [0], stu_age [1], etc.
Declaration of an Array: As you know that all variables in ‘C’ must be declared before use, hence, we must declare the array. It is done at the beginning of the function main () with data type and size.
Syntax:
datatype name [size];
Example:
int stu_age [5];
This statement states that stu_age is an array of 5 integers (Number of elements). This statement reserves enough memory for stu_age to hole 5 integers. Once the declaration is made, stu_age [0] refers to the first integer in the array, stu_age [1] refers to the second integer in the array and so on.
Question 2.
(a) Explain the following with reference to ‘C’ language: (U. P. 2006)
(i) Data Type
(ii) Variables.
(b) What are the various arithmetic operators available in ‘C’ language? Explain with example.
Answer:
(a) (i) Data Type: A data type decides the amount of memory to be allocated to a variable to store a particular type of data. To allocate memory for a particular piece of data, we have to declare a variable of a particular data type. The variable declaration means that some memory is allocated and that portion of memory will be referred to by the variable name.
Data types, which are commonly used in programming tools can be classified as :
- Numeric: Stores numeric value.
- Alpha Numeric: Stores descriptive information.
A numeric data type is called int: Similarly, an alpha-numeric data type is named char in C.
(ii) Variables: A variable is a name that represents a number or a string. Each numeric variable must consist of a letter or a letter followed by an integer. All variables that are to be used in the program must be declared prior to its use. Declaring a variable means defining a name and a data type of a variable. Every variable declaration is terminated by a semicolon. Variable names must begin with an alphabet. The first character may be followed by a sequence of letters or digits and can also include the special character ‘Underscore’.
(b) Arithmetical Operators: Operators that perform mathematical operations are called Arithmetical operators. These operators form a mathematical expression using operands along- with. There are five types of arithmetical operands available in C++ which are as follows:
(i) Add (+): To perform addition of operands. These operands can either be literal, identifier or combination of both.
e.g.:
a + b
2 + 2
x + 2
(ii) Subtract (-): In order to perform subtraction, operands can be literal, identifier or combination of both.
e.g.:
x – y
15 – 5
A – 10
(iii) Multiply (*): This operator performs multiplication on operands.
e.g.:
y * z
15 * 32
A * 25
(iv) Divide (/): It is used to perform division of the two operands.
e.g.
A/B
25/5
X/2
(v) Exponentiation (%): It is a modulus operator and returns the remainder of the division. A / operator returns the quotient after the division of operands and a modulus operator returns the remainder after division.
e.g.
10 % 3
X % 2
Question 3.
What do you mean by sorting? Define two methods of sorting. (UP 2004, 05, 11)
Or
Give names of the main methods of sorting and explain any one method with an example. (UP 2014, 15)
Answer:
Sorting: Sorting is the process of arranging the data or information in some logical order. This logical order may be ascending or descending in case of numeric values or dictionary order in case of alphanumeric values.
For example: Suppose an unsorted array X of ten numeric values.
There are some methods of sorting the data of an array.
- Insertion sorting
- Bubble sorting
- Selection sorting.
Insertion Sorting: This process uses only one array and requires (N – 1) passes and total comparison required are N (N – l)/2 to sort this array. The processing of sorting in ascending order is as follows:
Suppose an unsorted array A is
This array has 6 elements, so it will complete the sorting in 5 passes (N – 1 ⇒ 6 – 1 = 5)
In this pass, the first element of an array should be compared with the rest of the array elements and placed the smallest element on top in ascending order.
Suppose an unsorted array A is
Now you will sort this array using bubble sort method. In this method also, sorting requires (N – 1) passes to sort the elements of the array.
Question 4.
What is searching? Explain its mechanism. (UP 2007, 11, 15)
Or
What is searching? Explain two searching techniques. (UP 2009, 15)
Answer:
Searching: It refers to the process of finding the location of the given data element from a collection of data. The search is said to be successful if the given data element is found.
There are two searching techniques :
- Linear Search
- Binary Search.
1. Linear Search: It is also known as sequential search. In sequential searching, a particular data item is searched sequentially, Le., the desired data first compared with the first element then the second element of an array, and so on.
Example: WAP in ‘C’ to find the given number in the given array of 10 numeric type values:
Program :
# include<stdio.h> void main() { int A [10] = {10, 50, 6, 7, 20, 30, 15, 25, 80, 90}, int i, N; printf (“Input the Number”); scanf(“%d”, &N); for (i = 0; i < 10; i ++) { if (N = = A[i]) printf (“%d is present in list on %d position”, N, i); break; } printf (“N is not present”); }
2. Binary Search: In binary searching, the arranged data is divided into two parts. The particular data item to be searched is compared with the middle data item. If it is less than the middle data, then it means that the data item to be searched is present in the first half of the data item set. The loop will be created for only half of the total value, thus, it saves the computer time also. In a binary search, it is mandatory that the data be arranged in either ascending or descending order.
Example: Given a sorted (ascending order) array A with elements and we want to search the element 15 in it by binary search.
Binary Search Method:
Given Array A
To start with, we take BEG = 0, END = 6, and compute location of the middle element as MID = (BEG + END)/2 = (0 + 6)/2 = 3.
Since, A[MID] le., A[3] ≠ 15, and BEG < END. We start next iteration. As A[MID] = 20 > 15, therefore, we take END = MID -1=3-1=2, where BEG remains unchanged.
MID = (BEG + END)/2 = (0 + 2)/2 = 1
Since, A[MID] le., A[1] ≠ 15, and BEG < END.
We start the next iteration.
As A[MID] = 10 < 15, therefore, we take BEG = MID + 1 = 1 + 1 = 2, where END remains unchanged; since BEG = END, again compute location of the middle element as
MID = (BEG + END)/2 = (2 + 2)/2 = 2
Since A[MID] le., A[2] = 15, the search terminates with success.
Question 5.
What do you mean by Array? Define different types of array. (UP 2008, 12, 15, 19)
Or
What do you understand by Array? What are its different types? Describe each of them. (UP 2011)
Or
What do you understand by Array? Discuss one dimensional and two-dimensional array with example. (UP 2017)
Answer:
An array is the collection of numeric or string values in the form of LIST (single row) or TABLE (rectangular arrangements in the form of rows and columns). A single variable name is used to refer to the entire collection of items. ‘C’ language permits us to deal with many related data items as a group by means of the structure known as an array.
An array can be used to store integer, real or string values, but the values in a given array must be of the same type. That is, if an array is given a numeric variable name, a string data item cannot be entered to it, only numeric values are allowed. For example, suppose that there are five test scores 15, 25, 35, 81, 75 to be stored. The scores could be put in an array called TEST. This array can be represented in memory as such.
There are two types of array.
1. Single-dimensional array: It is a collection of numeric or string values in the form of a list. This is also known as single subscript variables because these variables have one subscript only e.g. int a[10].
2. Multi-dimensional array: It is defined in the same manner as a single-dimensional array, except that a separate pair of square brackets are required for each subscript. It is the collection of numeric or string values in the form of TABLE
e.g. A[5][6], B[2][3][2] etc.
Question 6.
WAP to sort the elements of an array in ascending order. (UP 2006)
Answer:
#include<stdio.h> void main() { int A[100]; int n, i, j, temp; printf (“Enter how many nos”); scant (“%d”, &n); for (i = 0; i < n; i++) { scanf (“%d”, &A[i]); } for (i =0; i < n - 1; i++) { for (j = 0; j < n + i; j++) { if (A [j +1] < A[j]) temp = A[j]; A[j] =A[j + 1]; A[j + 1] = temp; }} for (i = 0; i < n; i++) { printf (“%d”, A[i]); } }
Question 7.
WAP to search the given number in the given array of 10 numeric type values:
Answer:
#include<stdio.h> void main () { int A[10] = {10, 50, 20, 30, 6, 7, 8, 70, 90, 75}; int i, n; printf (“input the number”); scanf (“%d”, &n); for (i = 0; i < 10; i++) { if (n == A[i]) printf (“%d is present in list on %d position”, n, i); break; } printf (“%d is not present”, n) }
Subscripted Variables Short Answer Type Questions (4 Marks)
Question 1.
What do you mean by subscript? (UP 2012)
Answer:
To gain access to a single element within the array, a subscript is used. A subscript may be a numeric constant or an integer-valued variable, is enclosed in parentheses which identify the position of a given element in the array. For example, the first element of array TEST (containing the value 15) is referred to as TEST(0J. The second test score is as TEST[1], the third test score is as TEST[2], and so on. Therefore, the following statements are true:
TEST[0] = 15
TEST[1] = 25
TEST[2] = 5
TEST[3] = 81
TEST[4] = 75
Here, 0, 1, 2, 3 and 4 within parentheses are known as a subscript.
Question 2.
Define the term List and Table.
Answer:
An array is a collection of numeric or string values in the form of LIST or TABLE.
LIST: List is a kind of arrangement in which values are arranged either in horizontal form or vertical form in a sequential row.
e.g.,
TABLE: Table is a rectangular arrangement of values in the form of rows and columns.
e.g.,
Question 3.
WAP to find the factorial of ‘n’.
Answer:
#include<stdio.h> void main() { int n, i, fact = 1; printf (“Enter the value for n”); scanf (“%d”, &n); for (i = 1; i <= n; i++) { fact = fact * i; } printf (“factorial of %d is %d”, n, fact); }
Question 4.
What is Binary Search? (UP 2011, 19)
Answer:
In this method of searching, only sorted data can be used. Unsorted data cannot be used to apply this type of search. To do searching in Binary method, the sorted data is divided into two parts and the particular element is compared with the middle element. If found greater, the first and middle element is divided into two parts and their middle element is compared. And if found smaller, the middle and the last element is divided into two parts and their middle element is compared. This process goes on until the search is completed. This method of search is quick and saves time. It is also known as Random search.
Question 5.
Write a short note on sorting? (UP 2008, 11, 12)
Answer:
It is easy to carry out different operations on the idea if it is arranged or in some order (ascending or descending). The process of arranging the data or information in some order (ascending or descending in case of numeric data and dictionary order in case of alphanumeric data) is called sorting.
Example:
Question 6.
Write a short note on searching? (UP 2008)
Or
Differentiate between sequential and binary search. (UP 2014)
Answer:
Searching: Searching means finding an element from an array. It refers to the process of finding the location of a given data element from an array. For example: If you want to work on your computer notebook, you have to search for it for yourself. You can do searching by two methods:
1. Sequential Search: This search is also known as linear search because in this method to search an element from data source one has to compare particular data item with the first element than the second element and so on until the search is completed. Because of this sequential approach, it is known as sequential search. In this, if the element is found in the middle, search process terminates.
2. Binary Search: In this method of searching, only sorted data can be used, unsorted data cannot be used to apply this type of search. To do searching in Binary method, the sorted data is divided into two parts and the particular element is compared with the middle element. If found greater, the first and that middle element is divided into two parts and their middle element is compared. And if found smaller, the middle and the last element is divided into two parts and their middle element is compared. This process goes on until the search is completed. This method of search is quick and saves time. It is also known as Random search.
Subscripted Variables Very Short Answer Type Questions (2 Marks)
Question 1.
From which element binary search starts searching in an array?
Answer:
From the middle element, the binary search starts searching in an array.
Question 2.
What do you mean by sorting? (UP 2012)
Answer:
Sorting is the process of arranging the data in some order.
Question 3.
Is an array a collection of similar elements?
Answer:
Yes, an array is a collection of similar elements.
Question 4.
What is the number of the first element in an array?
Answer:
The first element in the array is numbered 0.
Question 5.
What are the different methods of sorting?
Answer:
The different methods of sorting are:
- Insertion
- Bubble
- Selection.
Question 6.
Arranging the data in some order is known as:
Answer:
Sorting.
Question 7.
Name two types of searching.
Answer:
Sequential and Binary.
Subscripted Variables Objective Type Questions (1 Marks)
There are four alternative answers for each part of the questions. Select the correct one and write in your answer book:
Question 1.
A double dimensional array is a collection of a numeric value in the form of:
(a) TABLE
(b) WAP
(c) SORT
(d) None of these.
Answer:
(a) TABLE
Question 2.
Which of the following is not a character constant?
(a) Sorry
(b) Enter values of A, B, C.
(c) 123.50
(d) All of the above.
Answer:
(d) All of the above.
Question 3.
If x is an integer variable, x = will return a value:
(a) 2.5
(b) 3
(c) 2
(d) 1
Answer:
(b) 3
Question 4.
The maximum width of a ‘C’ variable name can be:
(a) 5 characters
(b) 8 characters
(c) 10 characters
(d) 20 characters.
Answer:
(b) 8 characters