ICSE Class 10 Computer Applications Question 50 of 59

Arrays — Question 52

Back to all questions
52
Question

Question 46

Write a program to search for a given ITEM in a given array X[n] using linear search technique. If the ITEM is found, move it at the top of the array. If the ITEM is not found, insert it at the end of the array.

import java.util.Scanner;

public class KboatLinearSearch
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter size of array : ");
        int n = in.nextInt();
        
        int X[] = new int[n];
        int B[];
        
        System.out.println("Enter array elements : ");
        for(int i = 0; i < n; i++) {
            X[i] = in.nextInt();
        }

        System.out.println("Enter search item :" );
        int item = in.nextInt();
        
        int i = 0;
        
        // linear search
        for (i = 0; i < n; i++) {
            if (X[i] == item) {
                System.out.println("Search item found at index " 
                                    + i);
                break;
            }
        }
        
        // item not found
        if (i == n) {
            System.out.println("Search item not found");
            B = new int[n + 1];
            for(int j = 0; j < n; j++)  {
                B[j] = X[j];
            }
            B[n] = item;
            
            System.out.println("Array after inserting "
                                + item + " at end:");
            for(int j = 0; j <= n; j++)  {
                System.out.print(B[j] + " ");
            }
        }
        // item found
        else {
            for(int j = i; j >= 1; j--) {
                X[j] = X[j - 1];
            }
            X[0] = item;
                     
            System.out.println("Array after moving " 
                                + item + " to top :");
            for(int j = 0; j < n; j++)  {
                System.out.print(X[j] + " ");
            }
        }   
    }
}
Output
BlueJ output of KboatLinearSearch.java
BlueJ output of KboatLinearSearch.java
Answer

Source: This question is from Arrays, Computer Applications — Class 10, ICSE Board.

Key Concepts Covered

This question tests your understanding of the following concepts from the chapter Arrays: Question, Program, Search, Item, Array, Linear. These are fundamental topics in Computer Applications that students are expected to master as part of the ICSE Class 10 curriculum.

A thorough understanding of these concepts will help you answer similar questions confidently in your ICSE examinations. These topics are frequently tested in both objective and subjective sections of Computer Applications papers. We recommend revising the relevant section of your textbook alongside practising these solved examples to build a strong foundation.

How to Approach This Question

Read the question carefully and identify what is being asked. Break down complex questions into smaller parts. Use the terminology and concepts discussed in this chapter. Structure your answer logically — begin with a definition or key statement, then provide supporting details. Review your answer to ensure it addresses all parts of the question completely.

Key Points to Remember

  • Write programs with proper indentation and comments.
  • Trace through your code with sample inputs to verify correctness.
  • Explain the logic behind each step of your solution.
  • Familiarise yourself with common library functions and methods.

Practice more questions from Arrays — Computer Applications, Class 10 ICSE