C Language | Arrays 12

Question:3.
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 row elements ,get the following output:

1 2 3 6
4 5 6 15
7 8 9 24
Sol:

#include<stdio.h>

int main()
{
    int a[10][10],r,c,i,j,s=0;
    /* 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]);
        }
    }
  for(i=0;i<r;i++)
  {
      s=0;
        for(j=0;j<c;j++)
        {
            printf("%4d",a[i][j]);
            s=s+a[i][j];
        }
        printf(" %4d\n",s);
    }
    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
   1   2   3    6
   4   5   6   15
   7   8   9   24

Question:4.
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 row elements,get the following output:
1 2 3 6
4 5 6 21
7 8 9 45

Sol:

#include<stdio.h>

int main()
{
    int a[10][10],r,c,i,j,s=0;
    /* 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]);
        }
    }
  for(i=0;i<r;i++)
  {
        for(j=0;j<c;j++)
        {
            printf("%4d",a[i][j]);
            s=s+a[i][j];
        }
        printf(" %4d\n",s);
    }
    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
   1   2   3    6
   4   5   6   21
   7   8   9   45

Question:5.
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 diagonal elements
1 2 3
4 5 6
7 8 9
Sum : 15 ( ie Sum of (1+5+9))

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

Sol:

/* Sum of right 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)
                    s=s+a[i][j];

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

    return 0;
}
/* Output */

Case:1

Enter total rows and cols 2
3
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 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

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