Wednesday, 13 August 2014

Lower left sparse matrix

#include<stdio.h>
#include<conio.h>
int main(){
    int i, j, a[20][20], size;
    printf("Enter size of matrix: ");
    scanf("%d", &size);
    printf("Enter the elements\n");
    for(i=0;i<size;i++){
        for(j=0;j<size;j++){
            scanf("%d", &a[i][j]);
        }
    }
    printf("\n\n The Matrix is\n");
    for(i=0;i<size;i++){
        for(j=0;j<size;j++){
            printf("%d", a[i][j]);
        }
        printf("\n");
    }
    printf("\n The Sparse Matrix\n");
    for(i=0;i<size;i++){
        for(j=0;j<size;j++){
            if((i==j)||(i>j)){
                printf("%d", a[i][j]);
            }
            else
                printf(" ");
        }
        printf("\n");
    }
    getch();
    return 0;
}
                   



Output:
Enter size of matrix: 3
Enter the elements
1 2 3 4 5 6 7 8 9


 The Matrix is
123
456
789

 The Sparse Matrix
1
45
789

2 comments:

  1. If somebody 'll exceed the limit, as your user defined array size is 20, then it should display warning like exceed the limit.
    And if somebody is entering limit as 3, then lot of memory space is wasted
    Suggest to rectify this problem...
    4.5/5

    ReplyDelete
  2. the matrix can be improved if we allow the user to input the No.of rows n columns...which vl make the program dynamic...but I think it vl work just for n*n matrix...i.e is a square matric... i would be grateful if you could help me to improve it...

    ReplyDelete

Note: only a member of this blog may post a comment.