//Program to find the sum of the main and right diagonals of a 2D array
import java.io.*;
public class Diagonals
{
    public static void main(String[]args)throws IOException
    {
        int arr[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
        int maindiag=0;int rightdiag=0;
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                if(i==j)
                {
                    maindiag=maindiag+arr[i][j];
                }
            }
        }
        System.out.println("The sum of the main diagonal is "+maindiag);
        int c=3;
        for(int i=0;i<4;i++)
        {
            rightdiag=rightdiag+arr[i][c];
            c--;
        }
        System.out.println("Sum of the right diag. is "+rightdiag);
    }
}

So for this program, how exactly do I write the algorithm?? I mean, what's the standard format?? Thanks in advance :)

Recommended Answers

All 7 Replies

//Program to find the sum of the main and right diagonals of a 2D array
import java.io.*;
public class Diagonals
{
    public static void main(String[]args)throws IOException
    {
        int arr[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
        int maindiag=0;int rightdiag=0;
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                if(i==j)
                {
                    maindiag=maindiag+arr[i][j];
                }
            }
        }
        System.out.println("The sum of the main diagonal is "+maindiag);
        int c=3;
        for(int i=0;i<4;i++)
        {
            rightdiag=rightdiag+arr[i][c];
            c--;
        }
        System.out.println("Sum of the right diag. is "+rightdiag);
    }
}

So for this program, how exactly do I write the algorithm?? I mean, what's the standard format?? Thanks in advance :)

sum-diagonal(A)
1.maindiag =0,rightdiag =0
2. A= {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
3.for i= 1 to 4
4.do
5. for j= 1 to 4
6. if (i=j)
7. then maindiag = maindiag +A[j];
8. print maindiag;
9.c=3;
10.
11. else for i= 1 to 4
12. rightdiag = rightdiag +a[c];
13. print rightdiag;

Thanks sneha_07! But isn't there some sort of convention where "=" signs are replaced by an arrow like "->"? I heard about that in class the other day...but school's out so I can't clarify with my teach :(

sum-diagonal(A)
1.maindiag =0,rightdiag =0
2. A= {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
3.for i-> 1 to 4
4.do
5. for j= 1 to 4
6. if (i=j)
7. then maindiag = maindiag +A[j];
8. print maindiag;
9.c=3;
10.
11. else for i= 1 to 4
12. rightdiag = rightdiag +a[c];
13. print rightdiag;

this hardly makes a difference its jus that there are diffenet conventions of writing and it doesnt matter if u replace -> sign with = its all same its initialization

I'm confused on what you're actually trying to do.
So, without "correcting" any of the code, I split it to contain two methods.
Check these out and see if they are what you're intending to happen when the elements are counted. After that, maybe you'll want to convert that arr[][] to a class itself.

//Program to find the sum of the main and right diagonals of a 2D array
import java.io.*;
public class Diagonals
{
	private static int sumMainDiag(int[][] arr)
	{
		int maindiag=0;
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<4;j++)
			{
				if(i==j)
				{
					maindiag=maindiag+arr[i][j];
				}
			}
        }

		return maindiag;
	}

	private static int sumRightDiag(int[][] arr)
	{
		int rightdiag=0;
		int c=3;

		for(int i=0;i<4;i++)
		{
			rightdiag=rightdiag+arr[i][c];
			c--;
        }

        return rightdiag;
	}

    public static void main(String[]args)throws IOException
    {
        int arr[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
        //
        int maindiag = sumMainDiag(arr);
        int rightdiag = sumRightDiag(arr);
        //
        System.out.println("The sum of the main diagonal is "+maindiag);
        System.out.println("Sum of the right diag. is "+rightdiag);
    }
}

i am not getting you you were talking about conventions and now you said you are splitting the code
this wont chnage the logic rite but algo for this will chnage

I'm confused on what you're actually trying to do.
So, without "correcting" any of the code, I split it to contain two methods.
Check these out and see if they are what you're intending to happen when the elements are counted. After that, maybe you'll want to convert that arr[][] to a class itself.

//Program to find the sum of the main and right diagonals of a 2D array
import java.io.*;
public class Diagonals
{
	private static int sumMainDiag(int[][] arr)
	{
		int maindiag=0;
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<4;j++)
			{
				if(i==j)
				{
					maindiag=maindiag+arr[i][j];
				}
			}
        }

		return maindiag;
	}

	private static int sumRightDiag(int[][] arr)
	{
		int rightdiag=0;
		int c=3;

		for(int i=0;i<4;i++)
		{
			rightdiag=rightdiag+arr[i][c];
			c--;
        }

        return rightdiag;
	}

    public static void main(String[]args)throws IOException
    {
        int arr[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
        //
        int maindiag = sumMainDiag(arr);
        int rightdiag = sumRightDiag(arr);
        //
        System.out.println("The sum of the main diagonal is "+maindiag);
        System.out.println("Sum of the right diag. is "+rightdiag);
    }
}

I didn't have any problem as far as the program was concerned, although your version's better and more 'neater' than mine!! My prob was writing the algorithm for the program, as I wasn't sure about how the naming conventions and stuff are like in an algorithm.

ohh so its all if u want you can replace = wit -> for making it beautiful
however i shall tell u that algo can be written in simple english language too
but in most of the standard books like coreman n all its written in pascal

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.