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

  • Home
  • SEARCH
  • 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 1115307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:08:15+00:00 2026-05-17T03:08:15+00:00

Ok, so I have an array like so: 1 2 3 4 5 6

  • 0

Ok, so I have an array like so:

1 2 3
4 5 6
7 8 9

Doesn’t necessarily have to be any specific size, just showing it so you guys have something to look at.
So I have to go through each number, and calculate the average of the numbers that are surrounding it. For example, for the number 1 I have to calculate the average of 2, 5, and 4. I have to compare that average to the number itself, but that’s not my issue. My issue is running through an array of any size (determined by the user) and examining the numbers around it.
Now, I could program a separate method for each section, meaning a separate method for each corner, a separate method for each side, and a method for the center. But that sounds like a lot more work and a lot less efficient. So, I’ve decided to make a sort of border around the outside. This border will be filled with 0’s, assuming that 0 will never be an input value. This is so that I will not run out of my array, but i can run one coded function on the inside of this border, so that it calculates the average of the 8 surrounding numbers.

So I’m wondering how I could go about running through an array and ignore any 0’s it comes across, but take the average of the other numbers around it?

Example:
0 0 0 0 0
0 1 2 3 0
0 4 5 6 0
0 7 8 9 0
0 0 0 0 0

so in the first spot, it’d take the average of the numbers 2, 4, and 5, but ignore the 0’s. the reason I need to ignore them is because:
1. I cannot include them in the number count because even though adding 0 won’t affect the total, it’ll affect the amount of numbers to divide by in obtaining the average
2. I won’t actually be using 0 since 0 is a possible input, it’ll actually be -1. I put 0 here for the ease of typing and readability.

Suggested best method of going about this?

EDIT2:

So now, I’m getting an output that’s close to what I should be getting. But I’ve thoroughly looked through and I can’t see where this error is coming about.

public static int calculate(int[][] array, int row, int col){
    double num = 0;//Used to find the total of real numbers to be divided by
    double total = 0;
    double average;

    for (int a = -1; a <= 1; a++) 
    {
        for (int b = -1; b <= 1; b++) 
        {
            if(row + a < 0 || col + b < 0)
            {
                continue;
            }
            if(a == 0 && b == 0)//if(row + a == row || col + b == col)
            {
                continue;
            }
            if(row + a >= r || col + b >= c)
            {
                continue;
            }
            else
            {                   
                num+=1;
                total+= array[row + a][b + col];
                System.out.println("row+a = " + (row + a) + " and col+b = " + (col + b));
            }
        }
     }

    average = (double)(total/num);
    count++;
    System.out.print(count + ". Average = " + total + " divided by " + num + " = " + average + "\n");

    if (average < array[row][col])
        return 255;
    else
        return 0;
}

Into this function, I put each value in the array (going through each row) to be changed to either 0 or 255.
Apparently, the correct final result should be:
0 255 0 255 0

0 0 255 0 0

255 0 255 0 255

255 0 0 0 0

0 255 0 0 255

But I’m getting

0 255 0 255 0

0 0 0 0 0

255 0 255 0 255

0 0 0 0 0

255 0 255 0 255

So I’m looking into those specific differences and I’m going to see where the issue is.

  • 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-17T03:08:16+00:00Added an answer on May 17, 2026 at 3:08 am

    So, I’ve decided to make a sort of border around the outside.

    Trivia – Dan and Kathy Spacklen (Spacken?) did this wit their chessboard when they wrote Sargon 30 years ago.

    You can do the border but I think it isn’t going to help much. Tell me, in your first example

    1 2 3
    4 5 6
    7 8 9
    

    Assuming #1 is at [0][0], what would be the indexes for the 8 cells surrounding #2?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

They're both resizable arrays, and std::basic_string doesn't have any specifically character-related functions like upper().
Does VBA have dictionary structure? Like key<>value array?
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
Hello i have array like this [0] => Array ( [ExamMonth] => Array (
An array is defined of assumed elements like I have array like String[] strArray
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
I have an array like this var array = [{ order: 3, sub -
I have an array like: errors = [ {...}, {...}, {...} ] It's an
I have an array like this one $state[1]='FG'; $state[2]='BU'; $state[3]='CA'; ... $state[150]='YT' What I
i have an array like this: $arr = array(array('1', '0', 'nokia'), array('2', '0', 'samsung'),

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.