简体   繁体   中英

C++ Search a 2D vector for elements surrounding a chosen element?

Stuck here in my assignment. I am working with 2D Vectors. What my professor wants us to do is write a program that has the user enter a size of a matrix (NXN) and print the matrix with random 1's and 0's, which I have done.

What I am stuck is that he wants to find "nonzero" elements around a certain element. For instance:

0 0 0
0 1 1
1 1 1

Now the user is asked to type in a row and column to (to locate an element) then search for nonzero values adjacent to that element. So if rows and columns start at 0, row 1 and column 1 holds the value "1" (the center of the matrix) and has 4 adjacent nonzero elements. I am not quite sure where to go from here. Would I use the find code? I am not sure how to limit that to the adjacent locations of one element.

Thank you

Hint : if you want to look at the adjacent elements, you can just shift each index by one position. For example, if the given (row, column) is (1, 1), the adjacent positions are (0, 1), (2, 1), (1, 0), (1, 2). You should make sure your code only reads indices in the range (0..N, 0..N).

This is your assignment and you should do your best to finish it. Go for it and make us proud!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM