Publish Events & Email Alerts

Subscribe our email alerts and Publish your events with us

You'll Receive & Get Benefits :

Publish & Subscribe

 Technicalsymposium

DataStructure-Program to add two sparse matrices.

#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#define MAX1 3
#define MAX2 3
#define MAXSIZE 9
#define BIGNUM 100
struct sparse
{
int *sp ;
int row ;
int *result ;
} ;
void initsparse ( struct sparse * ) ;
void create_array ( struct sparse * ) ;
int count ( struct sparse ) ;
void display ( struct sparse ) ;
void create_tuple ( struct sparse *, struct sparse ) ;
void display_tuple ( struct sparse ) ;
void addmat ( struct sparse *, struct sparse, struct sparse ) ;
void display_result ( struct sparse ) ;
void delsparse ( struct sparse * ) ;
void main( )
{
struct sparse s[5] ;
int i ;
clrscr( ) ;
for ( i = 0 ; i <= 4 ; i++ )
initsparse ( &s[i] ) ;
create_array ( &s[0] ) ;
create_tuple ( &s[1], s[0] ) ;
display_tuple ( s[1] ) ;
create_array ( &s[2] ) ;
create_tuple ( &s[3], s[2] ) ;
display_tuple ( s[3] ) ;
addmat ( &s[4], s[1], s[3] ) ;
printf ( "\nResult of addition of two matrices: " ) ;
display_result ( s[4] ) ;
for ( i = 0 ; i <= 4 ; i++ )
delsparse ( &s[i] ) ;
getch( ) ;
}
/* initialises structure elements */
void initsparse ( struct sparse *p )
{
p -> sp = NULL ;
p -> result = NULL ;
}
/* dynamically creates the matrix */
void create_array ( struct sparse *p )
{
int n, i ;
/* allocate memory */
p -> sp = ( int * ) malloc ( MAX1 * MAX2 * sizeof ( int ) ) ;
/* add elements to the array */
for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
{
printf ( "Enter element no. %d:", i ) ;
scanf ( "%d", &n ) ;
* ( p -> sp + i ) = n ;
}
}
/* displays the contents of the matrix */
void display ( struct sparse s )
{
int i ;
/* traverses the entire matrix */
for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
{
/* positions the cursor to the new line for every new row */
if ( i % MAX2 == 0 )
printf ( "\n" ) ;
printf ( "%d\t", * ( s.sp + i ) ) ;
}
}
/* counts the number of non-zero elements */
int count ( struct sparse s )
{
int cnt = 0, i ;
for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
{
if ( * ( s.sp + i ) != 0 )
cnt++ ;
}
return cnt ;
}
/* creates an array that stores information about non-zero elements */
void create_tuple ( struct sparse *p, struct sparse s )
{
int r = 0 , c = -1, l = -1, i ;
/* get the total number of non-zero elements
and add 1 to store total no. of rows, cols, and non-zero values */
p -> row = count ( s ) + 1 ;
/* allocate memory */
p -> sp = ( int * ) malloc ( p -> row * 3 * sizeof ( int ) ) ;
/* store information about
total no. of rows, cols, and non-zero values */
* ( p -> sp + 0 ) = MAX1 ;
* ( p -> sp + 1 ) = MAX2 ;
* ( p -> sp + 2 ) = p -> row - 1 ;
l = 2 ;
/* scan the array and store info. about non-zero values in the 3-tuple */
for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
{
c++ ;
/* sets the row and column values */
if ( ( ( i % MAX2 ) == 0 ) && ( i != 0 ) )
{
r++ ;
c = 0 ;
}
/* checks for non-zero element row, column and non-zero element value is assigned to the matrix */
if ( * ( s.sp + i ) != 0 )
{
l++ ;
* ( p -> sp + l ) = r ;
l++ ;
* ( p -> sp + l ) = c ;
l++ ;
* ( p -> sp + l ) = * ( s.sp + i ) ;
}
}
}
/* displays the contents of the matrix */
void display_tuple ( struct sparse s )
{
int i, j ;
/* traverses the entire matrix */
printf ( "\nElements in a 3-tuple: \n" ) ;
j = ( * ( s.sp + 2 ) * 3 ) + 3 ;
for ( i = 0 ; i < j ; i++ )
{
/* positions the cursor to the new line for every new row */
if ( i % 3 == 0 )
printf ( "\n" ) ;
printf ( "%d\t", * ( s.sp + i ) ) ;
}
printf ( "\n" ) ;
}
/* carries out addition of two matrices */
void addmat ( struct sparse *p, struct sparse s1, struct sparse s2 )
{
int i = 1, j = 1, k = 1 ;
int elem = 1 ;
int max, amax, bmax ;
int rowa, rowb, cola, colb, vala, valb ;
/* get the total number of non-zero values from both the matrices */
amax = * ( s1.sp + 2 ) ;
bmax = * ( s2.sp + 2 ) ;
max = amax + bmax ;
/* allocate memory for result */
p -> result = ( int * ) malloc ( MAXSIZE * 3 * sizeof ( int ) ) ;
while ( elem <= max )
{
/* check if i < max. non-zero values
in first 3-tuple and get the values */
if ( i <= amax )
{
rowa = * ( s1.sp + i * 3 + 0 ) ;
cola = * ( s1.sp + i * 3 + 1 ) ;
vala = * ( s1.sp + i * 3 + 2 ) ;
}
else
rowa = cola = BIGNUM ;
/* check if j < max. non-zero values in secon 3-tuple and get the values */
if ( j <= bmax )
{
rowb = * ( s2.sp + j * 3 + 0 ) ;
colb = * ( s2.sp + j * 3 + 1 ) ;
valb = * ( s2.sp + j * 3 + 2 ) ;
}
else
rowb = colb = BIGNUM ;
/* if row no. of both 3-tuple are same */
if ( rowa == rowb )
{
/* if col no. of both 3-tuple are same */
if ( cola == colb )
{
/* add tow non-zero values
store in result */
* ( p -> result + k * 3 + 0 ) = rowa ;
* ( p -> result + k * 3 + 1 ) = cola ;
* ( p -> result + k * 3 + 2 ) = vala + valb ;
i++ ;
j++ ;
max-- ;
}
/* if col no. of first 3-tuple is < col no. of second 3-tuple, then add info. as it is to result */
if ( cola < colb )
{
* ( p -> result + k * 3 + 0 ) = rowa ;
* ( p -> result + k * 3 + 1 ) = cola ;
* ( p -> result + k * 3 + 2 ) = vala ;
i++ ;
}
/* if col no. of first 3-tuple is > col no. of second 3-tuple, then add info. as it is to result */
if ( cola > colb )
{
* ( p -> result + k * 3 + 0 ) = rowb ;
* ( p -> result + k * 3 + 1 ) = colb ;
* ( p -> result + k * 3 + 2 ) = valb ;
j++ ;
}
k++ ;
}
/* if row no. of first 3-tuple is < row no. of second 3-tuple, then add info. as it is to result */
if ( rowa < rowb )
{
* ( p -> result + k * 3 + 0 ) = rowa ;
* ( p -> result + k * 3 + 1 ) = cola ;
* ( p -> result + k * 3 + 2 ) = vala ;
i++ ;
k++ ;
}
/* if row no. of first 3-tuple is > row no. of second 3-tuple, then add info. as it is to result */
if ( rowa > rowb )
{
* ( p -> result + k * 3 + 0 ) = rowb ;
* ( p -> result + k * 3 + 1 ) = colb ;
* ( p -> result + k * 3 + 2 ) = valb ;
j++ ;
k++ ;
}
elem++ ;
}
/* add info about the total no. of rows, cols, and non-zero values that the resultant array contains to the result */
* ( p -> result + 0 ) = MAX1 ;
* ( p -> result + 1 ) = MAX2 ;
* ( p -> result + 2 ) = max ;
}
/* displays the contents of the matrix */
void display_result ( struct sparse s )
{
int i ;
/* traverses the entire matrix */
for ( i = 0 ; i < ( * ( s.result + 0 + 2 ) + 1 ) * 3 ; i++ )
{
/* positions the cursor to the new line for every new row */
if ( i % 3 == 0 )
printf ( "\n" ) ;
printf ( "%d\t", * ( s.result + i ) ) ;
}
}
/* deallocates memory */
void delsparse ( struct sparse *p )
{
if ( p -> sp != NULL )
free ( p -> sp ) ;
if ( p -> result != NULL )
free ( p -> result ) ;
}


Government Jobs

All State and Central Government Jobs/Railway & Bank Jobs/Alll Competitive Exams-Complete Details.

Software Jobs

Entry Level Software & IT Jobs in Chennai/Bangalore & Leading Metro Cities-Complete Details.

Conference/Symposium

Engineering Colleges & University /Premier Institute (IIT/IIM/etc.,) Events Details.

Off-Campus/Walk-in

All Software Companies & Core Companies Off-Campus Interviews & Walk-in across India-Complete Details.

All Bank Jobs

All Government Bank & Private Bank Jobs-Complete Details.

Internships

All Companies Internships-Complete Details.

IBPS

Probationary Officers/Clerk and Management Trainees & All IBPS Exams/Syllabus-Complete Details.

GATE

All Branches Syllabus & Question Papers-Free Download.

IES-Exam

Indian Engineering Services Syllabus & Question.Papers-Complete Details.

Software Program

Top Programming Source Codes/Lab Manuals with Solutions-Complete Details

Anna University

B.E/B.Tech/M.E/M.Tech/MCA/MBA Latest Syllabus & Question Papers

Scholarships

All Central & State Govt Scholarships/ International Scholarships/Merit Scholarships Details.

Placement Papers

All Companies Placement Papers with Answers - Free Download of Materials

Aptitude

All Aptitude Test Topics with Answers-Complete Details & Free Download

Technical Round Interview

All Companies Technical Round Interview Asked Questions with Answers

Certifications

All Certifications Software & All Branches Certifications-Complete Details.

Group Discussion

Group Discussion Various Topics with Valid Points-Complete Details.

HR Round

All HR Round Questions and Answers/Tips & Do's and Don'ts of HR Round.

General Job

Job Interview Tips & Do's/Dont's/Dress Codes/Body Language-Complete Details.

Placement ebooks

All Companies Placement Materials with Answers-Complete Details.

Technical Round Tips

All Kind of Technical Round Interview Tips and Answers/ Do's and Don'ts

Resume

Resume Writing Tips/Do's & Don'ts-Complete Details.

Career Pages

All MNC Companies Career Pages & Current Openings-Complete Details.

Software Projects

All Software Projects with Source Codes-Free Download & Details.

GRE

GRE Exam Procedure and Complete Details.

TOEFL

TOEFL Exam Procedure and Complete Details.

IELTS

IELTS Exam Procedure and Complete Details

SAT

SAT Exam Procedure and Complete Details

GMAT

GMAT Exam Procedure and Complete Details

MS in USA

MS in USA Procedure and Complete Details

Civil Engineering

Civil Engineering Lecture Notes for All Universities & Lab Manuals for All Semester.-Free Download

Mechanical Engineering

Mechanical Engineering Lecture Notes for All Universities & Lab Manuals for All Semester-Free Download.

Automobile Engineering

Automobile Engineering Lecture Notes for All Universities & Lab Manuals for All Semester-Free Download.

Computer Science

Computer Science Lecture Notes for All Universities & Lab Manuals for All Semester-Free Download.

Information Technology

Information Technology Lecture Notes for All Universities & Lab Manuals for All Semester-Free Download.

MBA

MBA Lecture Notes for All Universities & Lab Manuals for All Semester-Free Download.

Biotechnology

Biotechnology Lecture Notes for All Universities & Lab Manuals for All Semester-Free Download.

Biomedical

Biomedical Lecture Notes for All Universities & Lab Manuals for All Semester-Free Download.

Aeronautical Engineering

Aeronautical Engineering Lecture Notes for All Universities & Lab Manuals for All Semester-Free Download

Chemical Engineering

Chemical Engineering Lecture Notes for All Universities & Lab Manuals for All Semester-Free Download.

Electronics & Communication

Electronics & Communication Engineering Lecture Notes for All Universities & Lab Manuals for All Semester-Free Download.

Electrical & Electronics

Electrical & Electronics Lecture Notes for All Universities & Lab Manuals for All Semester-Free Download.

MCA

MCA Lecture Notes for All Universities & Lab Manuals for All Semester-Free Download.

B.Sc

B.Sc All Branches (Maths/Physics/Computer Science/Electronics/etc.,)-Lecture Notes-Free Download

BCA

BCA Lecture Notes for All Universities & Lab Manuals for All Semester-Free Download.

BBA

BBA Lecture Notes for All Universities & Lab Manuals for All Semester-Free Download.

M.Sc

M.Sc All Branches (Maths/Physics/Computer Science/Electronics/etc.,)-Lecture Notes-Free Download

M.Com

M.Com Lecture Notes for All Universities & Lab Manuals for All Semester-Free Download.

Engineering Maths

All Semester Engineering Maths (M1/M2/M3/M4/etc.,) Lecture Notes-Free Download

Engineering EBooks

All Engineering Branches of ebooks Free Download-Complete Details.

Engineering Physics

All Semester Engineering Physics Lecture Notes-Free Download

GATE Preparation Books

All Branches of GATE Free Preparation eBooks-Free Download.

Engineering Chemistry

All Semester Engineering Chemistry Lecture Notes-Free Download

Software Companies Info

All Software Companies in India - Complete Details.

Admission

Foreign Universities All Course Admission Procedures-Complete Details.

Software Ebooks

All Programming Software free Eboooks-Free Download.

Web Hosting

All Web Hosting Procedure-Complete Details.