Here's my problem.

I need to add only the first and last elements of each row, and display them separately.

Ex.
Row 0=11
Row 1=11
Row 2=8

int arr[][]={{5,9,6},{3,2,8},{7,0,1}};
int sum=0;
for(int row=0;row<arr.length;row++){
for(int col=0;col<arr[row].length;col++)
sum=arr[row][col]+ arr[row][col];
System.out.println(sum+" ");

this is my code, and i think i just need some help. advanced thank you to whomever answers this.

follow this example, first

public static void main(String[] args) {
        int arr[][] = {{5, 9, 6}, {3, 2, 8}, {7, 0, 1}};
        for (int row = 0; row < arr.length; row++) {
            for (int col = 0; col < arr[row].length; col++) {
                System.out.println("arr[" + row + "][" + col + "] = " + arr[row][col]);
            }
            System.out.println();
        }
    }

define the first and last INDEX of elements of row
-first - by default -- zero
-last - calculate last INDEX
make sum if your index is first or last

Accessing the first element of row i: arr[i][0]; Accessing the last element of row i: arr[i][arr[i].length - 1]; From here you just need to do the adding :) Give it a go, try to implement it.

Edited 6 Years Ago by apines: n/a

okay umm.. for example the user inputs an array with 3 rows, the first having 4 columns, the second with 3, and the third with 6 columns. i was wondering if i could use the

sum=arr[row][col] + arr[row].length-1;

or something like that. if ever i need the user to input the rows and columns of the array. thank you so much for the help quuba.

hey thanks for that too apines. i was thinking the same thing while washing the dishes lol.

public class AddFirstAndLastEachRow {
    public static void main(String[] args)
    {
        int arr[][]={{5,9,6},{3,2,8},{7,0,1}};
        int sum=0;
        for(int row=0;row<arr.length;row++){
            for(int col=0;col<arr[row].length;col++)
                sum=arr[row][0]+ arr[row][arr[row].length-1];
            System.out.println("The sum of the first and last elements of Row "+row+" is "+sum+".");
        }
            
    }
}

this is the code i got in the end. thanks again for the help ^_^

in this case is no need to use two for-loops (redundant calculations)

for (int row = 0; row < arr.length; row++) {
                //for (int col = 0; col < arr[row].length; col++) {
                sum = arr[row][0] + arr[row][arr[row].length - 1];
                //}
                System.out.println("The sum of the first and last elements of Row " + row + " is " + sum + ".");
            }

other variant:

for (int row = 0; row < arr.length; row++) {
                sum = 0;
                for (int col = 0; col < arr[row].length; col++) {
                    if (col == 0 || col == arr[row].length - 1) {
                        sum += arr[row][col];
                    }
                }
                System.out.println("*The sum of the first and last elements of Row " + row + " is " + sum + ".");
            }
This question has already been answered. Start a new discussion instead.