Thread: transpose sparse matrix

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    transpose sparse matrix

    Hello to all
    This is my firts post and topic here and also I'm new and starter in C++

    as you see in topic title here is the code of transpose of sparse matrix
    can anyone tell me completely what does program do line by line

    i have an another easy algorithm but want to know how this works and anyone knows another way to find transpose of this matrix , its good to have it in topic

    thanks a lot


    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    // transpose for the sparse matrix
    
    
    void main()
    {
     clrscr();
     int a[10][10],b[10][10];
     int m,n,p,q,t,col;
     int i,j;
    
     printf("enter the no of row and columns :\n");
     scanf("%d %d",&m,&n);
    
    // assigning the value of matrix
    
     for(i=1;i<=m;i++)
     {
       for(j=1;j<=n;j++)
       {
         printf("a[%d][%d]= ",i,j);
         scanf("%d",&a[i][j]);
         }
      }
    printf("\n\n");
    
    //displaying the matrix
    
     printf("\n\nThe matrix is :\n\n");
    
    for(i=1;i<=m;i++)
     {
       for(j=1;j<=n;j++)
       {
        printf("%d",a[i][j]);
         }
         printf("\n");
       }
    
    
    t=0;
    
    printf("\n\nthe non zero value matrix are :\n\n");
    
    for(i=1;i<=m;i++)
    {
      for(j=1;j<=n;j++)
      {
    
    // accepting only non zero value
      if(a[i][j]!=0)
      {
       t=t+1;
       b[t][1]=i;
       b[t][2]=j;
       b[t][3]=a[i][j];
        }  }
    }
    
    // displaying the matrix of non-zero value
    
    printf("\n");
    printf("a[0 %d %d %d\n",m,n,t);
    
    for(i=1;i<=t;i++)
    {
     printf("a[%d %d %d %d \n",i,b[i][1],b[i][2],b[i][3]);
      }
    
    b[0][1]=n; b[0][2]=m; b[0][3]=t;
    q=1;
    
    // transpose of the matrix
    
    printf("\n\nthe transpose of the matrix :\n ");
    
    if(t>0)
    {
     for(i=1;i<=n;i++)
     {
       for(j=1;j<=t;j++)
       {
        if(b[j][2]==i)
        {
          a[q][1]=b[j][2]; a[q][2]=b[j][1];
          a[q][3]=b[j][3]; q=q+1;
           }  }
         }    }
    
    printf("\n\n");
    printf("a[0 %d %d %d\n",b[0][1],b[0][2],b[0][3]);
    
    for(i=1;i<=t;i++)
    {
     printf("a[%d %d %d %d\n",i,a[i][1],a[i][2],a[i][3]);
        }
     getch();
     }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    First of all, this is 'C' code and not 'C++'.

    Second of all, you're never going to learn how to program by copy/pasting code from the internet.

    Finally, I don't think anyone here is going to explain this to you comprehensively line by line. That would take a long time, and probably wouldn't help you much if you don't understand anything about the language anyway.

    If learning to program is your goal, how about you start with a simple "hello world" program (that you write by yourself)? There are links for tutorials at the top if this very page.

  3. #3
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    How is it you have those code but no idea how it works? Tell us what you do understand.
    If the answer to that is nothing I would suggest the c++ tutorial on this website first.

    Looks like matt beat me to it!

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You need to show some effort, so here's how it goes:
    You tell us what you think each line means, then we'll tell you if you are right. If you can't be bothered doing that, then you can understand why we cant be bothered either.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Transpose Matrix
    By Micki-Zee in forum C Programming
    Replies: 10
    Last Post: 07-01-2011, 02:55 PM
  2. Transpose matrix
    By ineedmunchies in forum C Programming
    Replies: 4
    Last Post: 12-08-2009, 09:09 AM
  3. Help with matrix transpose
    By sass208 in forum C Programming
    Replies: 6
    Last Post: 12-09-2006, 02:02 AM
  4. transpose matrix?
    By tmoney$ in forum C Programming
    Replies: 4
    Last Post: 05-03-2003, 03:51 PM
  5. transpose matrix
    By tmoney$ in forum C Programming
    Replies: 1
    Last Post: 05-03-2003, 02:31 PM