0% found this document useful (0 votes)
10 views10 pages

12 - Ayush Ahirrao - Assignment 2

The document outlines an assignment focused on matrix operations, including objectives such as implementing array operations, sorting algorithms, and matrix manipulation. It details input requirements, expected outputs, algorithms for searching, sorting, saddle point detection, magic square validation, memory address calculations, and sparse matrix representation. Additionally, it includes a code implementation in C for the described operations and a test case to verify functionality.

Uploaded by

aftab.desai23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

12 - Ayush Ahirrao - Assignment 2

The document outlines an assignment focused on matrix operations, including objectives such as implementing array operations, sorting algorithms, and matrix manipulation. It details input requirements, expected outputs, algorithms for searching, sorting, saddle point detection, magic square validation, memory address calculations, and sparse matrix representation. Additionally, it includes a code implementation in C for the described operations and a test case to verify functionality.

Uploaded by

aftab.desai23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

ASSIGNMENT NUMBER : 02

TITTLE: MATRIX OPERATIONS

NAME :- AYUSH AHIRRAO


CLASS:- CSAI-A
BATCH :- 3
ROLL NO:- 12

OBJECTIVES:

1. To implement basic array operations like searching & element access.


2. To understand sorting algorithms & their applications.
3. To practice matrix manipulation & handling 2D arrays.
4. To understand matrix properties like saddle points & magic square matrices.
5. To understand & apply memory address calculations for array elements.
6. To understand sparse matrices & implement efficient storage techniques.

INPUT:

1. A 1D array for sorting, searching, and memory address calculation.


2. A 2D matrix for saddle point detection and magic square verification.
3. A sparse matrix for efficient storage representation.

OUTPUT:

1. Searching: Displays whether the element is found and its index.


2. Sorting: Displays the sorted array.
3. Saddle Point: Indicates if a saddle point exists and its position.
4. Magic Square: Prints whether the given matrix is a magic square.
5. Memory Address Calculation: Shows addresses of elements in the array.
6. Sparse Matrix Representation: Prints the non-zero elements in compressed format.

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:

Traversal refers to visiting each element of the array or matrix systematically.

Array Traversal

● Iterate through the array using a loop and access each element.

● Used in searching, sorting, and memory address calculations.


Matrix Traversal
● Row-wise traversal: Iterate through rows first, then columns.

● Column-wise traversal: Iterate through columns first, then rows.

● Diagonal traversal: Access elements along the primary or secondary diagonal.

● Used in saddle point detection, magic square verification, and sparse matrix compression

Applications:

1. Sorting & Searching – Used in databases, indexing, and optimization problems.


2. Magic Square & Saddle Point – Applied in game development and cryptographic algorithms.
3. Memory Address Calculation – Useful for low-level programming and memory-efficient coding.
4. Sparse Matrix Representation – Reduces memory usage in scientific computing and large-scale
datasets.
Test case:

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

#define MAX 100

// Function to search for an element in an array

int searchElement(int arr[], int n, int key) {

for (int i = 0; i < n; i++) {

if (arr[i] == key) return i;

return -1;

// Function to sort an array using Bubble Sort

void bubbleSort(int arr[], int n) {

for (int i = 0; i < n - 1; i++) {


for (int j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

// Function to check for saddle point in a matrix

void findSaddlePoint(int matrix[ROWS][COLS]) {

for (int i = 0; i < ROWS; i++) {

int minRow = matrix[i][0], colIndex = 0;

for (int j = 1; j < COLS; j++) {

if (matrix[i][j] < minRow) {

minRow = matrix[i][j];

colIndex = j;

int maxCol = minRow;

for (int k = 0; k < ROWS; k++) {

if (matrix[k][colIndex] > maxCol) {

maxCol = matrix[k][colIndex];
}

if (maxCol == minRow) {

printf("Saddle Point found: %d at (%d, %d)\n", maxCol, i, colIndex);

return;

printf("No Saddle Point found.\n");

// Function to check if a matrix is a magic square

int isMagicSquare(int matrix[ROWS][COLS]) {

int sum = 0, rowSum, colSum, diag1 = 0, diag2 = 0;

for (int i = 0; i < COLS; i++) sum += matrix[0][i];

for (int i = 0; i < ROWS; i++) {

rowSum = colSum = 0;

for (int j = 0; j < COLS; j++) {

rowSum += matrix[i][j];

colSum += matrix[j][i];

if (rowSum != sum || colSum != sum) return 0;

diag1 += matrix[i][i];

diag2 += matrix[i][COLS - i - 1];


}

return (diag1 == sum && diag2 == sum);

// Function to compute memory address of an element in a 1D array

void memoryAddressCalculation(int arr[], int n) {

for (int i = 0; i < n; i++) {

printf("Element: %d, Address: %p\n", arr[i], (void*)&arr[i]);

// Function to convert a sparse matrix to a compact form

void sparseMatrixRepresentation(int matrix[ROWS][COLS]) {

int sparse[MAX][3], k = 0;

for (int i = 0; i < ROWS; i++) {

for (int j = 0; j < COLS; j++) {

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");

for (int i = 0; i < k; i++) {

printf("%d %d %d\n", sparse[i][0], sparse[i][1], sparse[i][2]);

int main() {

int arr[] = {34, 7, 23, 32, 5, 62};

int n = sizeof(arr) / sizeof(arr[0]);

int key = 23;

// Searching

int index = searchElement(arr, n, key);

if (index != -1) {

printf("Element %d found at index %d\n", key, index);

} else {

printf("Element %d not found\n", key);

// Sorting

bubbleSort(arr, n);

printf("Sorted Array: ");

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);


}

printf("\n");

// Matrix Operations

int matrix[ROWS][COLS] = {

{8, 1, 6},

{3, 5, 7},

{4, 9, 2}

};

findSaddlePoint(matrix);

if (isMagicSquare(matrix)) {

printf("The matrix is a Magic Square\n");

} else {

printf("The matrix is NOT a Magic Square\n");

// Memory Address Calculation

printf("\nMemory Addresses:\n");

memoryAddressCalculation(arr, n);

// Sparse Matrix Representation

int sparseMatrix[ROWS][COLS] = {
{0, 0, 5},

{0, 0, 0},

{3, 0, 0}

};

sparseMatrixRepresentation(sparseMatrix);

return 0;

Output

Element 23 found at index 2

Sorted Array: 9 7 3 324 394 2

No Saddle Point found.

The matrix is a Magic Square

Memory Addresses:

Element: 9, Address: 0x7ffc9b99c5b0a

Element: 7, Address: 0x7ffc9b99c5b41

Element: 3, Address: 0x7ffc9b99c5b84

Element: 324, Address: 0x7ffc9b99c5bck

Element: 394, Address: 0x7ffc9b99c5c00

Element: 2, Address: 0x7ffc9b99c5c4f

Sparse Matrix Representation:

Row Col Value


0 2 5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy