Module 1
Module 1
APPLICATION
Module 1
What is a Data Structure?
Prepared by Amrutha N
• Data structure is a storage that is used to store and organize data.
Prepared by Amrutha N
DATA STRUCTURES
Core concept in programming
Prepared by Amrutha N
DATA STRUCTURES
• There are many ways of organizing the data in the memory, one of the data
structures is array.
• The data structure is not any programming language like C, C++, java, etc.
• It is not the complete program or code; it is just a solution (logic) of a problem, which
can be represented either as an informal description using a flowchart or pseudocode.
Prepared by Amrutha N
Properties of Algorithm
Definiteness
Clarity and precision of
steps Effectiveness
Feasibility and reliability of
operations
Output
Result produced after
processing the input Finiteness
Ensures completion in
limited time and resources
Input
Algorithm may accept zero
or more inputs.
Prepared by Amrutha N
TYPES OF DATA STRUCTURES
Prepared by Amrutha N
• Primitive Data Structures
• The int, char, float, double, and pointer are the primitive data structures that
can hold a single value.
• The pointers, however don’t hold a data value, instead, they hold memory
addresses of the data values.
Prepared by Amrutha N
• Non-primitive Data Structures
• Although, they too are provided by the system itself yet they are derived
data structures and cannot be formed without using the primitive data
structures.
Prepared by Amrutha N
Linear data structures
• In linear data structures, the elements are arranged in sequence one after the other.
• Since elements are arranged in particular order, they are easy to implement.
• However, when the complexity of the program increases, the linear data structures
might not be the best choice because of operational complexities.
• The data structures used for this purpose are arrays, linked list, stacks, and
queues.
Prepared by Amrutha N
• Linear data structure can be divided as:
• It is a type of data structure where the size is allocated at the compile time.
• Eg: Array
Prepared by Amrutha N
• Dynamic data structure
• It is a type of data structure where the size is allocated at the run time.
Prepared by Amrutha N
• Array Data Structure
Prepared by Amrutha N
• Stack Data Structure
• In stack data structure, elements are stored in the Last In First Out (LIFO)
principle.
• That is, the last element stored in a stack will be removed first.
Prepared by Amrutha N
• Queue Data Structure
• Unlike stack, the queue data structure works in the First In First Out (FIFO)
principle where first element stored in the queue will be removed first.
Prepared by Amrutha N
• Linked List Data Structure
• In linked list data structure, data elements are connected through a series of
nodes.
• And, each node contains the data items and address to the next node.
Prepared by Amrutha N
Non - Linear data structures
• This data structure does not form a sequence i.e. each item or element is
connected with two or more other items in a non-linear arrangement.
Prepared by Amrutha N
• Graph Data Structure
• The nodes are sometimes also referred to as vertices and the edges are lines or
arcs that connect any two nodes in the graph.
Prepared by Amrutha N
• Trees Data Structure
• A tree data structure is a non-linear data structure because it does not store in
a sequential manner.
Prepared by Amrutha N
Data Structure Operations
Prepared by Amrutha N
Traversing Deletion
Accessing each record
exactly once so that To remove the
certain items in the element from the data
record may be structure.
processed.
Searching Insertion
Search for any Insert the new
element in a data element in a data
structure. structure.
Prepared by Amrutha N
• Traversing: It means to access each data item exactly once so that it can be
processed. For example, to print the names of all the students in a class.
• Searching: It is used to find the location of one or more data items that satisfy the
given constraint. Such a data item may or may not be present in the given collection
of data items. For example, to find the names of all the students who secured 100
marks in mathematics.
• Inserting: It is used to add new data items to the given list of data items. For
example, to add the details of a new student who has recently joined the course.
Prepared by Amrutha N
• Deleting: It means to remove a particular data item from the given collection of data
items. For example, to delete the name of a student who has left the course.
• Sorting: Data items can be arranged in some order like ascending order or
descending order depending on the type of application. For example, arranging the
names of students in a class in an alphabetical order.
• Merging: Lists of two sorted data items can be combined to form a single list of
sorted data items
Prepared by Amrutha N
Prepared by Amrutha N
ARRAY DATA STRUCTURE
• The idea is to store multiple items of the same data type together.
• Array is the simplest data structure where each data element can be randomly accessed by
using its index number.
• Index − Each location of an element in an array has a numerical index, which is used to
identify the element.
Prepared by Amrutha N
ARRAY DATA STRUCTURE
Length = UB – LB +1
UB = Upper Bound
LB = Lower Bound
Prepared by Amrutha N
TYPES OF ARRAYS
• Multi-dimensional array
• 2 dimensional array
• 3 dimensional array
Prepared by Amrutha N
One Dimensional Array
• Declaring array
datatype array_name[];
Prepared by Amrutha N
One Dimensional Array
• Declaring Array
• Variable_name: It’s the name of the array , from which you want to
identify the array.
int myarray[10];
Prepared by Amrutha N
2 Dimensional Array
• Two dimensional array is arranged as an array of arrays.
• The representation of the elements is done in the form of rows & columns.
• Declaring Array
datatype array_name[row_size][column_size];
Prepared by Amrutha N
Creating an array
• Declaring an array
• datatype arrayname[arraysize];
• Initializing:
Prepared by Amrutha N
Applications an array
• The page number present in the books are examples of the array. Skipping
to a particular page number is similar to fetching a random element from
an array.
Prepared by Amrutha N
Applications an array
• We order products online. We add the products we wish to buy to the cart.
The server stores the ordered products in the form of an array.
Prepared by Amrutha N
Write a C program to read and
display “n” numbers using an array.
Prepared by Amrutha N
#include <stdio.h> for(i = 0; i < n; i++)
int main() {
scanf("%d", &a[i]);
{
}
int n, i; printf("\nResult:\n", n);
for(i = 0; i < n; i++)
int a[20];
{
printf("Enter the limit\n"); printf("%d ", a[i]);
scanf("%d", &n); }
return 0;
printf("Enter %d numbers:\n", n);
}
Prepared by Amrutha N
STRUCTURE
• Structure is a user-defined data type that can be used to group items of possibly
different types into a single type.
• Unlike an array, a structure can contain many different data types (int, float, char,
etc.).
• The items in the structure are called its members and they can be of any valid data
type.
Prepared by Amrutha N
How to create a structure?
struct structure_name struct book
{ {
}; };
Prepared by Amrutha N
How to declare structure variables?
Prepared by Amrutha N
Declaring structure variable separately
Declaring structure variable
struct book
{
struct book
char title[50];
{
float price;
char title[50];
};
float price;
int main()
}b1;
{
}
Prepared by Amrutha N
Creates a variable “b1” and that has two fields:
Prepared by Amrutha N
How to initialize structure members?
Prepared by Amrutha N
• Structure members cannot be initialized with declaration.
struct Point
{
int x = 0;
int y = 0;
};
• When a datatype is declared; no memory is allocated for it. Memory is allocated only
when variables are created.
Prepared by Amrutha N
• Structure members can be initialized using curly braces ‘{}’.
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {0, 1};
}
Prepared by Amrutha N
How to access structure members?
• To access or modify members of a structure, we use the ( . ) dot operator.
• In the case where we have a pointer to the structure, we can also use the (->)arrow
operator to access the members.
Prepared by Amrutha N
#include <stdio.h>
struct book
{
char title[50];
float price;
};
int main()
{
struct book b1 = {"Data Structures", 675.50};
printf("Title: %s \n", [Link]);
printf("Price: %.2f\n", [Link]);
return 0;
}
Prepared by Amrutha N
Store and display student details using
structure.(Name, RollNo, Marks)
Prepared by Amrutha N
#include <stdio.h>
struct Student {
int roll;
char name[50];
printf("\n--- Student Details ---\n");
float marks;
printf("Roll No: %d\n", [Link]);
};
printf("Name: %s\n", [Link]);
int main()
printf("Marks: %.2f\n", [Link]);
{
return 0;
struct Student s1;
}
printf("Enter roll number: ");
scanf("%d", &[Link]);
printf("Enter name: ");
scanf("%s", [Link]);
printf("Enter marks: ");
scanf("%f", &[Link]); Prepared by Amrutha N
Create a structure named "Employee" to store
employee details such as employee ID, name, and
salary. Write a program to input data for two
employees
Prepared by Amrutha N
#include <stdio.h> // Input details for the second employee
struct Employee printf("\nInput details for Employee 2:\n");
{ printf(" Enter the employee ID: ");
int employeeID; scanf("%d", &[Link]);
char name[50]; printf(“Enter the name: ");
float salary; scanf("%s", [Link]);
}; printf(“Enter the salary: ");
int main() scanf("%f", &[Link]);
{ printf("\nDetails Entered:\n");
struct Employee e1, e2; printf("Employee ID: %d\n", [Link]);
// Input details for the first employee printf("Name: %s\n", [Link]);
printf("Input details for Employee 1:\n"); printf("Salary: %.2f\n", [Link]);
printf("Enter the employee ID: "); printf("Employee ID: %d\n", [Link]);
scanf("%d", &[Link]); printf("Name: %s\n", [Link]);
printf(“Enter the name: "); printf("Marks: %.2f\n", [Link]);
scanf("%s", [Link]); return 0;
printf(“Enter the salary: "); }
scanf("%f", &[Link]); Prepared by Amrutha N
ARRAY OF STRUCTURES
• An array of structures in C is a powerful data structure that allows us
to represent multiple values with a single variable.
Prepared by Amrutha N
#include <stdio.h> for (i = 0; i < n; i++)
struct Student {
{ printf("\nEnter details of student %d\n", i + 1);
char name[50]; printf(“Enter the name: ");
float marks; scanf("%s", s[i].name);
}; printf(“Enter the marks: ");
int main() scanf("%f", &s[i].marks);
{ }
int n, i; printf("\n--- Student Details ---\n");
struct Student s[10]; for (i = 0; i < n; i++) {
printf("Enter number of students: "); printf("%s\t%.2f\n", s[i].name, s[i].marks);
scanf("%d", &n); }
return 0;
}
Prepared by Amrutha N
Structures with Functions
Prepared by Amrutha N
int main()
#include <stdio.h> {
struct student struct student s1;
{ s1 = getDetails();
int roll; printf("Student Details:\n");
float marks; printf("Roll No: %d\n", [Link]);
}; printf("Marks: %.2f\n", [Link]);
struct student getDetails() return 0;
{ }
struct student s;
[Link] = 101;
[Link] = 87.5;
return s;
}
Prepared by Amrutha N
#include <stdio.h> printf("Enter Marks: ");
struct student scanf("%f", &[Link]);
{ return s;
int roll; }
char name[30]; int main()
float marks; {
}; struct student s1 = input();
struct student input() printf("\nDetails Entered:\n");
{ printf("Roll No: %d\n", [Link]);
struct student s; printf("Name: %s\n", [Link]);
printf("Enter Roll No: "); printf("Marks: %.2f\n", [Link]);
scanf("%d", &[Link]); return 0;
printf("Enter Name: "); }
scanf("%s", [Link]);
Prepared by Amrutha N
POINTER TO STRUCTURE
• A structure pointer is a pointer variable that stores the address of a structure.
• It allows the programmer to manipulate the structure and its members directly by
referencing their memory location rather than passing the structure itself.
Prepared by Amrutha N
#include<stdio.h>
struct student
{
char name[30];
int marks; • Declare a pointer to the struct
}; • Assigning the address to the
int main() pointer
{ • Access members using the ->
struct student s1 = {"Aravind", 87}; operator
struct student *ptr;
ptr = &s1;
printf("Name: %s\n", ptr->name);
printf("Marks: %d\n", ptr->marks);
return 0;
}
Prepared by Amrutha N
UNION
• Union is a user-defined data type in C language that can contain elements of the
different data types just like structure.
• Unlike structures, all the members in the union are stored in the same memory
location.
Prepared by Amrutha N
How to create union?
union union_name union book
{ {
}; };
Prepared by Amrutha N
Declaring structure variable separately
Declaring structure variable
union book
{
union book
char title[50];
{
float price;
char title[50];
};
float price;
int main()
}b1;
{
}
Prepared by Amrutha N
#include <stdio.h>
union student
{
int roll;
float marks;
};
int main()
{
union student s1;
[Link] = 5;
printf("Roll: %d\n", [Link]);
[Link] = 120.0;
printf("Marks: %.2f\n", [Link]);
return 0;
} Prepared by Amrutha N
Differences between structure & union
Structure Union
Defined using the struct keyword. Defined using the union keyword.
Separate memory for each member Shared memory for all members
All members can hold values Only one member can hold a value at a
simultaneously time
Grouping related data with independent Memory-efficient storage when only one
values value is needed at a time
Prepared by Amrutha N
TYPES OF MEMORY
• Static Memory Allocation: The memory is allocated during compile-time.
Prepared by Amrutha N
Static Memory Allocation
• Static variable defines in one block of allocated space, of a fixed size. Once it is
allocated, it can never be freed.
int students[30];
• C will reserve space for 30 elements which is typically 120 bytes of memory (30 * 4).
Prepared by Amrutha N
Static Memory Allocation
• If you are using only 10 elements, the remaining 20 element’s memory (80 bytes)
stays unused.
• But it still cannot be freed until the program ends. This is because static memory
allocation.
Prepared by Amrutha N
Dynamic Memory Allocation
• Dynamic memory allocation in C allows programs to manage memory during
runtime, rather than having memory allocated at compile time.
Prepared by Amrutha N
Dynamic memory allocation
Method Description
Prepared by Amrutha N
malloc()
• malloc (memory allocation) is a function in C used to dynamically allocate memory
at runtime.
• Syntax:
Prepared by Amrutha N
For eg: malloc(4*sizeof(int));
• It means you are asking the system to reserve 16 bytes of memory from the heap.
• But that memory block exists somewhere in the computer’s memory, and to access it,
you need to store its starting address (the base address).
Prepared by Amrutha N
malloc()
int *ptr;
Prepared by Amrutha N
malloc()
ptr = (castType*) malloc(size_in_bytes);
• malloc function does not know which type of value you are going to store. That’s why
type casting.
int *ptr;
• (int *) tells the compiler that the memory block returned by malloc() will be used to
store integers.
Prepared by Amrutha N
malloc()
#include <stdlib.h>
Prepared by Amrutha N
#include <stdio.h>
printf("Enter the elements\n");
#include <stdlib.h>
for (int i = 0; i < n; i++)
int main()
{
{
scanf("%d", &arr[i]);
int n;
}
printf("Enter number of elements: ");
printf("The elements are: ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
// Dynamically allocate memory for n integers
{
int *arr = (int*) malloc(n * sizeof(int));
printf("%d ", arr[i]);
// Check if memory allocation was successful
}
if (arr == NULL)
// Free the allocated memory
{
free(arr);
printf("Memory allocation failed\n");
return 0;
return 1;
}
}
Prepared by Amrutha N
calloc()
• calloc() stands for Contiguous Allocation.
• Used to dynamically allocate multiple blocks of memory and each block is of same
size.
• Syntax:
Prepared by Amrutha N
calloc()
int *ptr = (int *)calloc(5, sizeof(int));
• ptr[0] = 0
• ptr[1] = 0
• ptr[2] = 0
• ptr[3] = 0
• ptr[4] = 0
Prepared by Amrutha N
Differences between malloc() & calloc()
malloc() calloc()
malloc() is a function that creates one Used to dynamically allocate multiple blocks
block of memory of a fixed size.
of memory and each block is of same size.
malloc() only takes one argument calloc() takes two arguments.
malloc() does not initialize the memory calloc() initializes the memory to zero
to zero
Prepared by Amrutha N
realloc()
• realloc() stands for Reallocation of memory.
• It is used to resize an already allocated memory block (which was previously created
using malloc() or calloc()).
• Syntax:
Prepared by Amrutha N
free()
• In C, the free() function is used to release dynamically allocated memory back to the
system.
• Syntax:
free(ptr);
• ptr → pointer to the memory block that was allocated using malloc(), calloc(), or
realloc().
Prepared by Amrutha N
What is a Polynomial?
Prepared by Amrutha N
A polynomial is a mathematical expression composed of variables, coefficients,
and constants, using only the operations of addition, subtraction, multiplication,
and non-negative integer exponents.
Prepared by Amrutha N
POLYNOMIAL REPRESENTATION USING AN ARRAY
Coefficient 8 -5 6
Exponent 2 1 0
0 1 2 3 4 5 6
Prepared by Amrutha N
POLYNOMIAL REPRESENTATION USING AN ARRAY
• 3x3 +2x + 2
• 8x2 - 5x + 6
start a start b avail
Coefficient 3 2 2 8 -5 6
Exponent 3 1 0 2 1 0
finish a finish b
Prepared by Amrutha N
#include <stdio.h>
printf("Enter terms for first polynomial\n");
struct term
for (i = 0; i < n1; i++)
{
{
int coeff;
printf("Term %d:\n", i + 1);
int exp;
printf("Coefficient: ");
};
scanf("%d", &poly[k].coeff);
int main()
printf("Exponent: ");
{
scanf("%d", &poly[k].exp);
struct term poly[20];
k++; // move to next index
int n1, n2, i, k = 0;
}
// First polynomial
printf("Enter number of terms in first polynomial\n");
scanf("%d", &n1);
Prepared by Amrutha N
// Second polynomial // Display both polynomials
printf("\nEnter number of terms in second polynomial\n"); printf("\nFirst Polynomial: ");
scanf("%d", &n2); for (i = 0; i < n1; i++)
printf("Enter terms for second polynomial \n");
{
for (i = 0; i < n2; i++)
printf("%dx^%d", poly[i].coeff, poly[i].exp);
{
if (i != n1 - 1)
printf("Term %d:\n", i + 1);
{
printf("Coefficient: ");
printf(" + ");
scanf("%d", &poly[k].coeff);
}
printf("Exponent: ");
}
scanf("%d", &poly[k].exp);
k++;
}
Prepared by Amrutha N
printf("\nSecond Polynomial: ");
for (i = n1; i < n1 + n2; i++)
{
printf("%dx^%d", poly[i].coeff, poly[i].exp);
if (i != n1 + n2 - 1)
{
printf(" + ");
}
}
printf("\n");
return 0;
}
Prepared by Amrutha N
POLYNOMIAL ADDITION
• Suppose you want to add two polynomials: (5x2 + 4x + 1) & (2x2 + 5x + 2)
Prepared by Amrutha N
POLYNOMIAL ADDITION
• Adding two polynomials: (6x2 + 3x + 1) & (2x2 + 5x)
Coefficient 6 3 1 2 5
Exponent 2 1 0 2 1
0 1 2 3 4 5 6 7 8
Coefficient 6 3 1 2 5 8 8 1
Exponent 2 1 0 2 1 2 1 0
0 1 2 3 4 5 6 7 8
Prepared by Amrutha N
Algorithm: Poly(starta, finisha, startb, finishb, startd, finishd)
1. startd ← avail 3. while starta ≤ finisha do
2. while starta ≤ finisha and startb ≤ finishb do
attach(terms[starta].coeff, terms[starta].exp)
if terms[starta].exp == terms[startb].exp then
starta ← starta + 1
coeff ← terms[starta].coeff + terms[startb].coeff
4. while startb ≤ finishb do
if coeff ≠ 0 then
attach(terms[startb].coeff, terms[startb].exp)
attach(coeff, terms[starta].exp)
startb ← startb + 1
starta ← starta + 1
startb ← startb + 1
5. finishd ← avail - 1
else if terms[starta].exp > terms[startb].exp then 6. return
attach(terms[starta].coeff, terms[starta].exp)
starta ← starta + 1
else
attach(terms[startb].coeff, terms[startb].exp)
startb ← startb + 1
Prepared by Amrutha N
• In step 1, initialize the start index of the result polynomial to the current available
position (avail) in the array.
• In step 2, if both polynomials have terms with the same exponent, they can be
combined.
• If the resulting coefficient is not zero, create a new term with the same exponent and
the sum of coefficients.
• The attach() function places this new term into terms[avail] and then increases avail.
• Move to the next term in both polynomials, since these two have been processed.
Prepared by Amrutha N
• If the exponent of terms[starta] is greater, it means that term has higher degree, so it
comes first in the result. Attach it to the result and move forward in Polynomial A.
• Otherwise (if exponent of terms[startb] is greater), attach that term from Polynomial B
and move forward in B.
• After the main loop (step 3), if Polynomial A still has leftover terms (because it was
longer), attach all remaining terms to the result polynomial.
• After the main loop (step 4), if Polynomial B still has leftover, attach all remaining terms
to the result polynomial.
Prepared by Amrutha N
What is a Matrix?
Prepared by Amrutha N
MATRIX
• A matrix can be defined with a 2-dimensional array.
• Any array with 'm' rows and 'n' columns represent a m x n matrix.
Prepared by Amrutha N
SPARSE MATRIX
• There may be a situation in which a matrix contains more number of ZERO values
than NON-ZERO values. Such matrix is known as sparse matrix.
Prepared by Amrutha N
SPARSE MATRIX
Prepared by Amrutha N
Why is a sparse matrix required if we can use the
simple matrix to store elements?
Prepared by Amrutha N
• Storage: Sparse matrix contains lesser non-zero elements than zero, so less
memory can be used to store elements. It evaluates only the non-zero
elements.
Prepared by Amrutha N
SPARSE MATRIX
• Sparse matrix can be represented as Triplet representation (Array representation).
• In this representation, we consider only non-zero values along with their row and
column index values.
Prepared by Amrutha N
• For example, consider a matrix of size 5 X 6 containing 6 number of non-zero
values.
Prepared by Amrutha N
• In above example matrix, there are only 6 non-zero elements (those are 9, 8, 4, 2,
5 & 2) and matrix size is 5 X 6.
• We represent this matrix as shown in the above image. Here the first row in the
right side table is filled with values 5, 6 & 6 which indicates that it is a sparse
matrix with 5 rows, 6 columns & 6 non-zero values.
• The second row is filled with 0, 4, & 9 which indicates the non-zero value 9 is at
the 0th-row 4th column in the sparse matrix.
• In the same way, the remaining non-zero values also follow a similar pattern.
Prepared by Amrutha N
Sparse Matrix Implementation
Prepared by Amrutha N
// Read matrix elements
#include <stdio.h>
printf("Enter the elements of the matrix\n");
#define MAX 100
struct Sparse
for(i = 0; i < m; i++)
{ {
int row; for(j = 0; j < n; j++)
int col; {
int value; scanf("%d", &matrix[i][j]);
}; if(matrix[i][j] != 0)
int main() {
{ sparse[k].row = i;
int matrix[10][10]; sparse[k].col = j;
struct Sparse sparse[MAX];
sparse[k].value = matrix[i][j];
int m, n, i, j, k = 0;
k++;
printf("Enter the number of rows and columns\n");
}
scanf("%d %d", &m, &n);
}
}
Prepared by Amrutha N
Output
Enter the number of rows and columns
// Display sparse matrix representation
44
printf("\nSparse Matrix Representation (row, column, value)\n");
Enter the elements of the matrix
printf("Row\tCol\tValue\n");
for(i = 0; i < k; i++) 0030
{ 0008
printf("%d\t%d\t%d\n", sparse[i].row, sparse[i].col, sparse[i].value); 1030
} 0070
printf("\nTotal non-zero elements = %d\n", k); Sparse Matrix Representation (row, column, value)
return 0; Row Col Value
}
023
138
201
223
327
Total non-zero elements = 5 Prepared by Amrutha N
Transpose of a Matrix
Prepared by Amrutha N
TRANSPOSE OF A MATRIX
• Transpose of a matrix is a matrix that is obtained by swapping the rows and columns
of the given matrix or vice versa, i.e., for the given matrix the elements in rows are
interchanged with the elements in columns.
Prepared by Amrutha N
TRANSPOSE OF A MATRIX
Prepared by Amrutha N
TRANSPOSE OF SPARSE MATRIX
Prepared by Amrutha N
TRANSPOSE OF SPARSE MATRIX
Rows 6 4 1 0 3 5 2
Columns 5 0 1 2 2 3 4
Values 6 9 8 4 2 5 2
Prepared by Amrutha N
TRANSPOSE OF SPARSE MATRIX
Rows 6 4 1 0 3 5 2
Columns 5 0 1 2 2 3 4
Values 6 9 8 4 2 5 2
Final result
Prepared by Amrutha N
TRANSPOSE OF SPARSE MATRIX
Rows 6 0 1 2 3 4 5
Columns 5 2 1 4 2 0 3
Values 6 4 8 2 2 9 5
Prepared by Amrutha N
Transpose of Sparse Matrix Implementation
Prepared by Amrutha N
#include <stdio.h>
struct Sparse
{
int row;
int col;
int value;
};
int main()
{
struct Sparse a[10], b[10];
int i, j, n, k = 1;
// Read sparse matrix
printf("Enter number of rows, columns, and non-zero elements: ");
scanf("%d %d %d", &a[0].row, &a[0].col, &a[0].value);
n = a[0].value;
Prepared by Amrutha N
printf("Enter row, column, and value for each non-zero element:\n");
for (i = 1; i <= n; i++)
{
scanf("%d %d %d", &a[i].row, &a[i].col, &a[i].value);
}
// Transpose dimensions
b[0].row = a[0].col;
b[0].col = a[0].row;
b[0].value = a[0].value;
Prepared by Amrutha N
// Perform transpose
for (i = 0; i < a[0].col; i++)
{
for (j = 1; j <= n; j++)
{
if (a[j].col == i)
{
b[k].row = a[j].col;
b[k].col = a[j].row;
b[k].value = a[j].value;
k++;
}
}
}
Prepared by Amrutha N
// Display transposed sparse matrix
printf("\nTranspose of Sparse Matrix:\n");
printf("Row\tCol\tValue\n");
for (i = 0; i <= n; i++)
{
printf("%d\t%d\t%d\n", b[i].row, b[i].col, b[i].value);
}
return 0;
}
Prepared by Amrutha N
Output Output
Enter number of rows, columns, and non-zero elements Transpose of Sparse Matrix
566 Row Col Value
Enter row, column, and value for each non-zero element 6 5 6
049 0 2 4
118 1 1 8
204 2 4 2
232 3 2 2
355 4 0 9
422 5 3 5
Prepared by Amrutha N