




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Exercises Sorting and Searching in the Array and DDA Solved question on Insertion sort
Typology: Assignments
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Question 1 A class Sorter contains an array of n integers. The subscripts of the array elements vary from 0 to n- 1. Some of the members of the class Sorter are given below: Class name : Sorter Data members/instance variables : arr[ ] : integer array to store elements. n : size of the array. Member functions/methods : Sorter( int nn ) : parameterized constructor to assign n = nn and create the array. void Accept( ) : to input n integers into the array arr[ ]. void InsSort( ) : sorts the array in ascending order using Insertion sort method. void showArray( ) : displays n integers. Specify the class Sorter giving details of the constructor and functions void Accept( ) , void InsSort( ) and void showArray( ). Write a main( ) function to create an object and call the functions accordingly to input the array, print the array before and after sorting. Solution:
Question 1 A class Search contains an array of 100 integers. Some of the members of the class are given below: Class name : Search Data members/instance variables : arr[ ] : to store 100 integers in the array. num : integer to store a number to search. Member functions/methods : Search( ) : default constructor. void readlist( ) : to input 100 integers into the array. void dispList( ) : to display integers present in the array. void LinSearch( ) : to accept an integer in num , using the Linear search technique search num from the array, display the number along with its index/subscript if found in the list otherwise display โ Search number missing โ. (a) Specify the class Search giving the details of the constructor and functions void readlist( ) , void dispList( ) and void LinSearch( ). Define a main( ) method to create an object and call the methods accordingly to enable the task. (b) State the difference between sorting and searching. Question 2 A class MySearch contains an array of โnโ characters. Some of the members of the class are given below: Class name : MySearch Data members/instance variables : n : integer to store the maximum size of the array. chr[ ] : character array to store characters. Member functions/methods : MySearch( int nm ) : constructor to initialize data member n = nm and creates the array of size โ n โ. void AcceptList( ) : to input and store โ n โ characters into the array chr[ ]. int LinSearch( char ele) : using the Linear search technique search argument ele from the array and return the index/subscript if found in the list otherwise returns - 1 if not found. void disp( ) : to display all the characters horizontally with at least one space between them and by invoking function int LinSearch(โฆ) display the index number/subscript along with the character if found otherwise display โ Search Unsuccessful โ.. Specify the class MySearch giving the details of the constructor and functions void AcceptList( ) , int LinSearch( int ) and void disp( ). Define a main( ) method to create an object and call the methods accordingly to enable the task.
Question 5 A class Search contains roll numbers and total marks obtained in an examination in two different arrays. The roll numbers are sorted in ascending order along with total marks. Some of the members of the class are given below: Class name : Search Data members/instance variables : roll[ ] : to store roll numbers in the array. total[ ] : to store total marks obtained. max : integer to store the maximum size of the array. rolnum : integer to store the roll number. Member functions/methods : Search( int nn ) : constructor to initialize data member max = nn and create both the arrays. void FillInfo( ) : to input and store roll numbers in roll[ ] and total marks in total[ ] of max size (assume that the roll numbers are sorted in ascending order along with total marks). void Show( ) : displays the sorted list containing roll numbers and total marks in the format given below: Roll Number Total Marks xxxx xxxx xxxx xxxx xxxx xxxx void BinSearch( ) : accept roll number ( rolnum ) and search it using the Binary search technique from the sorted array of roll[ ] and displays the roll number and total marks if found in the sorted list otherwise if not found, then displays โSearch roll number is not listedโ. Specify the class Search giving the details of the constructor and functions void FillInfo( ) , void Show( ) and void BinSearch( ). Define a main( ) method to create an object and call the methods accordingly to enable the task. Question 6 A class Arrange contains an array of N integers. Some of the members of the class are given below: Class name : Arrange Data members/instance variables : num[ ] : integer array. N : integer to store size of the array. Member functions/methods : Arrange( int nn) : constructor to initialize the data member N = nn and create the integer array. void FillArray( ) : to input N integers into the array. int indexOfMin( int p ) : returns the index/subscript of the smallest element of the array using the argument p. void Sort( ) : sorts the array in ascending order by invoking the function int indexOfMin(int) and by using the Selection sort algorithm. void dispArray( ) : to display integers present in the array. Specify the class Arrange giving the details of the constructor and functions void FillArray( ) , int indexOfMin(int) , void Sort( ) and void dispArray( ). Define a main( ) method to create an object and call the methods accordingly to input the array, print the original array and the sorted array.
Question 7 A class Matrix is declared with the following details : Class name : Matrix Data members/instance variables : mat[ ][ ] : double dimensional integer array. m : integers to store size of the array. Member functions/methods : Matrix( int nm ) : parameterized constructor to assign m = nm and create the array of size m x m. void readMatrix( ) : to input elements into the matrix mat[ ][ ] of m rows and m columns. void Show( ) : to print the array in square matrix form. void modify( ) : to replace the elements of left diagonal and right diagonal of the matrix by one ( 1 ). Example : Input : m = 4 INPUT MATRIX : OUTPUT MATRIX : 3 4 5 6 1 4 5 1 5 7 3 4 6 1 1 4 4 8 6 2 4 1 1 2 5 9 7 3 1 9 7 1 Specify the class Matrix giving the details of the constructor and void readMatrix( ) , void Show( ) and void modify( ). Define a main( ) function to create an object and by invoke methods print the original matrix and modified matrix. Question 8 The transpose of a matrix is obtained by changing rows to columns and columns to rows. For example: Input Matrix Transpose Matrix 3 4 5 3 8 4 8 9 7 4 9 3 4 3 2 5 7 2 A class Transpose is declared with the following details: Class name : Transpose Data members/instance variables : arr[ ][ ] : double dimensional integer array. newArr[ ][ ] : double dimensional integer array. m : integers to store size of the array. Member functions/methods : Transpose( int nm ) : parameterized constructor to assign m = nm and create the both the arrays of size m x m. void readMatrix( ) : to input and store elements into the matrix arr[ ][ ]. void TransMat( ) : to obtain and store the transpose form of the matrix arr[ ][ ] and into the matrix newArr[ ][ ]. void Show( ) : to print the original matrix and transpose matrix. Specify the class Transpose giving the details of the constructor and void readMatrix( ) , void TransMat( ) and void Show( ). Define a main( ) function to create an object and invoke the methods.