Please help me to Write a program "to print the sum of the main and sub diagonal elements"
Thanks all.

Recommended Answers

All 4 Replies

Look up how to write a 2 dimensional array using dynamic memory and then how to access a given element of said array. Then write a routine as to how to access each element of a given row. Then write a routine as to how to access each element of a given column. Then use that practice/knowledge to write a routine as to how to access each element of a given diagonal. Then remove the first two routines from your program.

If you have a problem at any given step first look at your notes, then your reference material and then if you need to, post your code indicating the apparent problem and any supporting information you have such as error or warning messages, etc.

Be sure to comment your code so you know exactly what you're trying to do at each step (and we don't have to guess). The comments can always be removed if you don't like the looks of them.

On continuation of earlier question -Write a program "to print the sum of the main and sub diagonal elements"

I could not get the result of below written program.Please help me.

#include<iostream.h>
void main()
{
  int a[3][3],i,j,sum1=0,sum2=0,z;
  cout<<"Enter the nos";
  for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
      cin>>a[i][j];
    }
  }
  for(i=0;i<3;i++)
  {
    sum1+=a[i][j];
  }
  for(j=0;j<3;j++)
  {
    sum2+=a[j][i];
  }
  cout<<"\n The sum of diagonal elements="<<sum1;
  cout<<"\n The sum of subdiagonal elements="<<sum2;
  cin>>z;
}

Thanks all.

> sum1+=a[i][j]; What is the value of j all through the loops of this line of code?

Perhaps sum1+=a[i][i]; The same kind of argument can be made for the other loop.

I got it. Thanks Salem and forum for Help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.