Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6092887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:29:49+00:00 2026-05-23T12:29:49+00:00

I have the following 2d array int [][] array = {{ 0, 1, 2,

  • 0

I have the following 2d array

        int [][] array = {{ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9},
                          {10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
                          {20, 21, 22, 23, 24, 25, 26, 27, 28, 29},
                          {30, 31, 32, 33, 34, 35, 36, 37, 38, 39},
                          {40, 41, 42, 43, 44, 45, 46, 47, 48, 49},
                          {50, 51, 52, 53, 54, 55, 56, 57, 58, 59},
                          {60, 61, 62, 63, 64, 65, 66, 67, 68, 69},
                          {70, 71, 72, 73, 74, 75, 76, 77, 78, 79},
                          {80, 81, 82, 83, 84, 85, 86, 87, 88, 89},
                          {90, 91, 92, 93, 94, 95, 96, 97, 98, 99}};

I have this code to find the sum of all the values in the array. How can I modify it to add only the diagonal values starting at 0 (0+11+22+33 etc.)?

 public static int arraySum(int[][] array)
{
    int total = 0;

    for (int row = 0; row < array.length; row++)
    {
        for (int col = 0; col < array[row].length; col++)
            total += array[row][col];
    }

    return total;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:29:49+00:00Added an answer on May 23, 2026 at 12:29 pm

    Since the diagonals are at perfect square you only need one loop to add the diagonals.


    Adding diagonal from orgin:

    public static int arraySum(int[][] array){
        int total = 0;
    
        for (int row = 0; row < array.length; row++)
        {
    
            total += array[row][row];
        }
    
        return total;
    }
    

    Add both diagonals:

    Adding diagonal from orgin: (note it adds the center twice..you can subtract one if needed)

    public static int arraySum(int[][] array){
        int total = 0;
    
        for (int row = 0; row < array.length; row++)
        {
            total += array[row][row] + array[row][array.length - row-1];
        }
    
        return total;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have an array numbers that contains the following values: int numbers
First of all have a look at the following code (in this code shape
I have the following code: ... int n; cin >> n; int numbers[n]; ...
i have following code for allocation two dimensional array #include <iostream> using namespace std;
I have an assignment that is the following: For a given integer array, find
Suppose I have the following arrays: List<int[]> numbers = new List<int[]>(); numbers.Add(new int[] {
I have this code to find the mean and standard deviation of some input
I have following array, that I need to operate by hand on bitmaps. const
I have the following array. <cfset ItemHasUsers = arrayNew(1)> <cfloop query=qReadData> <cfset ItemHasUsers[qReadData.currentrow][ID] =
I have the following array, $myarray[0]['first_name'] $myarray[0]['last_name'] I want to turn it into: $myarray['first_name']

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.