answersLogoWhite

0


Best Answer

#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.result + i ) ) ;

}

}

/* deallocates memory */

void delsparse ( struct sparse *p )

{

if ( p -> sp != NULL )

free ( p -> sp ) ;

if ( p -> result != NULL )

free ( p -> result ) ;

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

1. Initialize

l=1

T=0

2. Scan each row

Repeat thru step 9 while l<=M

3. Obtain row indices and starting positions of next rows

J=AROW[l]

K=BROW[l]

CROW[l]=T+1

AMAX=BMAX=0

If l<M

then Repeat for P=l+1, l+2, ......M while AMAX=0

If AROW[P]/=0

then AMAX=AROW[P]

Repeat for P=l+1, l+2,.......M while BMAX=0

If BROW[P]/=0

then BMAX=BROW[P]

If AMAX=0

then AMAX=R+1

If BMAX=0

then BMAX=S+1

4. Scan columns of this row

Repeat thru step 7 while J/=0 and K/=0

5. Elements in same column?

If ACOL[J]=BCOL[K]

then SUM=A[J]+B[K]

COLUMN=ACOL[J]

J=J+1

K=K+1

else If ACOL[J]<BCOL[K]

then SUM=A[J]

COLUMN=ACOL[J]

J=J+1

else SUM=B[K]

COLUMN=BCOL[K]

K=K+1

6. Add new elements to sum of matrices

If SUM/=0

then T=T+1

C[T]=SUM

CCOL[T]=COLUMN

7. End of either row?

If J=AMAX

then J=0

If K=BMAX

then K=0

8. Add remaining elements of a row

If J=0 and K/=0

then repeat while K<BMAX

T=T+1

C[T]=B[K]

CCOL[T]=BCOL[K]

K=K+1

else if K=0 and J/=0

then repeat while J<AMAX

T=T+1

C[T]=A[J]

CCOL[T]=ACOL[J]

J=J+1

9. Adjust index to matrix C and increment row index

If T<CROW[l]

then CROW[l]=0

l=l+1

10. Finished

Exit

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a algorithm in c to add two sparse matrices?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you add two matrices using Linux shell script?

write ashell script to add awo matrix using array.


How do you write and draw an algorithm in flowchart for Add 2 and 4 and print the sum?

2+4


Write the algorithm for the conversion of temperature from Celsius scale to Fahrenheit scale?

Start with C Multiply by 1.8 Add 32 Answer = F


What must be true in order to add matrices?

They must have the same dimensions.


How do you add fractions in matrices?

The usual rules of addition of fractions apply.


Write an algorithm that accept five numbers add them together and output the results instruct?

dont know use google instead dumb


How to write an algorithm that accepts five numbers and displays the sum and average of the numbers?

1.Start Algorithm 2.Enter first number 3.Enter second number 4.Enter third number 5.Enter fourth number 6.Enter fifth number 7.Add five number 8.display five number / 2 9.Display result 10.End Algorithm


Is there any benefit in a C plus plus program to add two matrices?

No.


Flow chart for addition of two matrices?

For the resulting matrix, just add the corresponding elements from each of the matrices you add. Use coordinates, like "i" and "j", to loop through all the elements in the matrices. For example (for Java; code is similar in C):for (i = 0; i


Write an algorithm for perfect number or not?

1. Get any no. 2. Find it factors. 3. Add its factor. 4. If sum of its factors is equal to itself then it is a perfect no. otherwise not


How do you add matrices?

You add matrices by adding their respective terms - e.g. the element in the first row and sixth column of the sum is the sum of the elements in the addends' first rows and sixth columns. Wikipedia has a nice example of matrix addition that I linked below.


What is algorithm and what are its characteristics?

An "algorithm" is simply a method to solve a certain problem. For example, when you use the standard method you learned in school to write down two numbers, one beneath the other, then add them, you are using an algorithm - a method that is known to give correct results in this particular case.