C Language | Arrays 13

Question:6.
C program to Take input for the elements of a double dimension array of dimension R X C. Further display the elements in the form of a matrix. Also calculate and print sum of left diagonal elements
1 2 3
4 5 6
7 8 9
Sum : 15 ( ie Sum of (3+5+7))

Note: condition: Matrix should be a square matrix (i.e. number of rows and cols should be same)

Sol:

/* sum of Left diagonal elements */
#include<stdio.h>
#include<stdlib.h> //exit()

int main()
{
    int a[10][10],r,c,i,j,s=0;
    /* clrscr(); */
    printf("Enter total rows and cols ");
    scanf("%d %d",&r,&c);
    if(r==c)
        printf("Sum of diagonal elements can be calculated\n");
    else
    {
        printf("Sum of diagonal elements cannot be calculated\n");
        getch();
        exit(1);
    }
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            printf("Enter [%d][%d] element ",i,j);
            scanf("%d",&a[i][j]);
        }
    }
    for(i=0;i<r;i++)
    {
            for(j=0;j<c;j++)
            {
                printf("%4d",a[i][j]);
                if(i+j==r-1) //or i+j==c-1)
                    s=s+a[i][j];

            }
            printf("\n");
    }
    printf("Sum of left diagonal elements = %d\n",s);

    return 0;
}
/* Output */

case:1

Enter total rows and cols 3
6
Sum of diagonal elements cannot be calculated

case 2:

Enter total rows and cols 3
3
Sum of diagonal elements can be calculated
Enter [0][0] element 2
Enter [0][1] element 6
Enter [0][2] element 9
Enter [1][0] element 3
Enter [1][1] element 6
Enter [1][2] element 5
Enter [2][0] element 8
Enter [2][1] element 5
Enter [2][2] element 2
   2   6   9
   3   6   5
   8   5   2
Sum of left diagonal elements = 23

Question:7.
C program to Take input for the elements of a double dimension array of dimension R X C. Further display the elements in the form of a matrix. Also calculate and print sum of right and left diagonal elements
1 2 3
4 5 6
7 8 9
Sum right diagonal elements = 15 (1+5+9)
Sum left diagonal elements = 15 (3+5+7)

Note: condition: Matrix should be a square matrix (i.e. number of rows and cols should be same)

Sol:

/* Sum of Left and right  diagonal elements */
#include<stdio.h>
#include<stdlib.h> //exit()

int main()
{
    int a[10][10],r,c,i,j,s1=0,s2=0;
    /* clrscr(); */
    printf("Enter total rows and cols ");
    scanf("%d %d",&r,&c);
    if(r==c)
        printf("Sum of diagonal elements can be calculated\n");
    else
    {
        printf("Sum of diagonal elements cannot be calculated\n");
        getch();
        exit(1);
    }
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            printf("Enter [%d][%d] element ",i,j);
            scanf("%d",&a[i][j]);
        }
    }
    for(i=0;i<r;i++)
    {
            for(j=0;j<c;j++)
            {
                printf("%4d",a[i][j]);
                if(i==j)
                    s1=s1+a[i][j];
                if(i+j==r-1) //or i+j==c-1)
                    s2=s2+a[i][j];
            }
            printf("\n");
    }
    printf("Sum of right diagonal elements = %d\n",s1);
    printf("Sum of left diagonal elements = %d\n",s2);

    return 0;
}
/* Output */

Enter total rows and cols 3
3
Sum of diagonal elements can be calculated
Enter [0][0] element 1
Enter [0][1] element 2
Enter [0][2] element 3
Enter [1][0] element 4
Enter [1][1] element 5
Enter [1][2] element 6
Enter [2][0] element 7
Enter [2][1] element 8
Enter [2][2] element 9
   1   2   3
   4   5   6
   7   8   9
Sum of right diagonal elements = 15
Sum of left diagonal elements = 15

Question:8.
C program to Take input for the elements of a double dimension array of dimension R X C. Print transpose of matrix?

Normal matrix

1 2 3
4 5 6
7 8 9

Transpose of matrix
1 4 7
2 5 8
3 6 9
Sol:

#include<stdio.h>

int main()
{
    int a[10][10],r,c,i,j;
    /* clrscr(); */
    printf("Enter total rows and cols ");
    scanf("%d %d",&r,&c);
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            printf("Enter [%d][%d] element ",i,j);
            scanf("%d",&a[i][j]);
        }
    }
    printf("\nNormal Matrix\n");
    for(i=0;i<r;i++)
    {
            for(j=0;j<c;j++)
            {
                printf("%4d",a[i][j]);
            }
            printf("\n");
    }
    printf("\nTranspose  Matrix\n");
    for(j=0;j<c;j++)
    {
            for(i=0;i<r;i++)
            {
                printf("%4d",a[i][j]);
            }
            printf("\n");
    }
    return 0;
}
/* Output */

Enter total rows and cols 3
3
Enter [0][0] element 1
Enter [0][1] element 2
Enter [0][2] element 3
Enter [1][0] element 4
Enter [1][1] element 5
Enter [1][2] element 6
Enter [2][0] element 7
Enter [2][1] element 8
Enter [2][2] element 9

Normal Matrix
   1   2   3
   4   5   6
   7   8   9

Transpose  Matrix
   1   4   7
   2   5   8
   3   6   9

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects