Showing posts with label 2 Dimensional Arrays. Show all posts
Showing posts with label 2 Dimensional Arrays. Show all posts

Sunday, 14 August 2016

Sparse Matrix: Fast Transpose

Sparse Matrix: Fast Transpose

#include<stdio.h>
#include<stdlib.h>
#define MAX 100

typedef struct
{
            int row;
            int col;
            int val;
}sparse;


void readsparse(sparse a[], int m, int n)
{
            int i, j, k, item, p;
            a[0].row = m;
            a[0].col = n;
            k = 1;

            printf("\nEnter the elements:\n");
            for(i=0; i<m; i++)
            {
                        for(j=0; j<n; j++)
                        {
                                    scanf("%d",&item);
                                    if(item == 0)
                                                continue;
                                    a[k].row = i;
                                    a[k].col = j;
                                    a[k].val = item;
                                    k++;
                        }
            }
            a[0].val = k-1;

            printf("\nThe entered sparse matrix is:\n");
            printf("\nRow\tColumn\tValue\n");
            for(p=0;  p<=a[0].val;  p++)
            {
                        printf("%d\t", a[p].row);
                        printf("%d\t", a[p].col);
                        printf("%d\n", a[p].val);
            }
}

void fast_transpose(sparse a[],  sparse b[])
{
            int row_terms[100],  start_pos[100];
            int i,  j,  p;       

            int numTerms = a[0].val;
            int numCols = a[0].col;
             
            b[0].row = numCols;
            b[0].col = a[0].row;
            b[0].val = numTerms;
            if(numTerms>0)
            {
                        for(i =0; i<numCols;  i++)
                                                row_terms[i] = 0;

                        for(i=1; i<=numTerms; i++)
                                                row_terms[a[i].col]++;

                        start_pos[0]=1;

                        for(i=1; i<numCols; i++)
                                                start_pos[i] = start_pos[i-1] + row_terms[i-1];

                        for(i=1; i<=numTerms; i++)
                        {
                                                j = start_pos[a[i].col]++;
                                    b[j].row = a[i].col;
                                                b[j].col = a[i].row;
                                                b[j].val = a[i].val;
                        }
             }
            printf("\nThe Fast Transpose sparse matrix is:\n");
            printf("\nRow\tColumn\tValue\n");
            for(p=0; p<=a[0].val; p++)
            {
                        printf("%d\t", b[p].row);
                        printf("%d\t", b[p].col);
                        printf("%d\n", b[p].val);
            }
}
void main()
{
            int m, n, key;
            sparse a[MAX], b[MAX];
            printf("\nEnter  the no of rows and columns:\t");
            scanf("%d%d",&m, &n);

            readsparse(a, m, n);
             fast_transpose(a, b);
}

Output:
Enter  the no of rows and columns:      6 6


Enter the elements:
15   0   0   22   0   -15
0   11   3   0    0   0
0   0   0   -6   0   0
0   0    0    0   0   0
91  0   0   0   0   0
0   0   28  0   0   0

The entered sparse matrix is:

Row     Column  Value
6       6       8
0       0       15
0       3       22
0       5       -15
1       1       11
1       2       3
2       4       -6
4       1       91
5       3       28

The Fast Transpose sparse matrix is:

Row    Column           Value
6          6                      8
0          0                      15
1          1                      11
1          4                      91
2          1                      3
3          0                      22
3          5                      28
4          2                      -6
5          0                      -15

Monday, 8 August 2016

2 Dimensional Arrays: Addresses and Values

2 Dimensional Arrays: Addresses and Values 

#include<stdio.h>
void main()
{
           int a[3][4] = {      {10,11,12,13}, {20,21,22,23}, {30,31,32,33}      };
           int i, j;

           for(i=0; i<3;  i++)
                    printf("\n *(a+%d) is %d", i, *(a + i) );

           for(i=0;i<3;i++)
           {
                    for(j=0; j<4; j++)
                   {
                               printf("\n (*(a+%d) + %d) is %d", i, j, (*(a + i) + j) );
                    }
           }

           for(i=0;i<3;i++)
          {
                    for(j=0; j<4; j++)
                   {
                              printf("\n *(*(a+%d) + %d) is %d", i, j, *(*(a + i) + j) );
                   }
          }
}


Output:

 *(a+0) is 2686736
 *(a+1) is 2686752
 *(a+2) is 2686768

--------------------------------------
 (*(a+0) + 0) is 2686736
 (*(a+0) + 1) is 2686740
 (*(a+0) + 2) is 2686744
 (*(a+0) + 3) is 2686748

 (*(a+1) + 0) is 2686752
 (*(a+1) + 1) is 2686756
 (*(a+1) + 2) is 2686760
 (*(a+1) + 3) is 2686764

 (*(a+2) + 0) is 2686768
 (*(a+2) + 1) is 2686772
 (*(a+2) + 2) is 2686776
 (*(a+2) + 3) is 2686780

------------------------------------
 *(*(a+0) + 0) is 10
 *(*(a+0) + 1) is 11
 *(*(a+0) + 2) is 12
 *(*(a+0) + 3) is 13

 *(*(a+1) + 0) is 20
 *(*(a+1) + 1) is 21
 *(*(a+1) + 2) is 22
 *(*(a+1) + 3) is 23

 *(*(a+2) + 0) is 30
 *(*(a+2) + 1) is 31
 *(*(a+2) + 2) is 32
 *(*(a+2) + 3) is 33