Code with Vinay

ISC practical papers › ISC 2006 › Question 2

Merging Of Two Arrays

ISC 2006 Computer Science Practical, Question 2 · Arrays and matrices

The question

Write a program that inputs the names of people into two different arrays A and B. Array A has N number of names while Array B has M number of names, with no duplicates in either of them. Merge arrays A and B into a single array C, such that the resulting array is sorted alphabetically.

Display all the three arrays, A, B and C, sorted alphabetically.

Test your program for the given data and some random data.

SAMPLE DATA:
INPUT:
Enter number of names in Array A, N = 2
Enter number of names in Array B, M = 3
First array: (A)
Suman
Anil
Second array: (B)
Usha
Sachin
John
OUTPUT:
Sorted Merged array: (C)
Anil
John
Sachin
Suman
Usha
Sorted first array: (A)
Anil
Suman
Sorted Second array: (B)
John
Sachin
Usha

Question: ISC 2006 Computer Science Paper 2 (Practical), Question 2, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.

Java solution

ISC2006Q2.java Download GitHub
import java.util.*;

class MyArray {
    private String A[], B[], C[];

    public MyArray(String A[], String B[]) {
        this.A = A;
        this.B = B;
        C = new String[A.length + B.length];
        sort();
    }

    private void sort() {
        Arrays.sort(A);
        Arrays.sort(B);
        merge();
    }

    private void merge() {
        int indexA, indexB, indexC;
        indexA = indexB = indexC = 0;
        while (indexA < A.length && indexB < B.length) {
            if (A[indexA].compareTo(B[indexB]) < 0) {
                C[indexC++] = A[indexA++];
            }else{
                C[indexC++] = B[indexB++];
            }
        }
        if(indexA<A.length){
            for(int i=indexA;i<A.length;i++){
                C[indexC++] = A[indexA++];
            }
        }else if(indexB<B.length){
            for(int i=indexB;i<B.length;i++){
                C[indexC++] = B[indexB++];
            }
        }
    }
    public String toString(){
        String output="";
        output+="Sorted Merged array: (C)\n";
        for(String name: C){
            output+=name+"\n";
        }

        output+="Sorted first array: (A)\n";
        for(String name: A){
            output+=name+"\n";
        }

        output+="Sorted second array: (B)\n";
        for(String name: B){
            output+=name+"\n";
        }

        return output;
    }
}

public class ISC2006Q2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("INPUT:");
        System.out.print("Enter number of names in Array A, N = ");
        int N = in.nextInt();
        System.out.print("Enter number of names in Array B, M = ");
        int M = in.nextInt();
        in.nextLine();

        String A[] = new String[N];
        String B[] = new String[M];

        System.out.println("First array: (A)");
        for (int i = 0; i < N; i++) {
            A[i] = in.nextLine();
        }

        System.out.println("First array: (B)");
        for (int i = 0; i < M; i++) {
            B[i] = in.nextLine();
        }

        MyArray obj = new MyArray(A, B);

        System.out.println("OUTPUT:");
        System.out.println(obj);

        in.close();
    }
}

All these solutions in one free book

Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.

Get the free PDF ₹0 ISC 2027 mock papers