Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

/* C Program to find Sum of Diagonal Elements of a Matrix */

#include<stdio.h>

int main()

int i, j, rows, columns, a[10][10], Sum = 0;

printf("\n Please Enter Number of rows and columns : ");

scanf("%d %d", &i, &j);

printf("\n Please Enter the Matrix Elements \n");

for(rows = 0; rows < i; rows++)

for(columns = 0;columns < j;columns++)

scanf("%d", &a[rows][columns]);

for(rows = 0; rows < i; rows++) i+2

Sum = Sum + a[rows][rows];  Sum=0+1 > sum=1

printf("\n The Sum of Diagonal Elements of a Matrix = %d", Sum );


return 0;

OUTPUT

C Program to find Sum of Diagonal Elements of a Matrix 1

ANALYSIS

In this C Program to find Sum of Diagonal Elements of a Matrix example, We declared single Two
dimensional arrays Multiplication of size of 10 * 10.

Below statements ask the User to enter the Matrix size (Number of rows and columns. For instance 2
Rows, 3 Columns = a[2][3] )

printf("\n Please Enter Number of rows and columns : ");

scanf("%d %d", &i, &j);

Next, we used for loop to iterate every cell present in a[3][3] matrix. Conditions inside the for loops
((rows < i) and (columns < j)) will ensure the compiler, not to exceed the matrix limit. Otherwise, the
matrix will overflow

scanf statement inside the for loop will store the user entered values in every individual array
element such as a[0][0], a[0][1], …..

for(rows = 0; rows < i; rows++).

for(columns = 0; columns < j; columns++)

scanf("%d", &a[rows][columns]);

}
In the next line, We have one more for loop to find Sum of Diagonal Elements of a Matrix

for(rows = 0; rows < i; rows++)

Sum = Sum + a[rows][rows];

User inserted values for C Program to find Sum of Diagonal Elements of a Multi-Dimensional Array
example are: a[3][3] = {{10, 20, 30}, { 40, 50, 60}, {70, 80, 90}}

Row First Iteration: for(rows = 0; rows < 3; 0++)

The condition (0 < 3) is True.

Sum = Sum + a[rows][rows]

Sum = Sum + a[0][0] => 0 + 10 = 10

Row Second Iteration: for(rows = 1; rows < 3; 1++)

The condition (1 < 3) is True.

Sum = Sum + a[1][1]

Sum = 10 + 50 = 60

Row Second Iteration: for(rows = 2; rows < 3; 2++)

The condition (2 < 3) is True.

Sum = Sum + a[2][2]

Sum = 60 + 90 = 150

Next, rows value will increment. After the increment, the condition inside the for loop (rows < 3) will
fail. So it will exit from the loop.

At last, we used the printf statement to print the total Sum as output.
Alternate:

#include<stdio.h>
  void main()
 {
   int a[10][10],sum=0;
   int i,j,m,n;
   clrscr();
   printf("Enter Order of Matrix = ");
   scanf("%d%d",&m,&n);
   if(m!=n)
     {
          printf("Not a Square Matrix : ");
          getch();
          exit();
     }
          printf("Enter Elements : ");
          for(i=0;i<m;i++)
              {
                  for(j=0;j<n;j++)
                      {
                         scanf("%d",&a[i][j]);
                      }
               }
                  for(i=0;i<m;i++)
                      {
                          for(j=0;j<n;j++)
                              {
                                    if(i==j)
                                      {
                                          sum+=a[i][j];
                                       }
                               }
                       }
                   printf("Sum of Diagonal Elements = %d ",sum);
                   getch();
 }

Previous:
 Stacks in C
 Making a stack using linked list in C

The previous article was all about introducing you to the concepts of a stack. In


this article, we will code up a stack and all its functions using an array.
As we know that we can’t change the size of an array, so we will make an array
of fixed length first (this will be the maximum length of our stack) and then
implement stack on it.

So, let’s do this.

#include <stdio.h>
#define SIZE 10

int stack[SIZE];

Here, we have created an array name ‘SIZE’ of length ‘SIZE’. This ‘SIZE’ is
the maximum length of our stack.

The next thing to do to create a stack is the creation of the ‘top’ pointer. This
pointer will point to the top element of the stack. In the implementation using an
array, this ‘top’ pointer will be a non-negative integer which will always be
equal to the index of the topmost element of the stack and if the stack is not
initialized then we can make it some negative integer (we will make -1 in this
article).

#include <stdio.h>
#define SIZE 10

int stack[SIZE];
int top = -1;

You can see that we have made the ‘top’ equal to -1 because the stack is not yet
initialized.

The next and the most important operations on a stack are push and pop. So,
let’s create them.

push
The steps for push operation are:
1. Check if the ‘top’ is negative or not.

2. If the ‘top’ is negative then make it 0 and put the ‘value’ in the element
having 0 index in the array ‘stack’.

3. If it is not then put the value in the element having index ‘top+1’ in the
array ‘stack’ and increase the top by 1.

These are very simple and logical steps. Let’s code these steps and implement
the push operation.

void push(int value)


{
if(top<SIZE-1)
{
if (top < 0)
{
stack[0] = value;
top = 0;
}
else
{
stack[top+1] = value;
top++;
}
}
else
{
printf("Stackoverflow!!!!\n");
}
}

if(top<SIZE-1) – We are just making sure that our stack is not full and we
can insert more items.

if (top < 0)/*step 1*/


{
 stack[0] = value;/*step 2*/
 top = 0;/*step 2*/
}
else
{
 stack[top+1] = value;/*step 3*/
 top++;/*step 3*/
}
We have just follow the steps mentioned above with these lines of code.

One point to note here is that we only care about the element present till the
index 'top' in the stack. We are not concerned with what is present after the
index 'top' because they are not the part of our stack.
pop
In pop operation, we return the topmost element and then decrease the value of
the ‘top’ by 1. That’s it. Let’s do this.

int pop()
{
if(top >= 0)
{
int n = stack[top];
top--;
return n;
}
}

This is a very simple code. Firstly, we are just making sure that the stack is not
empty and then decreasing the ‘top’ by 1 and returning the previous topmost
element.

Thus, the overall code for the implementation of stack using array is:

#include <stdio.h>
#define SIZE 10

int stack[SIZE];
int top = -1;

void push(int value)


{
if(top<SIZE-1)
{
if (top < 0)
{
stack[0] = value;
top = 0;
}
else
{
stack[top+1] = value;
top++;
}
}
else
{
printf("Stackoverflow!!!!\n");
}
}

int pop()
{
if(top >= 0)
{
int n = stack[top];
top--;
return n;
}
}

int Top()
{
return stack[top];
}

int isempty()
{
return top<0;
}

void display()
{
int i;
for(i=0;i<=top;i++)
{
printf("%d\n",stack[i]);
}
}

int main()
{
push(8);
push(4);

display();
pop();
display();
return 0;
}

Comparing stack using linked list and stack using


array

 Array implementation is easy.

 Array implementation has limited capacity because of using a fixed size


array.

 Array implementation requires less space.

Xxx

You might also like