Kerala Plus One Computer Science Chapter Wise Questions and Answers Chapter 8 Arrays
Very Short Answer Type Questions
(Score 1)
Question 1.
Consider an array, int num[1oo]. The index of last element is
a. num[o]
b. num[100]
c. num[99]
d. num[101]
Answer:
c. num[99]
Question 2.
Which statements are used to read and display an array?
a. loop statement
b. if statement
c. switch statement
d. if else statement
Answer:
a. loop statement
Question 3.
subscripts are there in a two dimensional array,
a. 1
b. 2
c. 4
d. o
Answer:
b. 2
Question 4.
In C+ + , the array index starts with
a. 1
b. 2
c. o
d. 4
Answer:
c. 0
Question 5.
The process of finding the smallest element and exchanging it with the element at the respective position is called
Answer:
pass
Question 6.
Linear search is also known as
Answer:
sequential search
Question 7.
When a multidimensional array is accessed, each array index is
a. separated by column
b. surrounded by brackets
c. surrounded by brackets and separated by commas
d. none of these
Answer:
b. surrounded by brackets
Question 8.
Which of the following array declaration is invalid?
a. float a[-35]
b. int n [3]
c. char namets]
d. int s[1o];
Answer:
a. float a[-35];
Question 9.
From the following which is not true for an array.
a. array uses a compact memory structure.
b. readability of program will be increased.
c. it is easy to represent and manipulate array variable.
d. array elements are dissimilar elements.
Answer:
d. array elements are dissimilar elements.
Question 10.
………. is a simple sorting algorithm.
a. linear search
b. bubble sort
c. selection sort
d.binary search
Answer:
c. selection sort
Question 11.
searching is slower for larger array.
Answer:
linear searching
Short Answer Type Questions
(Score 2)
Question 1.
Write C++ code segment to input values into the array: int ar[50];
Answer:
for (int i=o; i<50; i++)
{
cin> > ar [i];
}
Question 2.
Write C++ code fragment to display the elements in the even positions of the array: float val[1oo];
Answer:
for (int i=o; i<1oo; i+=2)
{
cout<<val [i];
}
Short Answer Type Questions
(Score 3)
Question 1.
Write array declarations for the following:
a. Scores of 100 students
b. English letters
c. A list of 10 years
d. A list of 30 real numbers
Ans:
a. int score [1oo];
b. char ch [26];
c. int yr[1o];
d. float num [30]
Question 2.
What are differences between linear search method and binary search method?
Answer:
Linear search method:
- The elements need not be in any order.
- Takes more time for the process.
- May need to visit all the elements.
- Suitable when the array is small.
Binary search method:
- The elements should be in sorted order.
- Takes very less time for the process.
- All the elements are never visited.
- Suitable when the array is large.
Question 3.
Write array initialization statements for the following:
a. An array of 10 scores: 89, 75, 82, 93, 78, 95, 81, 88, 77, and 82
b. A list of five amounts: 10.62, 13.98, 18.45, 12.68, and 14.76
c. An array of 10 marks with value o.
d. An array with the letters of VIBGYOR.
e. An array with number of days in each month.
Ans:
a. int mark [10] = {89, 75, 82, 93, 78, 95, 81, 88, 77, 82};
b. float amtfs] = { 10.62, 13.98, 18.45, 12.68, 14.76};
c. int mk[10] = {0};
d. char cr[7]= {‘V’, T, ‘B\ ‘G’, T, ‘O’, ‘R’};
e. int month[12]=(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Long Answer Type Questions
(Score 5)
Question 1.
Write a program to input the marks of 10 students and display them in reverse order.
Answer:
#include <iostream> using namespace std; int main( ) { int i, score[10]; for(i=o; i< 10; i++) { cout<<“Enter the mark:"; } for(i=9; i>=o; i- -) cout<<“Mark of student” <<i << “ is” << score[i]<<endl; return 0; }
Question 2.
Write a program that shows traversal of an array.
Answer:
#include <iostream> using namespace std; int main() { int a[10], i; cout<<“Enter the elements of the array for(i=o; i<to; i++) cin >> a[i]; for(i=o; i<io; i++) a[i] = a[i] + 1; cout<<“\nEntered elements of the array are:\n”; for(i=o; i<10; i+ + ) cout<< a[i]<< “\t”; return o; }
Question 3.
Program for arranging elements in ascending order using selection sort.
Answer:
#include <iostream> using namespace std; int main() { int AR[25], N, I, J, MIN, POS; cout<<“How many elements?”; cin>>N; cout<<“Enter the array elements: ”; for(I=o; I<N; I++) cin>>AR[I]; for(I=o; I < N-1; i++) { MIN=AR[I]; P0S=I; for(J = I+1; J < N; J++) if(AR[J]<MIN) { MIN=AR[J]; P0S=J; } if(P0S != I) { AR[P0S]=AR[I]; AR[I]=MIN; } } cout<<“Sorted array is:”; for(I=o; I<N; I++) cout<<AR[I]<<“\t”; return 0; }
Question 4.
Program for arranging elements in ascending order using bubble sort.
Answer:
#include <iostream> using namespace atd; int main() { int AR[25],N; int I, J, TEMP; cout<<“How many elements? ”; cin>>N; cout<<“Enter the array elements: ”; for(I=o; I<N; I++) cin>>AR[I]; for(I=1; I<N; I++) for(J=o; J<N-I; J++) ) if(AR[J] > AR[J+1]) > { TEMP = AR[J]; AR[J] = AR[J+i]; . AR[J+t] = TEMP; } cout<<T‘Sorted array is: ”; for(I=o; I<N; I++) cout<<AR[I]<<“\t”; }
Question 5.
Write a linear search program to find an item in the array.
Answer:
#include <iostream> using namespace std; int main( ) ; { int AR[25], N; int I, ITEM, L0C=-i; cout<<“How many elements? ”; cin>>N; cout<<“Enter the array elements: ”; for(I=0; I<n; I++) cin>>AR[I]; cout<<“Enter the item you are searching for: cin>>ITEM; for(I=o; I<N; I++) if(AR[I] == ITEM) { LOC=I; : break; if(LOC! = -i) cout<<“The item is found at position” <<LOC+i; else cout<<“The item is not found in the array”; return o; }
Question 6.
Binary search to find an item in the sorted array.
Answer:
#include <iostream> using namespace std; int main() { int LIST[25],MAX; int FIRST, LAST, MIDDLE, I, ITEM, LOC=-i; cout<<“How many elements? ”; cin>>MAX; cout<<“Enter array elements in ascending order: ”; for(I=o; I<MAX; I++) cin>>LIST[I]; cout<<“Enter the item to be searched”; cin>>ITEM; FIRST=o; LAST=MAX-1; while(FIRST < = LAST) { MIDDLE=(FIRST+LAST)/2; if(ITEM == LIST[MIDDLE]) { LOC = MIDDLE; break; } if(ITEM < LIST[MIDDLE]) LAST = MIDDLE-1; else FIRST = MIDDLE+i; } if(LOC != -l) cout<<“The item is found at position” <<LOC + 1; else cout<<“The item is not found in the array”; return o; }
Question 7.
Write a C++ program to find the sum of major diagonal elements of a matrix.
Answer:
#include <iostream> using namespace std; int main() { int mat[10][10], n, i, j, s=o; cout<<”Enter the rows/columns of square matrix: ”; cin> >n; cout<<“Enter the elements\n”; for(i=o; i<n; i++) for(j=o; j<n; j ++) cin>>mat[i][j]; cout<<“Major diagonal elements are\n”; for(i=o; i<n; i++) { cout<<mat[i][i]<<“\t”; s = s + mat[i][i]; } cout<<“\nSum of major diagonal elements is: ”; cout<<s; return o; }
Question 8.
Program to find the transpose of a matrix.
Answer:
#include <iostream> using namespace std; int main() { int ar[10][10], m, n, row, col; cout<<“Enter the order of matrix: ”; cin> >m> >n; cout<<“Enter the elements\n”; for(row=o; row<m; row++) for(col=o; col<n; col++) cin>>ar[row][col]; cout<<“Original matrix is\n”; for(row=o; row<m; row++) { cout<<“\n”; for(col=o; col<n; col++) cout< <ar[row][col] <<“\t”; } cout<<“\nTranspose of the entered matrix is\n”; for(row=o; row<n; row++) { cout<<“\n”; for(col=o; col<m; col++) cout< <ar[col][row]< <“\t”; } return o; }
Question 9.
Program to find the row sum and column sum of a matrix.
Answer:
#include <iostream> using namespace std; int main( ) { int ar[10][10], rsum[10] = {o}, csum [10]={o}; int m, n, row, col; cout<<“Enter the number of rows & columns in the array: cin> >m> >n; cout<<“Enter the elements\n”; for(row=o; row<m; row++) for(col=o; cokn; col+ + ) cin>>ar[row][col]; for(row=o; row<m; row++) for(col=o; cokn; col++) rsumfrow] += ar[row][col]; csumfcol] += ar[row][col]; } cout<<“Row sum of the 2D array is\n”; for(row=o; row<m; row++) cout< <rsum[row]< <“\t”; cout<<“\nColumn sum of the 2D array is\n”; for(col=o; cokn; col++) cout< < csum [col] < <“\t”; return o; }
Textual Questions & Answers
Question 1.
All the elements in an array must be data type. (1)
Answer:
same
Question 2.
The elements of an array with ten elements are numbered from to (1)
Answer:
o to 9
Question 3.
An array element is accessed using (l)
Answer:
subscript/index
Question 4.
If AR is an array, which element will be referenced using AR[7]?(1)
Answer:
Eighth element.
Question 5.
Consider the array declaration, int a[3]={2,3,4>; What is the value of a[1]? (l)
Answer:
3
Question 6.
Consider the array declaration, int a[ ]={i,2,4>; What is the value of a[1]? (1)
Answer:
2
Question 7.
Consider the array declaration, int a[5]={1,2,4>; What is the value of a[4]? (1)
Answer:
o
Question 8.
Write array initialization statements for an array of 6 scores: 89, 75, 82, 93, 78, 95. (1)
Answer:
int score[6]= {89, 75, 82, 93, 78, 95}
Question 9.
Printing all the elements of an array is an example for operation. (1)
Answer:
Traversal
Question 10.
How many bytes are required to store the array int a[2][3]; ? (1)
Answer:
2 x 2 x 3 = 12 bytes
Question 11.
In an array of m elements, Binary search required maximum of n search for finding an element. How many search required if the number of elements is doubled? (1)
Answer:
n + 1
Question 12.
Write the statement to initialize an array of 10 marks with value o. (1)
Answer:
int mark [10] = {o};
Question 13.
State True or false:
The compiler will complain if you i try to access array element 16 in ; a ten element array. (1)
Answer:
False
Question 14.
Define an array. (1)
Answer:
An array is a collection of elements of the same type placed in contiguous I memory locations.
Question 15.
What does the declaration int studlist [1000]; mean? (1)
Answer:
It is an integer type array with name studlist which can hold 1000 elements. The subscript starting from studlist[o] to studlist[999],
Question 16.
How is memory allocated for a single dimensional array? (1)
Answer:
For a single dimensional array, the total size allocated can be computed as, total_bytes = sizeof(array_type)x size_of_ array
Question 17.
Write C++ statements to accept an array of 10 elements and display the count of even and odd numbers in it. (3)
Answer:
#include<iostream.h> void main() { int ar [10], ev = 0, od = 0; for (int i=o; i<io; i++) { cin> >ar[i]; if (ar[i]%2==o) ev+ + ; else od+ + ; } cout<<“Even count is”<<ev; cout<<“Odd count is”<<od; }
Question 18.
Write the initialization statement for an array num with values 2, 3,4,5. (1)
Answer:
int num[4] = {2, 3, 4, 5};
Question 19.
What is meant by traversal? (1)
Answer:
Accessing each element of the array atleast once is called traversal.
Question 20.
Define sorting. (1)
Answer:
Sorting is the process of arranging the elements of the array in some logical order.
Question 21.
What is searching? (1)
Answer:
It is the process of finding the location of the given element in the array.
Question 22.
What is bubble sort? (2)
Answer:
Bubble sort is a sorting algorithm that works by repeatedly stepping through lists that need to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. This passing procedure is repeated until no swaps are required, indicating that the list is sorted.
Question 23.
What is binary search? (2)
Answer:
Binary search is an algorithm which uses minimum number of searches for locating the position of an element in a sorted list, by checking the middle, eliminating half of the list from consideration, and then performing the search on the remaining half.
Question 24.
Define a 2D array. (1)
Answer:
A two dimensional array in an one dimensional array in which each element itself is an array.
Question 25.
How is memory allocated for a 2D array? (2)
Answer:
The amount of storage required to hold a two dimensional array depends upon its base type, number of rows and number of columns. The formula to calculate total number of bytes reqired for a 2D array is, total_bytes = sizeof(base type) x number of rows number of columns.
Question 26.
An array AR contains the elements 25, 81, 36, 15, 45, 58, 70. Illustrate the working of binary search technique for searching an element 45.
Answer:
MiddIe=(First+last)/2=(o+6)/2=3,
List[3] 45 First=middle +1=3+1 =4,
Last = 6
Middle=(First+last)/2 = (4+6)/2=5,
Here LISTfs] is not equal to 45 and LIST[5] is greater than the search element therefore,
we take First=4,
Last=middle -1=5-1 =4,
Middle=(First+last)/2=(4+4)/2=4,
Here LIST[4] is equal to 45 and the search terminate successfully.
Question 27.
Write C++ statements to accept two single dimensional array of equal length and find the difference between corresponding elements. (5)
Answer:
# include <iostream.h> #include<conio.h> void main { clrscrf); int a[1o], b[10], c[10]; int i, n; cout<<“Enter number of elements”; cin> >n; cout<<“Enter the elements of first array”; for (i=o; i<n; i++) { cin> > a[i]; } cout<<“Enter the elements of second array”; for (i=o; i<n; i++) { cin > >b [i]; } cout<<“Difference of the corresponding elements array”; for (i=o; i<n; i++) { c[i] = a[i] - b[i]; cout<<c[i]<<“\n”; } }
Question 28.
Write a program to find the difference between two matrices. (5)
Answer:
#include <iostream> #include <cstdlib> using namespace std; int main( ) { int m1, n1, m2, n2, row, col; int A[10][10], B[10][10], C[10][10];
cout<<“Enter the order of first matrix:”; cin> >m1> >n1; cout<<“Enter the order of second matrix:”; cin>>m2>>n2; if(m1!=m2 11 n1!=n2) { cout<<“Addition is not possible”; exit(o); } cout<<“Enter the elements of first matrix\n”; for (row=o; row<m1; row++) { for (col=o; cokn1; col++) cin>>A[row][col]; } cout<<“Enter the elements of second for (row=o; row<m2; row++) { for (col=o; col<n2; col++) cin>>B[row][col]; } for (row=o; row<m1; row+a-) { for (col=o; col<n1; col++) C[row][col] = A[row][col] + B[row][col]; } cout<<“Sum of the matrices:\n”; for(row=o; rowcm1; row++) { cout<<endl; for (col=0; col<m; col++) cout<<C[row][col]<<“\t”; } }
Question 29.
Write a program to find the sum and average of elements in a 2D array. (5)
Answer:
#include <iostream> #include <cstdlib> using namespace std; int main ( ) { int m, n, i, j, s = o, A[10][10]; float avg; cout<<“Enter the order of matrix:”; cin>>m>>n; . cout<<“Enter the elements of matrix\n”; for (i=o; i<m; i++) { for (j=0; j<n; j++) cin> >A[i][j]; } for (i=o; i<m; i++) { ' for (j=o; j<n; j ++) s = s + A[i][j]; . } avg = s/(m*n); cout<<“Sum of the elements of matrix:”<<s; cont<<“Average of the elements of matrix:”<<avg; }
Question 30.
Write a program to find the largest element in a 2D array. (5)
Answer:
#include <iostream> #include <cstdlib> using namespace std; int mainQ { int m, n, i, j, 1, A[10][10]; cout<<“Enter the order of matrix:”; cin> >m> >n; cout<<“Enter the elements of matrix\n”; for (i=o; i<m; i++) { for (j=o; j<n; j++) cin> >A[i][j]; } I= A[o][o]; for (i=0; i<m; i++) { for (j=o; j<n; j++) if(l<A[i][j]) 1 - A[i][j]; } cout<<“Largest element in the array is :”<<I; }
Leave a Reply