English

Write a program to accept an integer double dimensional array and find the sum of its border and the sum of its non border elements separately.

Advertisements
Advertisements

Question

Write a program to accept an integer double dimensional array and find the sum of its border and the sum of its non border elements separately.

Code Writing
Advertisements

Solution

import java.util.*;
public class DDA3
{
    void main()
    {
        Scanner sc = new Scanner(System.in);
        int ar[ ][ ] = new int[4][5];
        int r, c;        
        System.out.println("Filling the array: ");
        for(r = 0; r < 4; r++)
        {
            for(c = 0; c < 5; c++)
            {
                //System.out.print("Cell[" + r+"]["+ c +"] : " );
                ar[r][c] = (int)(Math.random() * 100); 
            }
        }        
        System.out.println("The array is : ");
        for(r = 0; r < 4; r++)
        {
            for(c = 0; c < 5; c++)
            {
                System.out.print(ar[r][c]+"\t");
            }
            System.out.println();
        }        
        int sumBorder = 0, sumNonBorder = 0, sumAll = 0;
        System.out.println("The calculation of elements : ");        
        for(r = 0; r < 4; r++)
        {
            for(c = 0; c < 5; c++)
            {
                sumAll = sumAll + ar[r][c];                
                if(r == 0 || r == 3)
                {
                    System.out.print(" + " + ar[r][c]);
                    sumBorder = sumBorder + ar[r][c];
                }
                if((c=0 && r!= 0 &&r !=3) ||(c=4&&r!= 0 && r != 3 ))
                 }
                    System.out.print(" + " + ar[r][c]);
                    sumBorder = sumBorder + ar[r][c];
                 }     
            }
        }   
        sumNonBorder = sumAll - sumBorder;        
        System.out.println("The Sum of all elements : " + sumAll);
        System.out.println("The Sum of Border elements : " + sumBorder);
        System.out.println("The Sum of Non Border elements : " + sumNonBorder);
    }
}

Output:

Filling the array:
The array is :
17 21 50 72 45
96 79 31 66 68
1 55 94 77 88
50 80 97 75 11
The Sum of elements:
+ 17+21 + 50 + 72 + 45 + 96 + 68 + 1 + 88 + 50 + 80+97 + 75 + 11 The Sum 
of all elements: 1173
The Sum of Border elements : 771 
The Sum of Non Border elements : 402

shaalaa.com
  Is there an error in this question or solution?
Chapter 11: Programs Overall - Double Dimensional Arrays [Page 252]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 11 Programs Overall
Double Dimensional Arrays | Q 3. | Page 252
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×