-
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();
}
-
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.
-
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!
-
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.