Wrap Around 2D Vector

Professor mentions a modulus operator for 2D arrays.
He mentions using a vector<vector<Spot> > tor; for avoiding dynamic mem allocation so I rather use that. Except I don't think a modulus operator would work in this case.

Wrap around as in

1 - > 2 - > 3 - >4 - > 5 it goes to 5 and returns back to 1


https://dl.dropboxusercontent.com/u/53435950/proj.pdf
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
#include <iostream>
#include <cstdlib>
#include <vector>
using std::vector;
using std::cin;
using std::cout;
#include "torus.h"


Torus::Torus(){
vector<vector<Spot> > vec(100,vector<Spot>(100));

}
Torus::Torus(int n){
vector< vector<Spot> > tor(n,vector<Spot>(n));


}

Torus::Torus(int m,int n){
vector<vector<int> > vec(m,vector<int>(n));


}

void Torus::visit(Spot& spot){


};
void Torus::regenerate(double p){


}
int Torus::islands() {
int count=0;
for (int i=0; i<m; i=i+1)
for (int j=0; j<n; j=j+1)
if (tor[i][j].visited==false && tor[i][j].land) {
count=count+1; //new island discovered
visit(tor[i][j]); //visit it
}
return count;
}
Last edited on
i don't understand, what is your question actually?
Ah sorry.
I'm trying to understand how to make a 2D vector that loops itself.
Like the Game of Life except I've done that with a 2D array but differently a[i-1][j], a[i][j-1], etc..
Add a pointer to the spot to an empty vector. Repeatedly, remove the last pointer from the vector and check the up, down, left, and right neighbors of the corresponding spot (they all exist due to the modulo operator.


Oops, my mistake. Professor does say that a modulo operator could work with a vector.
I just don't know how to implement it and why it works.
Last edited on
modulo operator

???A modulo operator : % I don't understand its relevance here .

Your code looks ok,it should work.
if you mean the subscript operator ,then this is how it works:
Say you have a vector : vector<vector<T> v
accessing v.at or v.operator[] returns a vector<T>,
again accessing the at or [] operator , return a T.
so v[i][j] can be broken as
vector<T>& w = v[i];
and further
T &myelement = w[j];.

Hope that helps.
Last edited on
It is part of the extra credit project instructions.
I provided the original instructions in the PDF link for anyone's perusal.
But thank you though that really does help in understanding how vectors work. Eheh. I haven't had much practice with them.
Last edited on
But thank you though that really does help in understanding how vectors work.

???
I told you why subscripting in multidimensional std::vector works not the other way round.
Allow me to rephrase then. Thank you for showing me something that I can try experimenting with in order to solve me case.

Further research of the operator actually gave me some ideas. I'm just hoping it acts the same way as a 2D array wrap around.
Last edited on
Seeing the properties of a tree is difficult when looking at rainforest from planetary orbit.

A 2D array is one thing. A 2D array implemented with vector of vectors is one variant of it.

Modulo operator is an entirely unrelated thing. Modulo is a function that hashes (wraps) any integer into specific range of integers.

Lets say that ve have a list 'foo' with N elements. We refer to the elements with foo[0], foo[1], .. foo[N-1]. Now we take a random (non-negative) number 'x'. If x<N, we can use it as an index to the list. If we calculate the modulo x % N we do get a number that is always in range [0..N[, a valid index.

Lets do that again:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::vector<int> foo;
// fill the foo
// assert: 0 < foo.size()
// Previously, we called foo.size() "N"

size_t x; // index
// set the index to something

// access an element
std::cout << foo[x] << '\n';

// calculate the index of the previous element
size_t xp = (foo.size() + x - 1) % foo.size();
// and access
std::cout << foo[xp] << '\n';

Why so?
If 'x' is 0, then deducting one would create -1. We add first N, so the sum will be N-1.
If 'x' is N-1, we get 2N-2, which is then reduced by the modulo to N-2.

Now we are ready for the 2D:
1
2
3
// we want the tor[i-1][j-1]
vector<Spot> & row = tor[ (tor.size() + i - 1) % tor.size() ];
Spot island = row[ (row.size() + j - 1) % row.size()  ];

Thank you very much for the explanation and examples!
That solved most of my dilemma. :)
Can finally continue with my project.
Hmm. Though I'm really used to using mod for finding primes evens odds and so rather than this.
Need to keep practicing. >_<
Last edited on
Topic archived. No new replies allowed.