img
Question:
Published on: 3 December, 2024

Create a Sparse Matrix in C.

Answer:
#include<stdio.h>
#include<conio.h>
#define MAX1 3
#define MAX2 3
struct sparse
{
 int *sp;
 int row;
};

void initsparse(struct sparse *);
void ctarray(struct sparse *);
void display(struct sparse);


void main()
{
  struct sparse s1,s2;
  int c;
  clrscr();
  initsparse(&s1);
  initsparse(&s2);
  printf("\n\t\tCreation of the MATRIX");
  printf("\n");
  ctarray(&s1);
  printf("\n\t\tThe contents of the matrix");
  display(s1);
  getch();
}
void initsparse(struct sparse *s1)
{
  s1->sp=NULL;
}
void ctarray(struct sparse *s1)
{
  int n,i;
  s1->sp=(int *)malloc(MAX1*MAX2*sizeof(int));
  for(i=0;i<MAX1*MAX2;i++)
  {
      printf("Enter element %d:",i);
      scanf("%d",&n);
      *(s1->sp+i)=n;
  }
}
void display(struct sparse s1)
{
  int i;
  for(i=0;i<MAX1*MAX2;i++)
  {
    if(i%MAX2==0)
    printf("\n");
   printf("%d\t",*(s1.sp+i));
  }
}
Random questions