#include <iostream>
using namespace std;
void TimesTable(int count)
{
for (int j = 1; j < 13; j++)
{
cout << count << " x " << j << " = " << count*j << " " << endl;
}
}
int main()
{
for (int count = 1; count < 13; count++)
{
TimesTable(count);
cout << endl;
}
system("Pause");
}
This prints
"1 x 1 = 1
1 x 2 = 2
2x1 = 2
2x2 = 4
3x1 = 3
3x2 = 6"
and so on. But the thing I want to do is so instead of having them all print in a line downwards, I want to have it in blocks so,
"1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 4 x 1 = 4 5 x 1 = 5
1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 4 x 2 = 8 5 x 2 = 10"
and so on. But obviously it would look better if it didn't then go to 12 horizontally or otherwise you'd have to grab the bar at the bottom of the console window and drag to see the other timestables. I'd like it so that it went to 5 on block line 1 then on the 2nd line of blocks it would be 6 to 10 and the final line 10 to 12.
Yes it's random that it only goes up to 12 but it was an exercise and I've just adapted it into some other monster.
And I guess I could just have more than one cout in the for loop but I swear in the past I've seen someone do this with just a clever way of passing through a variable so that it printed a block of asterisks side by side instead of pretending to do it.
Desired Result (make it easier with symbols instead):
" ***** ^^^^^ &&&&&
***** ^^^^^ &&&&&
***** ^^^^^ &&&&& "
Result as of now:
" *****
*****
*****
^^^^^
^^^^^
^^^^^
&&&&&
&&&&&
&&&&& "
To format columns, use std::setw from <iomanip>
Sets the field width to be used on output operations.
now.
outside, iterate over the lines.
inside, iterate over the columns.
CODE
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int cols_per_line = 5;
for (int line = 1; line < 13; line++)
{
for (int col = 1; col <= cols_per_line; col++)
{
cout << setw(2) << col << " x " << setw(2) << line << " = " << setw(3) << line*col << " ";
}
cout << endl;
}
}
OUTPUT
[root#rnrlabs ~]# ./a.out
1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 4 x 1 = 4 5 x 1 = 5
1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 4 x 2 = 8 5 x 2 = 10
1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 4 x 3 = 12 5 x 3 = 15
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16 5 x 4 = 20
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35
1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40
1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45
1 x 10 = 10 2 x 10 = 20 3 x 10 = 30 4 x 10 = 40 5 x 10 = 50
1 x 11 = 11 2 x 11 = 22 3 x 11 = 33 4 x 11 = 44 5 x 11 = 55
1 x 12 = 12 2 x 12 = 24 3 x 12 = 36 4 x 12 = 48 5 x 12 = 60
Just remove the endl at the end of TimeTable for inner "for"
and add another counter for splitter say:
Edit: change j with count.
int splitter = 0;
for (int j = 1; j < 13; j++, splitter++)
{
cout << j << " x " << count << " = " << count*j << "\t";
if (splitter == 5)
{
cout << endl;
splitter=-1;
}
}
1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 4 x 1 = 4 5 x 1 = 5 6 x 1 = 6
7 x 1 = 7 8 x 1 = 8 9 x 1 = 9 10 x 1 = 10 11 x 1 = 11 12 x 1 = 12
1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 4 x 2 = 8 5 x 2 = 10 6 x 2 = 12
7 x 2 = 14 8 x 2 = 16 9 x 2 = 18 10 x 2 = 20 11 x 2 = 22 12 x 2 = 24
1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 4 x 3 = 12 5 x 3 = 15 6 x 3 = 18
7 x 3 = 21 8 x 3 = 24 9 x 3 = 27 10 x 3 = 30 11 x 3 = 33 12 x 3 = 36
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16 5 x 4 = 20 6 x 4 = 24
7 x 4 = 28 8 x 4 = 32 9 x 4 = 36 10 x 4 = 40 11 x 4 = 44 12 x 4 = 48
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25 6 x 5 = 30
7 x 5 = 35 8 x 5 = 40 9 x 5 = 45 10 x 5 = 50 11 x 5 = 55 12 x 5 = 60
1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36
7 x 6 = 42 8 x 6 = 48 9 x 6 = 54 10 x 6 = 60 11 x 6 = 66 12 x 6 = 72
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42
7 x 7 = 49 8 x 7 = 56 9 x 7 = 63 10 x 7 = 70 11 x 7 = 77 12 x 7 = 84
1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40 6 x 8 = 48
7 x 8 = 56 8 x 8 = 64 9 x 8 = 72 10 x 8 = 80 11 x 8 = 88 12 x 8 = 96
1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45 6 x 9 = 54
7 x 9 = 63 8 x 9 = 72 9 x 9 = 81 10 x 9 = 90 11 x 9 = 99 12 x 9 = 108
1 x 10 = 10 2 x 10 = 20 3 x 10 = 30 4 x 10 = 40 5 x 10 = 50 6 x 10 = 60
7 x 10 = 70 8 x 10 = 80 9 x 10 = 90 10 x 10 = 100 11 x 10 = 110 12 x 10 = 120
1 x 11 = 11 2 x 11 = 22 3 x 11 = 33 4 x 11 = 44 5 x 11 = 55 6 x 11 = 66
7 x 11 = 77 8 x 11 = 88 9 x 11 = 99 10 x 11 = 110 11 x 11 = 121 12 x 11 = 132
1 x 12 = 12 2 x 12 = 24 3 x 12 = 36 4 x 12 = 48 5 x 12 = 60 6 x 12 = 72
7 x 12 = 84 8 x 12 = 96 9 x 12 = 108 10 x 12 = 120 11 x 12 = 132 12 x 12 = 144
Related
I have a vector with 2 elements a_destinations that represent the source and destination with two integers. 2 4 or 5 1...
I have a matrix test as follows:
0 15 15 15 13 13 15 15 13 13 15 15 15 13 15 15
2 1 2 2 2 2 2 2 2 2 10 2 2 2 10 2
6 1 2 3 3 3 6 6 6 6 6 6 6 6 6 6
7 2 2 3 4 4 2 7 7 7 7 7 7 7 7 7
8 3 3 3 4 5 3 3 8 8 3 3 3 8 3 3
9 4 4 4 4 5 4 4 4 9 4 4 4 9 4 4
7 2 2 2 2 2 6 7 7 7 11 11 7 7 11 7
12 6 6 3 3 3 6 7 8 8 12 12 12 12 12 12
9 7 7 7 4 4 7 7 8 9 7 7 7 9 7 7
13 8 8 8 8 5 8 8 8 9 13 13 13 13 13 13
14 1 11 11 11 11 11 11 11 11 10 11 11 11 14 14
12 6 6 12 12 12 6 12 12 12 10 11 12 12 10 12
15 7 7 7 7 7 7 7 7 13 11 11 12 13 11 15
0 12 12 12 9 9 12 12 9 9 12 12 12 13 12 12
15 10 10 10 10 10 10 10 10 10 10 10 10 10 14 15
0 12 12 12 12 12 12 12 12 12 14 12 12 12 14 15
where test[i][j] represents the path from i to j
each time test[i][j] != j we do the loop again
Example:
path from 2 to 4 >> test[2][4] = 3, test[3][4] = 4: we output: 2, 3, 4
path from 1 to 7 >> test[1][7] = 2, test[2][7] = 6 , test[6][7] = 7: we output 1, 2 ,6, 7
I tried as follows:
std::vector<vector<int>> test;
test = Graphe->P; // MATRIX IS FILLED like on top
vector< int > a_destinations; // Vector with the destinations: 2 4 or 5 10 or 1 4 ...
for ( unsigned i = 0; i < test.size(); i++){
for (unsigned j = 0; j< test.size(); j++){
for (unsigned k = 0; k < a_destinations->size() - 1 ; k ++){
if ( a_destinations->at(k) == i && a_destinations->at(k+1) == j ){
if (test[i][j] == a_destinations->at(k+1)){
cout << a_destinations->at(k) << ", " <<test[i][j];
} else {
cout << a_destinations->at(k) << ", " << test[a_destinations->at(k)][j];
}
cout << ", " << test[i][j];// << ", " << a_destinations->at(k+1);
}
}
}
}
But i end up with 2 destinations always.
I'm writing a multi-precision library in C++, using a base of 2^64, and currently I'm working on the mod operation. I'm using Algorithm D described in Donald E. Knuth's 1998 edition of "The Art Of Computer Programming" Vol. 2, Section 4.3.1, for division, which yields a quotient and a remainder. For the mod operation, I'm performing a division, throwing away the quotient in the end. Although Knuth's Algorithm D is very fast if implemented in C++ with some ASM enhancements for the partial division and the concurrent multi-precision multiplication/subtraction in each step, I'm not sure if there is a better way, since throwing away a painstakingly computed result doesn't seem efficient to me.
Unfortunately, it's not possible to get rid of the partial division in Algorithm D, because the partial quotient is required to compute the remainder, by subtracting the product of the partial quotient and the divisor from the dividend iteratively.
I've searched the Internet for alternative solutions, and found the influential papers written by Paul Barrett and Peter L. Montgomery. However, the fancy tricks they use seem to pay off only if lots of mod operations are performed in a row with the same modulus, since they involve heavy precomputations. This is the case in complex operations like modular exponentiation, where the mod of several squares and products is required for a single modulus. Barrett starts with the basic definition of the remainder, r = a - b * (a / b), and changes the division to a multiplication with the reciprocal of b. Then he presents an efficient way to compute this multiplication, which pays off if the reciprocal is computed once for several similar computations. Montgomery transforms the operands into a completely different residue system, where modular arithmetic is cheap, but for the price of transformations to and fro.
Additionally, Both algorithms introduce some restrictions, which need to be met for correct operation. Montgomery, for instance, usually requires the operands to be odd, which is the case in RSA calculations with primes, but which cannot be assumed in the general case. Outside these restrictions, even more overhead for normalizations is required.
So what I need, is an efficient one-shot mod function without overhead and special restrictions. Hence my question is: Is it possible to compute a remainder without computing the quotient in the first place, in a way that is more efficient than division?
One suggestion would be to write a simple function that would calculate A%B=C and store the A, B and C values into an array, then store all the results into a vector. Then print them out to see the relationships of all of the inputs and output values.
There is one thing that can be done to simplify some of this work and that is to know some of the properties of the mod function. These two statements will help you out with the function.
0 mod N = 0
N mod 0 = undefined
Since 0 mod N = 0 we can put a test case for A and if it is 0 we can just use that to populate our array. Likewise if B = 0 we can populate our array's C value with -1 just to represent undefined because you can not perform A mod 0 as the compilation will fail due to division by 0.
I wrote this function to do just that; then I run it through a loop for both A & B from [0,15].
#include <array>
#include <vector>
#include <iostream>
std::array<int, 3> calculateMod(int A, int B) {
std::array<int, 3 > res;
if (A == 0) {
res = std::array<int, 3>{ 0, B, 0 };
}
else if (B == 0) {
res = std::array<int, 3>{ A, 0, -1 };
}
else {
res = std::array<int, 3>{ A, B, A%B };
}
return res;
}
int main() {
std::vector<std::array<int, 3>> results;
int N = 15;
for (int A = 0; A <= N; A++) {
for (int B = 0; B <= N; B++) {
results.push_back(calculateMod(A, B));
}
}
// Now print out the results in a table form:
int i = 0; // Index for formatting output
for (auto& res : results) {
std::cout << res[0] << " % " << res[1] << " = " << res[2] << '\n';
// just for formatting output data to make it easier to read.
i++;
if ( i > N ) {
std::cout << '\n';
i = 0;
}
}
return 0;
}
Here is it's output:
0 % 0 = 0
0 % 1 = 0
0 % 2 = 0
0 % 3 = 0
0 % 4 = 0
0 % 5 = 0
0 % 6 = 0
0 % 7 = 0
0 % 8 = 0
0 % 9 = 0
0 % 10 = 0
0 % 11 = 0
0 % 12 = 0
0 % 13 = 0
0 % 14 = 0
0 % 15 = 0
1 % 0 = -1
1 % 1 = 0
1 % 2 = 1
1 % 3 = 1
1 % 4 = 1
1 % 5 = 1
1 % 6 = 1
1 % 7 = 1
1 % 8 = 1
1 % 9 = 1
1 % 10 = 1
1 % 11 = 1
1 % 12 = 1
1 % 13 = 1
1 % 14 = 1
1 % 15 = 1
2 % 0 = -1
2 % 1 = 0
2 % 2 = 0
2 % 3 = 2
2 % 4 = 2
2 % 5 = 2
2 % 6 = 2
2 % 7 = 2
2 % 8 = 2
2 % 9 = 2
2 % 10 = 2
2 % 11 = 2
2 % 12 = 2
2 % 13 = 2
2 % 14 = 2
2 % 15 = 2
3 % 0 = -1
3 % 1 = 0
3 % 2 = 1
3 % 3 = 0
3 % 4 = 3
3 % 5 = 3
3 % 6 = 3
3 % 7 = 3
3 % 8 = 3
3 % 9 = 3
3 % 10 = 3
3 % 11 = 3
3 % 12 = 3
3 % 13 = 3
3 % 14 = 3
3 % 15 = 3
4 % 0 = -1
4 % 1 = 0
4 % 2 = 0
4 % 3 = 1
4 % 4 = 0
4 % 5 = 4
4 % 6 = 4
4 % 7 = 4
4 % 8 = 4
4 % 9 = 4
4 % 10 = 4
4 % 11 = 4
4 % 12 = 4
4 % 13 = 4
4 % 14 = 4
4 % 15 = 4
5 % 0 = -1
5 % 1 = 0
5 % 2 = 1
5 % 3 = 2
5 % 4 = 1
5 % 5 = 0
5 % 6 = 5
5 % 7 = 5
5 % 8 = 5
5 % 9 = 5
5 % 10 = 5
5 % 11 = 5
5 % 12 = 5
5 % 13 = 5
5 % 14 = 5
5 % 15 = 5
6 % 0 = -1
6 % 1 = 0
6 % 2 = 0
6 % 3 = 0
6 % 4 = 2
6 % 5 = 1
6 % 6 = 0
6 % 7 = 6
6 % 8 = 6
6 % 9 = 6
6 % 10 = 6
6 % 11 = 6
6 % 12 = 6
6 % 13 = 6
6 % 14 = 6
6 % 15 = 6
7 % 0 = -1
7 % 1 = 0
7 % 2 = 1
7 % 3 = 1
7 % 4 = 3
7 % 5 = 2
7 % 6 = 1
7 % 7 = 0
7 % 8 = 7
7 % 9 = 7
7 % 10 = 7
7 % 11 = 7
7 % 12 = 7
7 % 13 = 7
7 % 14 = 7
7 % 15 = 7
8 % 0 = -1
8 % 1 = 0
8 % 2 = 0
8 % 3 = 2
8 % 4 = 0
8 % 5 = 3
8 % 6 = 2
8 % 7 = 1
8 % 8 = 0
8 % 9 = 8
8 % 10 = 8
8 % 11 = 8
8 % 12 = 8
8 % 13 = 8
8 % 14 = 8
8 % 15 = 8
9 % 0 = -1
9 % 1 = 0
9 % 2 = 1
9 % 3 = 0
9 % 4 = 1
9 % 5 = 4
9 % 6 = 3
9 % 7 = 2
9 % 8 = 1
9 % 9 = 0
9 % 10 = 9
9 % 11 = 9
9 % 12 = 9
9 % 13 = 9
9 % 14 = 9
9 % 15 = 9
10 % 0 = -1
10 % 1 = 0
10 % 2 = 0
10 % 3 = 1
10 % 4 = 2
10 % 5 = 0
10 % 6 = 4
10 % 7 = 3
10 % 8 = 2
10 % 9 = 1
10 % 10 = 0
10 % 11 = 10
10 % 12 = 10
10 % 13 = 10
10 % 14 = 10
10 % 15 = 10
11 % 0 = -1
11 % 1 = 0
11 % 2 = 1
11 % 3 = 2
11 % 4 = 3
11 % 5 = 1
11 % 6 = 5
11 % 7 = 4
11 % 8 = 3
11 % 9 = 2
11 % 10 = 1
11 % 11 = 0
11 % 12 = 11
11 % 13 = 11
11 % 14 = 11
11 % 15 = 11
12 % 0 = -1
12 % 1 = 0
12 % 2 = 0
12 % 3 = 0
12 % 4 = 0
12 % 5 = 2
12 % 6 = 0
12 % 7 = 5
12 % 8 = 4
12 % 9 = 3
12 % 10 = 2
12 % 11 = 1
12 % 12 = 0
12 % 13 = 12
12 % 14 = 12
12 % 15 = 12
13 % 0 = -1
13 % 1 = 0
13 % 2 = 1
13 % 3 = 1
13 % 4 = 1
13 % 5 = 3
13 % 6 = 1
13 % 7 = 6
13 % 8 = 5
13 % 9 = 4
13 % 10 = 3
13 % 11 = 2
13 % 12 = 1
13 % 13 = 0
13 % 14 = 13
13 % 15 = 13
14 % 0 = -1
14 % 1 = 0
14 % 2 = 0
14 % 3 = 2
14 % 4 = 2
14 % 5 = 4
14 % 6 = 2
14 % 7 = 0
14 % 8 = 6
14 % 9 = 5
14 % 10 = 4
14 % 11 = 3
14 % 12 = 2
14 % 13 = 1
14 % 14 = 0
14 % 15 = 14
15 % 0 = -1
15 % 1 = 0
15 % 2 = 1
15 % 3 = 0
15 % 4 = 3
15 % 5 = 0
15 % 6 = 3
15 % 7 = 1
15 % 8 = 7
15 % 9 = 6
15 % 10 = 5
15 % 11 = 4
15 % 12 = 3
15 % 13 = 2
15 % 14 = 1
15 % 15 = 0
From the data above we can see that if A == B the result will be 0. We can also see that if B > A then B == A. Finally we can see that there are patterns between odd and even values of A while B < A. If you can understand these patterns then most of it becomes algebraic manipulation. From here the next step would be to create an algorithm that would take all of this data and convert it to its binary equivalence.
I chose the value of N above as 15 for a reason. This is due to the binary representation of all the possible combinations of binary digits before they repeat again. We know that a single byte of data is 8 bits; we know that the values from [0,15] will fit into half of that; for example:
binary byte: hex decimal
0000 0000 0x00 0
...
0000 1111 0xFF 15
After these 15 different sequences of 0s and 1s these patterns will repeat. So by taking the table above you can convert these into binary representations. Now once you examine the representations of A & B inputs with their C outputs in binary and understand the 3 properties of the results that I had mentioned above; you should be able to design an algorithm to quickly compute the modulo of any A B combination quite easily. One trick to remember is that there are 3 other things to take into consideration. The first is what user eerokia had stated:
"In particular, modulo with a power of 2 can be replaced by a bitwise operations."
The next beyond that is are the values even or odd as the even and odd cases do present different patters of A mod B when B < A.
I have give you some tools of information to start out on, but the rest I'll leave up to you including the task of converting the A, B, and C values into their binary representations.
Once you know the binary patterns of both the A and B inputs according to their C outputs and you understand the truth tables of the logical gates - operators such as And - &, Or - |, Nand - (!&), Nor - (!|), Xor - ^ Xnor - (!^) and Not - ! as well as the compliment (~). You should be able to design your algorithm with efficiency.
I am writing a program that uses a hexagon map (obviously in the output seen below it appears as a square, but the numbers will make sense for a hexagon shape) to generate a path from a certain point. 0 indicates the goal, -2 indicates an off limits section, and any other number indicates a distance from that spot to the goal (0). I've written 6 functions to populate surrounding neighbors. These functions feed into another function that populates the map.. or is supposed to. I find with certain inputs, the map population goes awry on the left portion. I've done a desk check and can't figure out why. Any fresh eyes would help greatly, I've been looking at this for some time:
struct Point {
int r;
int c;
};
Queue <Point> q;
Point getNeighbors1(int r, int c) {
int n1r, n1c;
if (r < (ROW-1) ) {
n1r = r+1;
n1c = c;
Point neighborLoc1;
neighborLoc1.r = n1r;
neighborLoc1.c = n1c;
return neighborLoc1;
}
}
Point getNeighbors2(int r, int c) {
int n2r, n2c;
if (r > 0) {
n2r = r-1;
n2c = c;
Point neighborLoc2;
neighborLoc2.r = n2r;
neighborLoc2.c = n2c;
return neighborLoc2;
}
}
Point g
etNeighbors3(int r, int c) {
int n3r, n3c;
if (c < (COL-1) ) {
n3r = r;
n3c = c+1;
Point neighborLoc3;
neighborLoc3.r = n3r;
neighborLoc3.c = n3c;
return neighborLoc3;
}
}
Point getNeighbors4(int r, int c) {
int n4r, n4c;
if (c > 0) {
n4r = r;
n4c = c-1;
Point neighborLoc4;
neighborLoc4.r = n4r;
neighborLoc4.c = n4c;
return neighborLoc4;
}
}
Point getNeighbors5(int r, int c) {
int n5r, n5c;
if (c % 2 == 0) {
if (r > 0 && c < COL-1 ) {
n5r = r-1;
n5c = c+1;
Point neighborLoc5;
neighborLoc5.r = n5r;
neighborLoc5.c = n5c;
return neighborLoc5;
}
}
else {
if (r < (ROW-1) && c < (COL-1) ) {
n5r = r+1;
n5c = c+1;
Point neighborLoc5;
neighborLoc5.r = n5r;
neighborLoc5.c = n5c;
return neighborLoc5;
}
}
}
Point getNeighbors6(int r, int c) {
int n6r, n6c;
if (c % 2 == 0) {
if (r > 0 && c > 0) {
n6r = r-1;
n6c = c-1;
Point neighborLoc6;
neighborLoc6.r = n6r;
neighborLoc6.c = n6c;
return neighborLoc6;
}
}
else {
if (r < (ROW-1) && c > 0) {
n6r = r+1;
n6c = c-1;
Point neighborLoc6;
neighborLoc6.r = n6r;
neighborLoc6.c = n6c;
return neighborLoc6;
}
}
}
//populate grid
void numberScheme (Queue<Point> pQ, int map[ROW][COL]) {
while (!pQ.isEmpty()) {
Point p = pQ.dequeue();
Point n1 = getNeighbors1(p.r, p.c);
if (map[n1.r][n1.c] == -1) {
map[n1.r][n1.c] = map[p.r][p.c] + 1;
pQ.enqueue(n1);
}
Point n2 = getNeighbors2(p.r, p.c);
if (map[n2.r][n2.c] == -1) {
map[n2.r][n2.c] = map[p.r][p.c] + 1;
pQ.enqueue(n2);
}
Point n3 = getNeighbors3(p.r, p.c);
if (map[n3.r][n3.c] == -1) {
map[n3.r][n3.c] = map[p.r][p.c] + 1;
pQ.enqueue(n3);
}
Point n4 = getNeighbors4(p.r, p.c);
if (map[n4.r][n4.c] == -1) {
map[n4.r][n4.c] = map[p.r][p.c] + 1;
pQ.enqueue(n4);
}
Point n5 = getNeighbors5(p.r, p.c);
if (map[n5.r][n5.c] == -1) {
map[n5.r][n5.c] = map[p.r][p.c] + 1;
pQ.enqueue(n5);
}
Point n6 = getNeighbors6(p.r, p.c);
if (map[n6.r][n6.c] == -1) {
map[n6.r][n6.c] = map[p.r][p.c] + 1;
pQ.enqueue(n6);
}
}
}
some example input: goal is at (12, 12), off limits cell: (1, 19). And I get this mess:
9 9 10 11 12 13 14 14 14 13 13 12 12 12 13 13 14 14 15 15
8 9 10 11 12 13 14 13 13 12 12 11 11 11 12 12 13 13 14 -2
9 10 10 11 12 13 13 12 12 11 11 10 10 10 11 11 12 12 13 13
10 11 11 12 12 12 12 11 11 10 10 9 9 9 10 10 11 11 12 12
11 12 12 12 12 11 11 10 10 9 9 8 8 8 9 9 10 10 11 11
11 11 12 11 11 10 10 9 9 8 8 7 7 7 8 8 9 9 10 10
10 10 11 10 10 9 9 8 8 7 7 6 6 6 7 7 8 8 9 9
9 9 10 9 9 8 8 7 7 6 6 5 5 5 6 6 7 7 8 8
8 9 10 9 8 7 7 6 6 5 5 4 4 4 5 5 6 6 7 7
8 9 10 9 8 7 6 5 5 4 4 3 3 3 4 4 5 5 6 7
8 9 10 9 8 7 6 5 4 3 3 2 2 2 3 3 4 5 6 7
8 9 10 9 8 7 6 5 4 3 2 1 1 1 2 3 4 5 6 7
8 9 10 9 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7
8 9 10 9 8 7 6 5 4 3 2 2 1 2 2 3 4 5 6 7
8 9 10 9 8 7 6 5 4 4 3 3 2 3 3 4 4 5 6 7
8 9 10 9 8 7 6 6 5 5 4 4 3 4 4 5 5 6 6 7
9 10 10 9 8 8 7 7 6 6 5 5 4 5 5 6 6 7 7 8
10 10 10 10 9 9 8 8 7 7 6 6 5 6 6 7 7 8 8 9
9 9 10 11 10 10 9 9 8 8 7 7 6 7 7 8 8 9 9 10
8 9 10 11 11 11 10 10 9 9 8 8 7 8 8 9 9 10 10 11
It looks like the way you're calculating directions is off. You would probably do well to name them, instead of getNeighborsx, to getNorthNeighbor, getSouthNeighbor, getNortheastNeighbor, getSouthwestNeighbor, getNorthwestNeighbor, getSoutheastNeighbor, as that would make it easy to identify which functions are doing what and why they might not be behaving as expected.
When I made a hexagonal grid, I defined directions like this:
enum direction {
north, south, northeast, southwest, northwest, southeast
};
And I got relative points from a direction like this:
point point::getRelativePoint(const direction & d) const {
switch (d) {
case north: return point(x + 1, y); //North and south are defined along the X axis, for our purposes
case south: return point(x - 1, y);
case northeast: return point(x, y + 1); //Northeast and Southwest are defined along the Y axis
case southwest: return point(x, y - 1);
case southeast: return point(x - 1, y + 1); //Northwest and Southeast can be defined by adding together other directions: Northwest is North + Southwest, and Southeast is South + Northeast.
case northwest: return point(x + 1, y - 1);
}
}
Your getNeighbors5 and getNeighbors6 functions are what I believe are at fault, because they change direction based on suspect criteria:
Point getNeighbors5(int r, int c) {
int n5r, n5c;
if (c % 2 == 0) {
if (r > 0 && c < COL-1 ) {
n5r = r-1;
n5c = c+1;
Point neighborLoc5;
neighborLoc5.r = n5r;
neighborLoc5.c = n5c;
return neighborLoc5;
}
}
else {
if (r < (ROW-1) && c < (COL-1) ) {
n5r = r+1;
n5c = c+1;
Point neighborLoc5;
neighborLoc5.r = n5r;
neighborLoc5.c = n5c;
return neighborLoc5;
}
}
}
It doesn't make sense that you're changing which direction this is based on which column it's in. SouthEast of a cell (if it's defined as a composite of South and NorthEast) is always going to be -1, +1 of that cell.
I've attached an image of a hexagonal grid, I suggest you use it to work out the positions of these cells. Depending on how you've defined North/NorthEast, you may need to rotate the directions of the Axis' I provided, but it should illuminate where you might have gone wrong.
I'm trying to read an array in c++, filled with values from 0 to 5, For an unimportant reason, I need to count how many numbers 1, numbers 2, numbers 3, numbers 4 and numbers 5 do stand on the 'iii*DAYS'th position, so when iii = 0 and DAYS is 5, I need to know how many numbers 1, numbers 2, numbers 3, numbers 4 and numbers 5 are located on the 0th, 4th, 9th, 14th position. The code I posted does this quite well, but sometimes, gives a very big unlogical value, -36589245 or 99653256, can somebody tell me why this happens ( it does happen +- one in a hunderd times )
DAYS = 28
NURSES = 33
SHIFTS =5
int calculate_penalty_coverage_offspring(int offspring[NURSES*DAYS])
{
int temporary[DAYS];
int count[DAYS][SHIFTS];
penalty_score_offspring_coverage =0;
for (int ii = 0; ii<DAYS; ii++)
{
int een = 0;
int twee = 0;
int drie = 0;
int vier = 0;
int vijf = 0;
for (int iii = 0; iii<NURSES; iii++)
{
temporary[iii] = offspring[(ii+(iii*DAYS))];
}
for(int a = 0 ; a<DAYS ; a++)
{
if(temporary[a]== 1)
{
een++;
count[ii][0] = een;
}
else if(temporary[a] == 2)
{
twee++;
count[ii][1] = twee;
}
else if(temporary[a]== 3)
{
drie++;
count[ii][2] = drie;
}
else if(temporary[a]== 4)
{
vier++;
count[ii][3] = vier;
}
else if(temporary[a] == 5)
{
vijf++;
count[ii][4] = vijf;
}
}
}
for(int ii = 0; ii<DAYS ; ii++)
{
for (int iii =0 ; iii<SHIFTS ; iii++)
{
cout << count[ii][iii] << '\t';
}
cout << '\n';
}
this is the exeptional output where I talked about, as you can see, there is an onlogical value in the output of -31427696 ... I can't see why the function is working good, except for this one time.
1 2 2 4 4
5 2 2 9 5
9 6 3 5 2
8 3 4 3 8
9 3 3 4 6
5 5 6 8 1
6 8 2 2 5
3 5 8 -31427696 7
5 5 2 5 8
5 7 8 2 3
2 7 1 2 10
5 6 3 5 5
4 4 4 6 7
7 4 6 4 6
6 5 6 4 3
5 3 7 4 6
5 5 6 1 7
5 5 1 6 2
4 6 6 4 5
3 3 4 5 9
6 6 5 4 4
5 5 4 4 5
8 4 4 5 3
5 5 4 7 5
4 8 6 3 3
9 1 5 7 3
3 7 5 2 5
2 6 5 7 5
First you say
int temporary[DAYS];
Where
DAYS = 28
Then you do:
for (int iii = 0; iii<NURSES; iii++)
{
temporary[iii] = offspring[(ii+(iii*DAYS))];
}
Where
NURSES = 33
You're trying to access indices that are out of bounds in temporary.
EDIT: Following our discussion in the comments,
You're additionally not initializing your arrays, specifically count:
int count[DAYS][SHIFTS];
Which you then conditionally fill in (partially) later:
if(temporary[a]== 1)
{
een++;
count[ii][0] = een;
}
// ...
Accesses to count afterwards to indices that were not assigned to will result in the garbage numbers you're seeing. You should probably just default the matrix to all zeros like so:
int count[DAYS][SHIFTS] = {0};
How to pick the best uniformed 1d array from the 2d arrays ?
I have two 2d array of : 11 x 10
Example :
4 8 12 12 12 14 16 18 4 1 0
5 7 11 12 13 11 15 18 3 2 1
8 3 12 14 18 19 20 21 8 5 4 ,
8 2 11 12 17 17 19 20 7 4 3 ,
4 7 11 11 11 15 17 19 5 1 1 ,
3 8 11 13 11 15 14 17 4 1 0 ,
4 7 12 13 13 14 16 19 3 1 1 ,
5 9 11 12 13 15 17 19 5 0 1 ,
9 7 25 22 24 18 23 17 3 3 3 ,
4 8 13 13 13 15 17 17 5 2 0 ,
here we have 2d arrays of size 11x10 - Need to analysis and have to find out the common 1d array which has common like.
find the best closing number and its difference- and keep doing for all the corresponding columns in an array .
below answer should be like - finding the corresponding very column and comparing with the next row column - if it has some difference below ( 5 ) take the both column of two rows are same and process for next column of the same row..process untill finding the 1 row where it has at least nearby matches of 5
4 8 11 12 13 13 15 18 4 1 0
why don't you do something like this
int[] count(int[][] array)
int result[11];
for(int x = o; x<= 11;x++)
{
int temp[10];
for(int y = o; y<= 10;y++)
{
temp[y] = array[y][x];
}
result[x] = getHighest(temp);
}
return result;
}
int getHighest(int[] array)
{
int size = array.length;
int[size][1] temp;
for(int x; x<= size;x++)
{
int z = array[x];
temp[z][0]++;
}
int highest = -1;
for(int z; z<= size;z++)
{
int z = array[x];
int h = temp[z][0];
if(h > highest)
{
highest = h;
}
}
return highest;
}
Something like this, but my C++ has gotten a bit of rusty so sorry if there are any mistakes.