Advertisement
ayawaska

Untitled

Apr 2nd, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <alloc.h>
  4.  
  5. #define MAX1 3
  6. #define MAX2 3
  7. #define MAXSIZE 9
  8. #define BIGNUM 100
  9.  
  10. struct sparse
  11. {
  12.     int *sp ;
  13.     int row ;
  14.     int *result ;
  15. } ;
  16.  
  17. void initsparse ( struct sparse * ) ;
  18. void create_array ( struct sparse * ) ;
  19. int count ( struct sparse ) ;
  20. void display ( struct sparse ) ;
  21. void create_tuple ( struct sparse *, struct sparse ) ;
  22. void display_tuple ( struct sparse ) ;
  23. void addmat ( struct sparse *, struct sparse, struct sparse ) ;
  24. void display_result ( struct sparse ) ;
  25. void delsparse ( struct sparse * ) ;
  26.  
  27. void main( )
  28. {
  29.     struct sparse s[5] ;
  30.     int i ;
  31.  
  32.     clrscr( ) ;
  33.  
  34.     for ( i = 0 ; i <= 4 ; i++ )
  35.         initsparse ( &s[i] ) ;
  36.  
  37.     create_array ( &s[0] ) ;
  38.  
  39.     create_tuple ( &s[1], s[0] ) ;
  40.     display_tuple ( s[1] ) ;
  41.  
  42.     create_array ( &s[2] ) ;
  43.  
  44.     create_tuple ( &s[3], s[2] ) ;
  45.     display_tuple ( s[3] ) ;
  46.  
  47.     addmat ( &s[4], s[1], s[3] ) ;
  48.  
  49.     printf ( "\nResult of addition of two matrices: " ) ;
  50.     display_result ( s[4] ) ;
  51.  
  52.     for ( i = 0 ; i <= 4 ; i++ )
  53.         delsparse ( &s[i] ) ;
  54.  
  55.     getch( ) ;
  56. }
  57.  
  58. /* initialises structure elements */
  59. void initsparse ( struct sparse *p )
  60. {
  61.     p -> sp = NULL ;
  62.     p -> result = NULL ;
  63. }
  64.  
  65. /* dynamically creates the matrix */
  66. void create_array ( struct sparse *p )
  67. {
  68.     int n, i ;
  69.  
  70.     /* allocate memory */
  71.  
  72.     p -> sp = ( int * ) malloc ( MAX1 * MAX2 * sizeof ( int ) ) ;
  73.  
  74.     /* add elements to the array */
  75. for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
  76.     {
  77.             printf ( "Enter element no. %d:", i ) ;
  78.             scanf ( "%d", &n ) ;
  79.             * ( p -> sp + i ) = n ;
  80.     }
  81. }
  82.  
  83. /* displays the contents of the matrix */
  84. void display ( struct sparse s )
  85. {
  86.     int i ;
  87.  
  88.     /* traverses the entire matrix */
  89. for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
  90.     {
  91.         /* positions the cursor to the new line for every new row */
  92. if ( i % MAX2 == 0 )
  93.             printf ( "\n" ) ;
  94.         printf ( "%d\t", * ( s.sp + i ) ) ;
  95.     }
  96. }
  97.  
  98. /* counts the number of non-zero elements */
  99. int count ( struct sparse s )
  100. {
  101.     int cnt = 0, i ;
  102.  
  103.     for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
  104.     {
  105.         if ( * ( s.sp + i ) != 0 )
  106.             cnt++ ;
  107.     }
  108.     return cnt ;
  109. }
  110.  
  111. /* creates an array that stores information about non-zero elements */
  112. void create_tuple ( struct sparse *p, struct sparse s )
  113. {
  114.     int r = 0 , c = -1, l = -1, i ;
  115.  
  116.     /* get the total number of non-zero elements
  117.        and add 1 to store total no. of rows, cols, and non-zero values */
  118.  
  119.     p -> row = count ( s ) + 1 ;
  120.  
  121.     /* allocate memory */
  122.  
  123.     p -> sp = ( int * ) malloc ( p -> row * 3 * sizeof ( int ) ) ;
  124.  
  125.     /* store information about
  126.        total no. of rows, cols, and non-zero values */
  127.  
  128.     * ( p -> sp + 0 ) = MAX1 ;
  129.     * ( p -> sp + 1 ) = MAX2 ;
  130.     * ( p -> sp + 2 ) = p -> row - 1 ;
  131.  
  132.     l = 2 ;
  133.  
  134.     /* scan the array and store info. about non-zero values
  135.        in the 3-tuple */
  136. for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
  137.     {
  138.         c++ ;
  139.  
  140.         /* sets the row and column values */
  141. if ( ( ( i % MAX2 ) == 0 ) && ( i != 0 ) )
  142.         {
  143.             r++ ;
  144.             c = 0 ;
  145.         }
  146.  
  147.         /* checks for non-zero element
  148.            row, column and non-zero element value
  149.            is assigned to the matrix */
  150. if ( * ( s.sp + i ) != 0 )
  151.         {
  152.             l++ ;
  153.             * ( p -> sp + l ) = r ;
  154.             l++ ;
  155.             * ( p -> sp + l ) = c ;
  156.             l++ ;
  157.             * ( p -> sp + l ) = * ( s.sp + i ) ;
  158.         }
  159.     }
  160. }
  161.  
  162. /* displays the contents of the matrix */
  163. void display_tuple ( struct sparse s )
  164. {
  165.     int i, j ;
  166.  
  167.     /* traverses the entire matrix */
  168.  
  169.     printf ( "\nElements in a 3-tuple: \n" ) ;
  170.  
  171.     j = ( * ( s.sp + 2 ) * 3 ) + 3 ;
  172.  
  173.     for ( i = 0 ; i < j ; i++ )
  174.     {
  175.         /* positions the cursor to the new line for every new row */
  176. if ( i % 3 == 0 )
  177.             printf ( "\n" ) ;
  178.         printf ( "%d\t", * ( s.sp + i ) ) ;
  179.     }
  180.     printf ( "\n" ) ;
  181. }
  182.  
  183. /* carries out addition of two matrices */
  184. void addmat ( struct sparse *p, struct sparse s1, struct sparse s2 )
  185. {
  186.     int i = 1, j = 1, k = 1 ;
  187.     int elem = 1 ;
  188.     int max, amax, bmax ;
  189.     int rowa, rowb, cola, colb, vala, valb ;
  190.  
  191.     /* get the total number of non-zero values
  192.        from both the matrices */
  193.  
  194.     amax = * ( s1.sp + 2 ) ;
  195.     bmax = * ( s2.sp + 2 ) ;
  196.     max = amax + bmax ;
  197.  
  198.     /* allocate memory for result */
  199.  
  200.     p -> result = ( int * ) malloc ( MAXSIZE * 3 * sizeof ( int ) ) ;
  201.  
  202.     while ( elem <= max )
  203.     {
  204.         /* check if i < max. non-zero values
  205.            in first 3-tuple and get the values */
  206. if ( i <= amax )
  207.         {
  208.             rowa =  * ( s1.sp + i * 3 + 0 ) ;
  209.             cola =  * ( s1.sp + i * 3 + 1 ) ;
  210.             vala =  * ( s1.sp + i * 3 + 2 ) ;
  211.         }
  212.         else
  213.             rowa = cola = BIGNUM ;
  214.  
  215.         /* check if j < max. non-zero values
  216.            in secon 3-tuple and get the values */
  217. if ( j <= bmax )
  218.         {
  219.             rowb = * ( s2.sp + j * 3 + 0 ) ;
  220.             colb = * ( s2.sp + j * 3 + 1 ) ;
  221.             valb = * ( s2.sp + j * 3 + 2 ) ;
  222.         }
  223.         else
  224.             rowb = colb = BIGNUM ;
  225.  
  226.         /* if row no. of both 3-tuple are same */
  227. if ( rowa == rowb )
  228.         {
  229.             /* if col no. of both 3-tuple are same */
  230. if ( cola == colb )
  231.             {
  232.                 /* add tow non-zero values
  233.                    store in result */
  234.  
  235.                 * ( p -> result + k * 3 + 0 ) = rowa ;
  236.                 * ( p -> result + k * 3 + 1 ) = cola ;
  237.                 * ( p -> result + k * 3 + 2 ) = vala + valb ;
  238.                 i++ ;
  239.                 j++ ;
  240.                 max-- ;
  241.             }
  242.  
  243.             /* if col no. of first 3-tuple is < col no. of
  244.                second 3-tuple, then add info. as it is
  245.                to result */
  246. if ( cola < colb )
  247.             {
  248.                 * ( p -> result + k * 3 + 0 ) = rowa ;
  249.                 * ( p -> result + k * 3 + 1 ) = cola ;
  250.                 * ( p -> result + k * 3 + 2 ) = vala ;
  251.                 i++ ;
  252.             }
  253.  
  254.             /* if col no. of first 3-tuple is > col no. of
  255.                second 3-tuple, then add info. as it is
  256.                to result */
  257. if ( cola > colb )
  258.             {
  259.                 * ( p -> result + k * 3 + 0 ) = rowb ;
  260.                 * ( p -> result + k * 3 + 1 ) = colb ;
  261.                 * ( p -> result + k * 3 + 2 ) = valb ;
  262.                 j++ ;
  263.             }
  264.             k++ ;
  265.         }
  266.  
  267.         /* if row no. of first 3-tuple is < row no. of
  268.            second 3-tuple, then add info. as it is
  269.            to result */
  270. if ( rowa < rowb )
  271.         {
  272.             * ( p -> result + k * 3 + 0 ) = rowa ;
  273.             * ( p -> result + k * 3 + 1 ) = cola ;
  274.             * ( p -> result + k * 3 + 2 ) = vala ;
  275.             i++ ;
  276.             k++ ;
  277.         }
  278.  
  279.         /* if row no. of first 3-tuple is > row no. of
  280.            second 3-tuple, then add info. as it is
  281.            to result */
  282. if ( rowa > rowb )
  283.         {
  284.             * ( p -> result + k * 3 + 0 ) = rowb ;
  285.             * ( p -> result + k * 3 + 1 ) = colb ;
  286.             * ( p -> result + k * 3 + 2 ) = valb ;
  287.             j++ ;
  288.             k++ ;
  289.         }
  290.         elem++ ;
  291.     }
  292.  
  293.     /* add info about the total no. of rows,
  294.        cols, and non-zero values that the resultant array
  295.        contains to the result */
  296.  
  297.     * ( p -> result + 0 ) = MAX1 ;
  298.     * ( p -> result + 1 ) = MAX2 ;
  299.     * ( p -> result + 2 ) = max ;
  300. }
  301.  
  302. /* displays the contents of the matrix */
  303. void display_result ( struct sparse s )
  304. {
  305.     int i ;
  306.  
  307.     /* traverses the entire matrix */
  308. for ( i = 0 ; i < ( * ( s.result + 0 + 2 ) + 1 ) * 3 ; i++ )
  309.     {
  310.         /* positions the cursor to the new line for every new row */
  311. if ( i % 3 == 0 )
  312.             printf ( "\n" ) ;
  313.         printf ( "%d\t", * ( s.result + i ) ) ;
  314.     }
  315. }
  316.  
  317. /* deallocates memory */
  318. void delsparse ( struct sparse *p )
  319. {
  320.     if ( p -> sp != NULL )
  321.         free ( p -> sp ) ;
  322.     if ( p -> result != NULL )
  323.         free ( p -> result ) ;
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement