Plus One Computer Science Chapter Wise previous Questions Chapter 8 Arrays

Kerala Plus One Computer Science Chapter Wise Previous Questions Chapter 8 Arrays

Question 1.
int num[10]; (Sep – 2015 (Imp)
The above C++ statement declares an array named num that can store maximum integer numbers.
a) 9
b) 10
c) N
d) none of these
Answer:
b) 10

Question 2.
Declare a two dimensional array to store the elements of a matrix with order 3 x 5 (March – 2016)
Answer:
int m[3] [5]; or float m[3] [5]

Question 3.
……….. search method is an example for ‘divide and conquer method’. (Sep – 2016)
Answer:
Plus One Computer Science Chapter Wise previous Questions Chapter 8 Arrays 1

Question 4.
Find the value of score [4] based on the following declaration statement. (March – 2017)
Answer:
int score [5]={988,87,92,79,85};

Question 5.
Read the following C++ statement: (March – 2015)
int MAT [5] [4];
a) How many bytes will be allocated for this array?
b) Suppose MAT [4] [4] is a 2D array that contains the elements of a square matrix. Write C++ statements to find the’ sum of all the elements in the array.
Answer:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int MAT[4][4],i,j,SUM=0; 
cout<<“Enter 4*4=16 elements for matrix”; 
for(i=0;i<4;i++) 
for(j=0;j<4;j++)
{
cin>>MAT[i][j];
SUM=SUM+MAT[i][j];
}
cout<<“The matrix is given below\n”; 
for(i=0;i<4;i++)
{
for(j=0;j<4;j++) cout<<MAT[i][j]<<“\t”; 
cout<<endl;
}
cout<<“The sum of elements is “<<SUM; 
getchO;
}

Question 6.
Declare an array of size 5 and intitialize it with the numbers 8, 7, 2, 4 and 6. (March – 2016)
Answer:
int n [5] = {8,7,2,4,6};

Question 7.
a) Write C++ program for sorting a list of numbers using Bubble Sort Method. (Sep – 2016)
b) Write the different passes of sorting the following numbers using Bubble Sort.
Answer:
a) Example for Bubble sort is given below

#include<iostream> 
using namespace std; 
int main()
{
int i,j,n[5],t; 
for(i=0;i<5;i++)
{
cout<<“Enter number”<<i+1<<“:”; cin>>n(i];
}
for(i=0;i<4;i++)
for(j=0;j<4-i;j++)
if(n[il>n[j+1])
{
t=n[j];
n[j]=n[j+1 ];
n[j+1]=t;
}
cout<<“The sorted array is given below”;
for(i=0;i<5;i++)
coiit<<n[i]<<“,”;
}

b) Bubble sort (ascending order)
Consider the first 2 elements of the numbers and compare it
32 is greater than 21 then interchange both of them. Then the numbers are
21.32.9.17.5
Next compare 32 and 9. Here 32 is greater so interchange both of them.Then the numbers are 21,9,32,17,5.
Next compare 32 and 7.
Then interchange and the numbers are
21.9.17.32.5
Next compare 32 and 5
Then interchange and the numbers are
21.9.17.5.32.
After the first phase the largest number is on the right side.
Similarly do the remaining.
At last we will get the result as follows.
5.9.17.21.32.

Question 8.
Suppose M[5] [5] is a 2D array that contains the elements of a square matrix. Write C++ statements to find the sum of the diagonal elements. (March – 2017)
Answer:

#include<iostream> 
using namespace std; 
int main()
{
int mat[5][5],i,j,sum=0; 
cout<<“Enter 25 elements”; 
for(i=0;i<5;i++) 
for(j=0;j<5;j++)
{
cin>>mat[i][j];
if(i==j)
sum+=mat[i][j];
}
cout<<“The matrix is given beiow\n”; 
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
cout<<niat[i][j]<<“\t”;
cout<<“\n”;
}
cout<<“\nThe diagonal sum is ”<<sum;
}

Question 9.
Write the names of two searching methods in arrays. Prepare a chart that shows the comparisons of two searching methods. (March – 2015)
Answer:
It is the process of finding the position of the given element.

  • Linear search In this method each element of the array is compared with the element to be searched starting from the first element. If it finds the position of the element in the array is returned.
  • Binary searchIt uses a technique called divide and conquer method. It can be performed only on sorted arrays.

First we check the element with the middle element. There are 3 possibilities. The first possibility is the searched element is the middle element then search can be finished. The second possibility is the element is less than the middle value so the upper bound is the middle element. The third possibility is the element is greater than the middle value so the lower bound is the middle element. Repeat this process.

Question 10.
Write a C++ program to illustrate array traversal. (Say – 2015 (Imp))
Answer:

#include<iostream> 
using namespace std; 
int main()
{
int i, mark[50]; 
for(i=0;i<50; i++)
{
cout<<"Enter mark "<<i+1<<": cirt>>mark[i];
}
cout<<"\nThe mark of 50 students after adding bonus mark 10 is given below \n"; for(i=0;i<50; i++)
cout<<mark[i]+10<<endl;
}

Question 11.
Define an array. Also write an algorithm for searching an element in the array using any one method that you are familiar with. (March-2016)
Answer:
An array is a collection of elements with same data type. The index of first element is 0 (zero) and the index of last element is size -1. There are 2 methods linear search and binary search.

Searching
It is the process of finding the position of the given element.

  • Linear searchIn this method each element of the array is compared with the element to be searched starting from the first element. If it finds the position of the element in the array is returned.
  • Binary searchIt uses a technique called divide and conquer method. It can be performed only on sorted arrays.

First we check the element with the middle element. There are 3 possibilities. The first possibility is the searched element is the middle element then search can be finished. The second possibility is the element is less than the middle value so the upper bound is the middle element. The third possibility is the element is greater than the middle value so the lower bound is the middle element. Repeat this process.8.3 Two dimensional (2D) arrays.

Some occasions we have to store 6 different marks of 50 students. For this we use 2D arrays. An array with two subscripts is used, eg. int mark[r][c]; Here r is the row and c is the column.

Question 12.
Explain ‘Call by Value’ and ‘Call by Reference’ methods of function calling with the help of a suitable example. (Sep – 2016)
Answer:
Two types are call by value and call by reference.
1. Call by value : In call by value method the copy of the original value is passed to the function, if the function makes any change will not affect the original value.
Eg:

#include<iostream> 
using namespace std; 
void swap(int x,int y)
{
int temp; 
temp=x; 
x=y; 
y=temp;
}
int main()
{
int a,b;
cout<<“Enter 2 nos”; 
cin>>a>>b;
cout<<“The values before swap a=”<<a<<“ and b=”<<b;
swap(a,b);
cout<<“\nThe values after swap a=’’<<a<<“ and b=”<<b;
}

There is no change in the values.
2. Call by reference : In call by reference method the address of the original value is passed to the function, if the function makes any change will affect the original value.
Eg:

#include<iostream> 
using namespace std; 
void swap(int &x,int &y)
{
int temp; 
temp=x; 
x=y; 
y=temp;
}
int main()
{
int a,b;
cout<<“Enter 2 nos”; 
cin>>a>>b;
cout<<“The values before swap a=”<<a<<“ and b=”<<b;
swap(a,b);
cout<<“\nThe values after swap a=”<<a<<“ and , b=”<<b;
}
There is a change in the values.

Question 13.
What is an array? Write C++ program to declare and use a single dimensional array for storing the computer science marks of all students in your class. (Sep – 2016)
Answer:
An array is a collection of elements with same data type or with the same-name we can store many elements, the first or second or third etc can be distinguished by using the index(subscript). The first element’s index is 0, the second elements index is 1, and so on.

#include<iostream>
using namespace std; 
int main()
{
int marks[60],i;
for(i=0;i<60;i++)
{
cout<<“Enter mark of student”<<i+1<<“:”; cin>>marks[i];
}
cout<<“The marks are given below\n”;
for(i=0;i<60;i++)
cout<<marks[i]<<endl;
}

Question 14.
Write an algorithm for arranging elements of an array in ascending order using bubble sort. (March – 2017)
Answer:

#include<iostream> 
using namespace std; 
int main()
{
int i,j,n[5],t; 
for(i=0;i<5;i++)
{
cout<<“Enter number”<<i+1<<“:”; 
cin>>n[i];
}
for(i=0;i<4;i++)
for(j=0;j<4-i;j++) 
if(n{j]>n[j+1])
{
t=n[j];
n[j]=n[j+1];
n[j+1]=t;
}
cout<<“The sorted array is given below”; 
for(i=0;i<5;i++) cout<<n[i]<<“,”;
}

Plus One Computer Science Chapter Wise previous Questions