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 4619982
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:29:54+00:00 2026-05-22T02:29:54+00:00

Given a n*n matrix and a value k, how do we find all the

  • 0

Given a n*n matrix and a value k, how do we find all the neighbors for each element?
for example: in a 4*4 matrix, with k=2
say matrix is :

[ 1  2  3  4
  5  6  7  8
  9 10 11 12
 13 14 15 16]

where these values are the indexes of the location, the neighbors for 1 are 1,2,3,5,6,9 . The values 3,6 and 9 come only because k =2 and wouldnt be there if k was = 1.

similarly the neighbors of 6 will be 1 2 3 5 6 7 8 9 10 11 and 14

Can you please help me to write a c code to implement this in c++.

It is the problem of von Neumann neighborhood, please can some one implement it in c++. Thanks

  • 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-22T02:29:55+00:00Added an answer on May 22, 2026 at 2:29 am

    This should do the trick for k=1. Make minor changes to make it work for all k

    int width = 4;
    int height = 4;
    int k = 1;
    int value = 2;
    
    bool hasRight = (value % width != 0);
    bool hasLeft = (value % width != 1);
    bool hasTop = (value > 4);
    bool hasBottom = (value < (height * width - width));
    
    cout << value;  // Always itself
    if(hasRight == true) {
     cout << value+1 << " ";  // Right
     if(hasTop == true) {
      cout << value-width << " " << value-width+1 << " "; // Top and Top-right
     }
     if(hasBottom == true) {
      cout << value+width << " " << value+width+1; // Bottom and Bottom-right
     }
    }
    
    if(hasLeft == true) {
     cout << value-1 << " ";  // Left
     if(hasTop == true) {
      cout << value-width-1 << " ";  // Top-left
     }
     if(hasBottom == true) {
      cout << value+width-1 << " ";  // Bottom-left
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a matrix of boolean values (example): a b c +-------- X | 1
Given an n-dimensional matrix of values: what is the most efficient way of retrieving
For example, given a matrix: 01|02|03|04|05 06|07|08|09|10 11|12|13|14|15 And knowing that the matrix is
Given a 1*N matrix or an array, how do I find the first 4
Given a value in a matrix how can you get the subscript(s) at which
given a matrix of distances between points is there an algorithm for determining a
Given a NxN matrix with 0s and 1s. Set every row that contains a
Given two 3D vectors A and B, I need to derive a rotation matrix
Given graph, how could i represent it using adj matrix?. I have read lots
Given a specific DateTime value, how do I display relative time, like: 2 hours

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.