PRASHNIKAप्रश्निका
Back to the paper

A class InsSort contains an array of integers which sorts the elements in a particular order. Some…

Computer Science202410 marksProgram
A class InsSort contains an array of integers which sorts the elements in a particular order. Some of the members of the class are given below. Class name : InsSort Data members/instance variables: arr[ ] : stores the array elements size : stores the number of elements in the array Methods / Member functions: InsSort(int s) : constructor to initialise size = s void getArray( ) : accepts the array elements void insertionSort( ) : sorts the elements of the array in descending order using the Insertion Sort technique double find( ) : calculates and returns the average of all the odd numbers in the array void display() : displays the elements of the array in a sorted order along with the average of all the odd numbers in the array by invoking the function find( ) with an appropriate message Specify the class InsSort giving details of the constructor( ), void getArray( ), void insertionSort( ), double find() and void display( ). Define a main() function to create an object and call all the functions accordingly to enable the task.

Answer

Answer

AI
import java.util.Scanner;

class InsSort
{
    int arr[];
    int size;

    InsSort(int s)
    {
        size = s;
        arr = new int[size];
    }

    void getArray()
    {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < size; i++)
        {
            System.out.print("Enter element " + (i + 1) + ": ");
            arr[i] = sc.nextInt();
        }
    }

    void insertionSort()
    {
        for (int i = 1; i < size; i++)
        {
            int key = arr[i];
            int j = i - 1;
            while (j >= 0 && arr[j] < key)
            {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = key;
        }
    }

    double find()
    {
        int sum = 0, count = 0;
        for (int i = 0; i < size; i++)
        {
            if (arr[i] % 2 != 0)
            {
                sum += arr[i];
                count++;
            }
        }
        if (count == 0)
            return 0;
        return (double) sum / count;
    }

    void display()
    {
        System.out.print("Sorted array (descending order): ");
        for (int i = 0; i < size; i++)
            System.out.print(arr[i] + " ");
        System.out.println();
        System.out.println("Average of odd numbers = " + find());
    }

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter size of array: ");
        int s = sc.nextInt();
        InsSort obj = new InsSort(s);
        obj.getArray();
        obj.insertionSort();
        obj.display();
    }
}
Explanation: The constructor allocates arr[] of the given size. getArray() reads the elements. insertionSort() implements the standard Insertion Sort but shifts elements while the key is greater than the compared element, so the array ends up sorted in descending order. find() sums all odd-valued elements and returns their average (0 if there are none). display() prints the sorted array and calls find() to print the average of odd numbers. Tested with input {12,5,9,20,3,8}: sorted output is 20 12 9 8 5 3, and the average of the odd numbers (5,9,3) is correctly computed as 5.666666666666667.
Arrays, Strings

From ISC 2024 Computer Science Paper 1, question 7.