12 - Ayush Ahirrao - Assignment 2
12 - Ayush Ahirrao - Assignment 2
OBJECTIVES:
INPUT:
OUTPUT:
ALGORITHM:
1D Array Operations
1. Search Element in Array
1. Iterate through the array.
2. If the element matches the key, return its index.
3. If not found, return -1.
2. Bubble Sort
1. Iterate through the array multiple times.
2. Compare adjacent elements and swap if needed.
3. Repeat until the array is sorted.
Matrix Operations
3. Find Saddle Point
1. Find the minimum element in each row.
2. Check if it is the maximum element in its column.
3. If found, print it; otherwise, return "No saddle point."
4. Check for Magic Square
1. Calculate the sum of the first row.
2. Check if all row sums, column sums, and diagonal sums match.
3. If they do, it is a magic square; otherwise, it is not.
Memory Address Calculation
1. Iterate over the array.
2. Print each element’s value and memory address.
Sparse Matrix Representation
1. Iterate through the matrix.
2. Store non-zero elements in a compressed form (row, col, value).
3. Print the compact representation
Traversals:
Array Traversal
● Iterate through the array using a loop and access each element.
● Used in saddle point detection, magic square verification, and sparse matrix compression
Applications:
A test case for this program involves providing an array or matrix as input and verifying the correctness
of implemented operations like sorting, searching, saddle point detection, magic square validation, and
sparse matrix representation. For instance, given an unsorted array, the program should return a sorted
version and confirm element presence through searching. In a matrix, it should correctly identify a
saddle point (minimum in its row, maximum in its column) and validate if a square matrix follows the
magic square property (equal row, column, and diagonal sums). For sparse matrices, the program should
efficiently store and display non-zero elements with their positions.
Code:
#include <stdio.h>
#include <stdlib.h>
#define ROWS 3
#define COLS 3
return -1;
arr[j + 1] = temp;
minRow = matrix[i][j];
colIndex = j;
maxCol = matrix[k][colIndex];
}
if (maxCol == minRow) {
return;
rowSum = colSum = 0;
rowSum += matrix[i][j];
colSum += matrix[j][i];
diag1 += matrix[i][i];
int sparse[MAX][3], k = 0;
if (matrix[i][j] != 0) {
sparse[k][0] = i;
sparse[k][1] = j;
sparse[k][2] = matrix[i][j];
k++;
}
printf("\nSparse Matrix Representation:\nRow Col Value\n");
int main() {
// Searching
if (index != -1) {
} else {
// Sorting
bubbleSort(arr, n);
printf("\n");
// Matrix Operations
int matrix[ROWS][COLS] = {
{8, 1, 6},
{3, 5, 7},
{4, 9, 2}
};
findSaddlePoint(matrix);
if (isMagicSquare(matrix)) {
} else {
printf("\nMemory Addresses:\n");
memoryAddressCalculation(arr, n);
int sparseMatrix[ROWS][COLS] = {
{0, 0, 5},
{0, 0, 0},
{3, 0, 0}
};
sparseMatrixRepresentation(sparseMatrix);
return 0;
Output
Memory Addresses: