Click here to Skip to main content
15,891,689 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<stdio.h>
#include<conio.h>
void main()
{
 int A[10][10],B[10][10],row,col,s=0,i,j;

 printf("\nEnter the row x column size of the sparse matrix\n");
 scanf("%d%d",&row,&col);
 printf("\nEnter the elements in the sparse matrix(mostly zeroes)\n");
 for(i=0;i<row;i++)//and for i is the 
 {
  for(j=0;j<col;j++)
  {
   printf("\nrow %d and column %d:   ",i,j);
   scanf("%d",&A[i][j]);
  }
 }
 printf("\n\nThe given matrix is:\n");
 for(i=0;i<row;i++)
 {
  for(j=0;j<col;j++)
  {
   printf("%d ",A[i][j]);
  }
  printf("\n");
 }
 for(i=0;i<row;i++)
 {
  for(j=0;j<col;j++)
  {
   if(A[i][j]!=0)
   {
    B[s][0]=A[i][j];
    B[s][1]=i;
    B[s][2]=j;
    s++;
   }
  }
 }
 printf("\n\nThe sparse matrix is given by VALUE, ROW, COLUMN");
 for(i=0;i<s;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("%d ",B[i][j]);
  }
  printf("\n");
 }
 getch();
}


What I have tried:

i get this code in google and i dont understand what does this certain line of code used for/ mean.Hope you could help me in this
Posted
Updated 4-Nov-21 2:24am
v2

It is testing the values held in a two-dimensional array

If a value is non-zero it is attempting to assign that value into another array, starting at the first location so that all the values in the 2nd array are contiguous (continuous, no gaps). It is also attempting to capture the original location (i,j)

However, this code is very old (it uses conio.h for example) and I'm not convinced it will give you the answers you are looking for. Also bear in mind that if you found it on Google your tutor can probably find it too ... and probably also this question. That is a sure-fire way to fail a course.

Go back the question you were set and work your way through it using your course materials. Write your own code instead of just copying something off the internet. If you get really stuck then come back with your code and state your problem. We will try to help those who help themselves
 
Share this answer
 
To add to what Chill60 said, first off, that not "One line of code", it's a "block of code" - there is a big difference!
And that is poor code, even by student standards - it's undocumented, hard to read, and not even slightly user friendly. You try it: enter a 20 x 20 matrix and see how far you can get before the app crashes or just locks up because you miskeyed, or lost count ...

Don't use scanf to process user input: read it into a buffer, and process it with sscanf instead with error reporting instead of a failure when the user mistypes.
And prompt the user: "Values for row n, please: ", and "Row n: Value for column m, please: " helps him keep track of what he is doing and what he needs to do.

Me? I'd throw the whole lot in the bin and write something half decent using functions to break it up, make it more readable, and use more than one character names for my variables ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900