0% found this document useful (0 votes)
5 views16 pages

ISC Class11 Arrays Matrix Complete Guide

This document is a comprehensive practical guide for ISC Class 11 students covering arrays and matrix programs, including over 30 programs with detailed explanations. It includes sections on basic array operations, sorting algorithms, array manipulation, matrix operations, and special array programs. Each section provides logic and code examples for various programming tasks related to arrays and matrices.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

ISC Class11 Arrays Matrix Complete Guide

This document is a comprehensive practical guide for ISC Class 11 students covering arrays and matrix programs, including over 30 programs with detailed explanations. It includes sections on basic array operations, sorting algorithms, array manipulation, matrix operations, and special array programs. Each section provides logic and code examples for various programming tasks related to arrays and matrices.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ISC CLASS 11

Complete Practical Guide

Arrays & Matrix Programs

All DDA, Array, and Matrix Programs with Logic

30+ Programs with Complete Explanation

Topics Covered:
✓ Array Operations
✓ Searching Algorithms
✓ Sorting Algorithms
✓ Array Manipulation
✓ Matrix Operations
✓ Special Matrix Types
✓ Complete Logic Explanations
TABLE OF CONTENTS

Section 1: Basic Array Operations 3


• Input/Display, Sum/Average, Min/Max
• Linear Search, Binary Search
Section 2: Sorting Algorithms 8
• Bubble Sort, Selection Sort, Insertion Sort
Section 3: Array Manipulation 11
• Reverse, Merge, Remove Duplicates
• Frequency, Second Largest, Rotate
Section 4: Matrix Programs 15
• Basic Operations: Add, Subtract, Multiply
• Transpose, Symmetric Check
• Special Matrix Types
Section 5: Special Array Programs 23
• Palindrome, Even/Odd, Prime Numbers
Appendix: Quick Reference & Exam Tips 26
SECTION 1: BASIC ARRAY OPERATIONS

Program 1: Input and Display Array


Logic: Simple array traversal using for loop. Take size input, store elements in array, display using loop.

Code:
import [Link].*;
public class Program1 {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter size: ");
int n = [Link]();
int arr[] = new int[n];
[Link]("Enter elements:");
for(int i = 0; i < n; i++) {
arr[i] = [Link]();
}
[Link]("Array elements:");
for(int i = 0; i < n; i++) {
[Link](arr[i] + " ");
}
}
}

Program 2: Sum and Average of Array


Logic: Accumulator pattern - Initialize sum=0, traverse array adding each element, calculate average = sum/n

Code:
Scanner sc = new Scanner([Link]);
int n = [Link]();
int arr[] = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = [Link]();
}
int sum = 0;
for(int i = 0; i < n; i++) {
sum += arr[i];
}
double avg = (double)sum / n;
[Link]("Sum: " + sum);
[Link]("Average: " + avg);

Program 3: Find Largest and Smallest Element


Logic: Min-Max search - Initialize max=arr[0], min=arr[0], traverse from index 1, update if current element > max
or < min

Code:
int max = arr[0], min = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] > max) {
max = arr[i];
}
if(arr[i] < min) {
min = arr[i];
}
}
[Link]("Largest: " + max);
[Link]("Smallest: " + min);

Program 4: Linear Search


Logic: Sequential search - Compare each element with search key, display position if found

Code:
int search = [Link]();
boolean found = false;
for(int i = 0; i < n; i++) {
if(arr[i] == search) {
[Link]("Found at position " + (i+1));
found = true;
break;
}
}
if(!found) {
[Link]("Not found");
}
Program 5: Binary Search (Sorted Array)
Logic: Divide and conquer - Set low=0, high=n-1, find mid=(low+high)/2. If arr[mid]==key found; if arr[mid] less
than key search right (low=mid+1); else search left (high=mid-1)

Code:
int key = [Link]();
int low = 0, high = n - 1;
boolean found = false;
while(low <= high) {
int mid = (low + high) / 2;
if(arr[mid] == key) {
[Link]("Found at " + (mid+1));
found = true;
break;
}
else if(arr[mid] < key) {
low = mid + 1;
}
else {
high = mid - 1;
}
}

SECTION 2: SORTING ALGORITHMS

Program 6: Bubble Sort


Logic: Compare adjacent elements and swap if left > right. After each pass, largest element reaches end. Need
(n-1) passes.

Code:
for(int i = 0; i < n-1; i++) {
for(int j = 0; j < n-1-i; j++) {
if(arr[j] > arr[j+1]) {
// Swap
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

Program 7: Selection Sort


Logic: Find minimum element in unsorted part, swap with first unsorted element, move boundary

Code:
for(int i = 0; i < n-1; i++) {
int minIndex = i;
for(int j = i+1; j < n; j++) {
if(arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
Program 8: Insertion Sort
Logic: Build sorted array one element at a time. Pick key, shift larger elements right, insert at correct position

Code:
for(int i = 1; i < n; 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;
}
SECTION 3: ARRAY MANIPULATION

Program 9: Reverse an Array


Logic: Two pointer approach - start at beginning and end, swap elements, move pointers inward until start >=
end

Code:
int start = 0, end = n - 1;
while(start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}

Program 10: Merge Two Arrays


Logic: Create third array of size n1+n2, copy all elements from first array, then second array

Code:
int arr3[] = new int[n1 + n2];
for(int i = 0; i < n1; i++) {
arr3[i] = arr1[i];
}
for(int i = 0; i < n2; i++) {
arr3[n1 + i] = arr2[i];
}

Program 11: Remove Duplicates


Logic: For each element, check if it appears before. If not seen before, include in result

Code:
for(int i = 0; i < n; i++) {
boolean isDuplicate = false;
for(int j = 0; j < i; j++) {
if(arr[i] == arr[j]) {
isDuplicate = true;
break;
}
}
if(!isDuplicate) {
[Link](arr[i] + " ");
}
}

Program 12: Frequency of Elements


Logic: Mark visited elements, for each unvisited element count occurrences, display element and frequency

Code:
boolean visited[] = new boolean[n];
for(int i = 0; i < n; i++) {
if(visited[i]) continue;
int count = 1;
for(int j = i + 1; j < n; j++) {
if(arr[i] == arr[j]) {
visited[j] = true;
count++;
}
}
[Link](arr[i] + " occurs " + count + " times");
}
Program 13: Second Largest Element
Logic: Track largest and secondLargest. Update both as you find larger elements

Code:
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for(int i = 0; i < n; i++) {
if(arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
}
else if(arr[i] > secondLargest && arr[i] != largest) {
secondLargest = arr[i];
}
}

Program 14: Rotate Array Left by N Positions


Logic: Store first n elements in temp, shift remaining left, place temp at end

Code:
int n = 3; // rotate by 3 positions
int temp[] = new int[n];
for(int i = 0; i < n; i++) {
temp[i] = arr[i];
}
for(int i = 0; i < size - n; i++) {
arr[i] = arr[i + n];
}
for(int i = 0; i < n; i++) {
arr[size - n + i] = temp[i];
}

SECTION 4: MATRIX PROGRAMS

Program 15: Input and Display Matrix


Logic: 2D array traversal using nested loops. Outer loop for rows, inner for columns

Code:
int matrix[][] = new int[rows][cols];
[Link]("Enter elements:");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
matrix[i][j] = [Link]();
}
}
[Link]("Matrix:");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}
Program 16: Matrix Addition
Logic: Element-wise addition. Matrices must have same dimensions. result[i][j] = mat1[i][j] + mat2[i][j]

Code:
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
sum[i][j] = mat1[i][j] + mat2[i][j];
}
}

Program 17: Matrix Subtraction


Logic: Element-wise subtraction. result[i][j] = mat1[i][j] - mat2[i][j]

Code:
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
diff[i][j] = mat1[i][j] - mat2[i][j];
}
}

Program 18: Matrix Multiplication


Logic: Columns of first = Rows of second. result[i][j] = sum of (mat1[i][k] * mat2[k][j]). Three nested loops

Code:
if(c1 != r2) {
[Link]("Multiplication not possible!");
return;
}
int product[][] = new int[r1][c2];
for(int i = 0; i < r1; i++) {
for(int j = 0; j < c2; j++) {
product[i][j] = 0;
for(int k = 0; k < c1; k++) {
product[i][j] += mat1[i][k] * mat2[k][j];
}
}
}

Program 19: Transpose of Matrix


Logic: Swap rows and columns. transpose[j][i] = original[i][j]. Rows become columns

Code:
int transpose[][] = new int[cols][rows];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
Program 20: Symmetric Matrix Check
Logic: Matrix equals its transpose. Must be square. Check if matrix[i][j] == matrix[j][i] for all i,j

Code:
boolean isSymmetric = true;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(matrix[i][j] != matrix[j][i]) {
isSymmetric = false;
break;
}
}
if(!isSymmetric) break;
}

Program 21: Diagonal Elements Sum


Logic: Principal diagonal: i==j. Secondary diagonal: i+j==n-1

Code:
int principalSum = 0, secondarySum = 0;
for(int i = 0; i < n; i++) {
principalSum += matrix[i][i];
secondarySum += matrix[i][n-1-i];
}

Program 22: Identity Matrix Check


Logic: Square matrix with diagonal=1, rest=0. Check matrix[i][j]==1 if i==j, else 0

Code:
boolean isIdentity = true;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i == j) {
if(matrix[i][j] != 1) isIdentity = false;
} else {
if(matrix[i][j] != 0) isIdentity = false;
}
}
}

Program 23: Upper Triangular Matrix Check


Logic: All elements below diagonal are zero. Check if matrix[i][j]==0 when i greater than j

Code:
boolean isUpper = true;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i > j && matrix[i][j] != 0) {
isUpper = false;
}
}
}
Program 24: Lower Triangular Matrix Check
Logic: All elements above diagonal are zero. Check if matrix[i][j]==0 when i less than j

Code:
boolean isLower = true;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i < j && matrix[i][j] != 0) {
isLower = false;
}
}
}

Program 25: Sparse Matrix Check


Logic: More than half elements are zero. Count zeros, if zeros > total/2, it's sparse

Code:
int zeroCount = 0, total = rows * cols;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
if(matrix[i][j] == 0) zeroCount++;
}
}
if(zeroCount > total / 2) {
[Link]("Sparse Matrix");
}

Program 26: Row and Column Sum


Logic: For row sum: fix row, vary column. For column sum: fix column, vary row

Code:
// Row Sum
for(int i = 0; i < rows; i++) {
int rowSum = 0;
for(int j = 0; j < cols; j++) {
rowSum += matrix[i][j];
}
[Link]("Row " + (i+1) + ": " + rowSum);
}
// Column Sum
for(int j = 0; j < cols; j++) {
int colSum = 0;
for(int i = 0; i < rows; i++) {
colSum += matrix[i][j];
}
[Link]("Col " + (j+1) + ": " + colSum);
}
Program 27: Boundary Elements of Matrix
Logic: Print only boundary: first row, last row, first column (excluding corners), last column (excluding corners)

Code:
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
if(i == 0 || i == rows-1 ||
j == 0 || j == cols-1) {
[Link](matrix[i][j] + " ");
} else {
[Link](" ");
}
}
[Link]();
}

SECTION 5: SPECIAL ARRAY PROGRAMS

Program 28: Check Palindrome Array


Logic: Array reads same forwards and backwards. Compare first with last, second with second-last using two
pointers

Code:
boolean isPalindrome = true;
for(int i = 0; i < n/2; i++) {
if(arr[i] != arr[n-1-i]) {
isPalindrome = false;
break;
}
}

Program 29: Separate Even and Odd Numbers


Logic: Traverse array, check each element with modulo operator (%), display or store separately

Code:
[Link]("Even: ");
for(int i = 0; i < n; i++) {
if(arr[i] % 2 == 0) {
[Link](arr[i] + " ");
}
}
[Link]("Odd: ");
for(int i = 0; i < n; i++) {
if(arr[i] % 2 != 0) {
[Link](arr[i] + " ");
}
}
Program 30: Prime Numbers in Array
Logic: Check each element for primality. Prime: number > 1 with no divisors except 1 and itself. Check from 2 to
sqrt(n)

Code:
public static boolean isPrime(int num) {
if(num <= 1) return false;
if(num == 2) return true;
if(num % 2 == 0) return false;
for(int i = 3; i*i <= num; i += 2) {
if(num % i == 0) return false;
}
return true;
}
// In main:
for(int i = 0; i < n; i++) {
if(isPrime(arr[i])) {
[Link](arr[i] + " ");
}
}

APPENDIX: QUICK REFERENCE & EXAM TIPS

Key Points to Remember:


1. Array Declaration: int arr[] = new int[size]; or int matrix[][] = new int[rows][cols];

2. Index Range: Array starts at 0, last index is size-1. Be careful of ArrayIndexOutOfBoundsException

3. Loop Patterns: Single array: for(int i=0; i less than n; i++) | 2D: nested loops for rows and columns

4. Diagonal Access: Principal diagonal: i==j | Secondary diagonal: i+j==n-1

5. Swap Logic: Always use temp variable: temp=a; a=b; b=temp;

Matrix Properties:
Property Condition

Square Matrix rows == cols


Symmetric matrix[i][j] == matrix[j][i]
Identity diagonal = 1, rest = 0
Upper Triangular elements below diagonal = 0
Lower Triangular elements above diagonal = 0
Sparse more than 50% zeros

Important Conditions for Matrix Operations:


Addition/Subtraction: Both matrices must have same dimensions (rows and columns)

Multiplication: Columns of first matrix = Rows of second matrix

Result Dimension: (r1×c1) × (r2×c2) = (r1×c2)


Exam Strategy & Common Mistakes:
✓ Read Question Carefully: Note if array is sorted (for binary search), size constraints, input/output format

✓ Initialize Variables: Always initialize sum=0, count=0, max/min values before loops

✓ Check Edge Cases: Empty array, single element, all same elements, negative numbers

✓ Proper Formatting: Use consistent indentation, meaningful variable names, add comments

✓ Test Your Code: Run through with sample input mentally before submitting

✓ Handle Invalid Input: Check for invalid dimensions in matrix operations

✗ Common Mistakes: Forgetting array starts at 0 | Mixing up i and j in nested loops | Not checking array bounds

✗ Off-by-One Errors: Using i less than or equal to n instead of i less than n | Wrong loop conditions in sorting

Time Complexity (For Knowledge):


Algorithm Time Complexity Best For

Linear Search O(n) Small/unsorted arrays

Binary Search O(log n) Large sorted arrays

Bubble Sort O(n²) Small arrays, simple

Selection Sort O(n²) Small arrays, memory limited

Insertion Sort O(n²) Nearly sorted arrays

ALL THE BEST FOR YOUR EXAM! ■

Remember: Practice makes perfect! Run each program, understand the logic, and you'll ace the exam.

You might also like