The following is the whole question.
Write a program that simulates the rolling of two dice. The program
should use rand to roll the first die and should use rand again to
roll the second die. The sum of two values should then be calculated.
[Note : Each die can show an integer value from 1 to 6, so the sum of
the two values will vary from 2 to 12, with 7 being the most frequent
sum and 2 and 12 being the least frequent sums.] Note that there are
36 possible combinations of the two dice. Your program should roll the
two dice 3,600 times. Use a one_dimensional array to tally the numbers
of times each possible sum appears. Print the results in a tabular
format. Also, determine if the totals are reasonable (i.e., there are
six ways to roll a 7, so approximately one-sixth of all the rolls
should be 7).
The result should be the following :
Question 2
Please enter the seed : 2
I don't know how to generate the "expected" column.
Here is my program : (The main is Q2_main())
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
double total_Array[11];
double expected_Array[11];
double actual_Array[11];
int seed;
void initialization_of_Array()
{
for (int counter=0; counter < 12; counter++)
{
total_Array[counter] = 0;
expected_Array[counter] = 0;
actual_Array[counter] = 0;
}
}
void show_heading_line()
{
cout << setw(5) << "Sum"
<< setw(10) << "Total"
<< setw(17) << "Expected"
<< setw(16) << "Actual"
<< endl;
}
void show_Data_Results_line(int sum, int total, double expected, double actual)
{
cout << setw(5) << sum
<< setw(10) << total
<< setw(16) << expected << "%"
<< setw(15) << actual << "%"
<< endl;
}
void calculation_of_total()
{
int die_1, die_2;
for (int counter = 1; counter <= 3600; counter++)
{
die_1 = 1 + rand() % 6;
die_2 = 1 + rand() % 6;
total_Array[((die_1 + die_2)-2)]++;
}
}
void calculation_of_expect()
{
}
void calculation_of_actual()
{
for (int counter = 0; counter < 11; counter++)
{
actual_Array[counter] = (total_Array[counter] / 3600.0) * 100.0;
}
}
void rollDice_Operation()
{
calculation_of_total();
calculation_of_expect();
calculation_of_actual();
}
void print_Result()
{
show_heading_line();
for (int counter = 0; counter <= 10; counter++)
{
show_Data_Results_line((counter+2), total_Array[counter], 1, actual_Array[counter]);
}
}
void Q2_main()
{
cout << setprecision(3) << fixed;
initialization_of_Array();
cout << "Please enter the seed : ";
cin >> seed;
srand(seed);
rollDice_Operation();
print_Result();
}
Anyone can give me some hints to deal with the "expected" column?
Thank you for your attention
The expected column is just the mathematical probability of the result:
+-------+-------------------------+--------------------+-------------+
| Value | Possibilities | # of possibilities | Probability |
+-------+-------------------------+--------------------+-------------+
| 2 | 1+1 | 1 | 1/36=2.78% |
| 3 | 1+2,2+1 | 2 | 2/36=5.56% |
| 4 | 1+2,2+2,2+1 | 3 | 3/36=8.33% |
| 5 | 1+4,2+3,3+2,4+1 | 4 | 4/36=11.11% |
| 6 | 1+5,2+4,3+3,4+2,5+1 | 5 | 5/36=13.89% |
| 7 | 1+6,2+5,3+4,4+3,5+2,6+1 | 6 | 6/36=16.67% |
| 8 | 2+6,3+5,4+4,5+3,6+2 | 5 | 5/36=13.89% |
| 9 | 3+6,4+5,5+4,6+3 | 4 | 4/36=11.11% |
| 10 | 4+6,5+5,6+4 | 3 | 3/36=8.33% |
| 11 | 5+6,6+5 | 2 | 2/36=5.56% |
| 12 | 6+6 | 1 | 1/36=2.78% |
+-------+-------------------------+--------------------+-------------+
You don't have to compute it, just print it in order to compare with the actual statistical results:
double expected_Array[11] = {1/.36, 2/.36, 3/.36, 4/.36, 5/.36, 6/.36, 5/.36, 4/.36, 3/.36, 2/.36, 1/.36};
...
show_Data_Results_line((counter+2), total_Array[counter], expected_Array[counter], actual_Array[counter]);
The expected column should contain the probability that a roll of a dice ends up with the given sum. This is pure maths probability theory to be more specific, but you can also brute force its computation. Compute all the possible rolls of the dice and for each roll increase the number of rolls that result in the given sum. After that the expected value for each sum is equal to the number of ways in which you can get that sum divided by the total number of possible rolls(how many different rolls are possible with 2 dice?).
1 2 3 4 5 6
+---+---+---+---+---+---+
1 | 2 | 3 | 4 | 5 | 6 | 7 |
+---+---+---+---+---+---+
2 | 3 | 4 | 5 | 6 | 7 | 8 |
+---+---+---+---+---+---+
3 | 4 | 5 | 6 | 7 | 8 | 9 |
+---+---+---+---+---+---+
4 | 5 | 6 | 7 | 8 | 9 | 10|
+---+---+---+---+---+---+
5 | 6 | 7 | 8 | 9 | 10| 11|
+---+---+---+---+---+---+
6 | 7 | 8 | 9 | 10| 11| 12|
+---+---+---+---+---+---+
so to calculate the expected probability of 9
it is the number of combinations in the table above
that becomes 9 divided by total 36 i.e. 4/36
I know this answer is eight years late but here it is...
int nExpectedOutcome(int x, int y) {
return (x - abs(y-(x+1))) * 100000;
}
void calculation_of_expect() {
int numDice = 2;
int dFaces = 6;
int RT = 36000;
int expectedOutcome = (dFaces * numDice) - numDice;
for (int i = 0; i <= expectedOutcome; i++) {
expected_Array[i] = nExpectedOutcome(dFaces, i+2) / (float)RT;
}
}
...
show_Data_Results_line(
counter+2,
total_Array[counter],
expected_Array[counter],
actual_Array[counter]
);
Related
I'm new to C++ and currently learning iterators.
I wrote the following code, which adds the first and last digit in a vector.
In order to decrement the iterator, I've had to decrement the variable dec_pointer twice for the correct results. I'm obviously doing something wrong, but what?
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<int> vec{1,4,2,6,9,10,17,13,15};
size_t first_last =0;
size_t dec_pointer = vec.size()-1;
for(auto it =vec.cbegin(); it !=vec.cend() && !vec.empty(); ++it)
{
first_last = *it + *(it+(dec_pointer--));
std::cout<<"Add First and Last Digit : "<<first_last<<std::endl;
dec_pointer--;
}
return 0;
}
If you were adding the first and last elements (as per your text), you wouldn't need a loop, you could just do (after checking minimum size, of course):
first_plus_last = *(vec.cbegin()) + *(vec.cend()-1);
It looks however that you're trying to add the first and last, second and second last, and so on. The reason why you would have to decrement twice is because you're getting the second iterator value by adding something to the current iterator value (not the start iterator).
For example, let's for the purposes of understanding just pretend they're indexes rather than iterators:
index: 0 1 2 3 4 5 6 7 8
value: 1 4 2 6 9 10 17 13 15
To correctly add the first (moving) index and a delta value to get the second index, you would need:
index1 index2 index2 as (index1 + delta)
------ ------ --------------------------
0 8 0 + 8
1 7 1 + 6
2 6 2 + 4
... and so on
You can see that the required delta is decreasing by two each time: 8, 6, 4, ....
But, rather than doing iterator calculations, I would opt for just running an iterator from both ends, toward the middle. In other words, something like this:
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec{1, 4, 2, 6, 9, 10, 17, 13, 15};
if (vec.size() > 0) {
auto left = vec.cbegin();
auto right = vec.cend() - 1;
while (left < right) {
auto num1 = *left++;
auto num2 = *right--;
auto sum = num1 + num2;
std::cout << "Add (" << num1 << ", " << num2 << "): " << sum << '\n';
}
if (left == right) {
std::cout << "One number left in middle: " << *left << '\n';
}
}
}
That seems like cleaner (as in "easier to understand") code to me, and the output is:
Add (1, 15): 16
Add (4, 13): 17
Add (2, 17): 19
Add (6, 10): 16
One number left in middle: 9
It also works with all the possible vector sizes (empty, one element, even number of elements, and odd number of elements greater than one).
If you used vec.cbegin() instead of it when calculating the last pointer, you would not have this issue.
first_last = *it + *(vec.cbegin() + dec_pointer--)
On the 1st loop iteration, it refers to the first element, and dec_pointer is 8, so you have:
-----------------------------------------
| 1 | 4 | 2 | 6 | 9 | 10 | 17 | 13 | 15 |
-----------------------------------------
^ ^
it it+8
If you then increment it by 1 and decrement dec_pointer by 1 not 2, on the 2nd iteration dec_pointer is now 7 and you have this:
-----------------------------------------
| 1 | 4 | 2 | 6 | 9 | 10 | 17 | 13 | 15 |
-----------------------------------------
^ ^
it it+7
If you then increment it by 1 and decrement dec_pointer by 1 not 2, on the 3rd iteration dec_pointer is now 6 and you have this:
-----------------------------------------
| 1 | 4 | 2 | 6 | 9 | 10 | 17 | 13 | 15 |
-----------------------------------------
^ ^
it it+6
And so on. So, as you can see, the last value you refer to with it+dec_pointer is always the same element 15. That is why you have to decrement dec_pointer by 2 instead of 1 to get the correct result:
-----------------------------------------
| 1 | 4 | 2 | 6 | 9 | 10 | 17 | 13 | 15 |
-----------------------------------------
^ ^ ^ ^ ^ ^
it | | | | it+8
| | | |
it+1 | | (it+1)+6
| |
it+2 (it+2)+4
That being said, I would suggest an alternative and simpler solution - use a reverse_iterator from vec.crbegin() to access the 2nd value, instead of offsetting a forward iterator from vec.cbegin(), eg:
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<int> vec{1,4,2,6,9,10,17,13,15};
auto last_it = vec.crbegin();
for(auto it = vec.cbegin(); it != vec.cend(); ++it, ++last_it)
{
int first_last = *it + *last_it;
std::cout << "Add First and Last Digit : " << first_last << std::endl;
}
return 0;
}
Online Demo
Or even simpler, just get rid of the it loop iterator completely:
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<int> vec{1,4,2,6,9,10,17,13,15};
auto last_it = vec.crbegin();
for(int first : vec)
{
int first_last = first + *last_it++;
std::cout << "Add First and Last Digit : " << first_last << std::endl;
}
return 0;
}
Online Demo
I am trying to do the permutations of 8 characters, but I am only interested in output which contains maximum of 3 same characters. So any output which contains any character in more than 3 occurrences should be skipped.
Character set: a, b, c, d, e, f, g, G
Example:
Not interested in output e.g. aaaaaaab , aabcdeaa, acdGGGGg, GGGGbbbb ...
Interested in output e.g. abcdefgG, aaabcdef, abacadGf ...
I tried to write a code where I evaluate in each cycle number of occurrence of each character and skip (break/continue) to next loop if more than 3 same character occurrences are present.
Here is problem with my code which I can't solve. The program do only permutations starting with character 'a' and stops at aaabgGGG and I can't manage it to continue with iterations starting with b, c, d, e etc...
I want to achieve filtering during cycle to avoid unneeded cycles to occur => achieve as fast processing as possible.
When commenting the the ">3 occurrences filter" code between ##### lines, all permutations are processed correctly.
My code:
#include <iostream>
// C++ program to print all possible strings of length k
using namespace std;
int procbreak = 0;
// The main recursive method to print all possible strings of length k
void printAllKLengthRec(char set[], int setn[], string prefix, int n, int k)
{
// Base case: k is 0, print prefix
//cout << "03. In printAllKLengthRec function" << endl;
if (k == 0)
{
//print table with characters and their count
cout << (prefix) << endl;
cout << " | ";
for (size_t b = 0; b < 8; b++)
{
cout << set[b] << " | ";
}
cout << endl;
cout << " | ";
for (size_t c = 0; c < 8; c++)
{
cout << setn[c] << " | ";
}
cout << endl;
return;
}
// One by one add all characters from set and recursively call for k equals to k-1
for (int i = 0; i < n; i++)
{
cout << "04. In for loop where one by one all chars are added. K = " << k << "; I = " << i << "; N = " << n << endl;
string newPrefix;
//update characters count table
setn[i] += 1;
if (i > 0)
{
setn[i - 1] -= 1;
}
else
{
if (setn[7] > 0)
{
setn[7] -= 1;
}
}
//#############################################################################################
//check if there is any character in a table with count more than 3, then break current cycle
for (size_t d = 0; d < 8; d++)
{
if (setn[d] > 3)
{
procbreak = 1;
break; // enough to find one char with >3, then we don't need to continue and break operation
}
}
if (procbreak == 1)
{
procbreak = 0; // reset procbreak
continue; // skip to next cycle
}
//#############################################################################################
// Next character of input added
newPrefix = prefix + set[i];
// k is decreased, because we have added a new character
printAllKLengthRec(set, setn, newPrefix, n, k - 1);
}
}
void printAllKLength(char set[],int setn[], int k, int n)
{
cout << "02. In printAllKLength function" << endl;
printAllKLengthRec(set, setn, "", n, k);
}
// Main code
int main()
{
cout << "Start" << endl;
char set1[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'G' };
int setn[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
int k = 8; // string length
printAllKLength(set1, setn, k, 8); // 8 = n => number of characters in the set1
}
Where is main mistake in my code logic?
The solution to your problem is pretty simple.
What you want to do is to take your character set: a, b, c, d, e, f, g, G
and construct a "fake" sequence with each character triplicated.
std::string perm{"GGGaaabbbcccdddeeefffggg"};
The key insight here is that you can compute your permutations as usual, e.g., using std::next_permutation. You just need to take the first 8 elements from that permutation to have the result that you need.
[Edit: In order to avoid computing permutations for the rightmost 16 values, since these will always yield duplicates for the leftmost 8 values, after each step set the rightmost 16 values to the last permutation. The next call to std::next_permutation will permute the first 8 values.]
[Edit2: Working example
#include <algorithm>
#include <chrono>
#include <iostream>
int main()
{
// Initial state
std::string perm{"GGGaaabbbcccdddeeefffggg"};
using clock = std::chrono::steady_clock;
auto start = clock::now();
do
{
// Output permutation
std::cout << perm.substr(0, 8) << "\n";
// Now reverse the last 16 values, so that the call to the next_permutation would change the top 8
std::reverse(std::next(perm.begin(), 8), perm.end());
} while (std::next_permutation(perm.begin(), perm.end()));
std::clog << "Elapsed: " << std::chrono::duration_cast<std::chrono::milliseconds>(clock::now() - start).count() << "ms\n";
return 0;
}
]
I have found where the problem with filtering was...
The whole permutation is done by running cycles within cycles, in other words the function is calling itself.
When passing from right hand character (right most) to the left hand character (one step to the left), function is doing empty 'k' cycles (1 empty 'k' cycle when going from position 8 to 7 .... up to 7 empty 'k' cycles when going from position 2 to 1).
<-----------|
12345678
My initial code was evaluating the count of each character during each of these empty 'k' cycles.
And that was the issue.
During the empty 'k' cycles, the count of each character is changing and when the empty cycle finishes, the count of the character is real and exactly as it should be.
So the solution is, to do the evaluation of count of each character and if any of the chars is in count >3, break only the last cycle when k = 1.
I was breaking the loop in very first empty cycle, where the count of the characters in string were incorrect.
01. In for loop where one by one all chars are added. K = 1; I = 7; N = 8 <--- OK, loop when the last G was added to form string aaaaabGG
table in for loop
| a | b | c | d | e | f | g | G |
| 5 | 1 | 0 | 0 | 0 | 0 | 0 | 2 |
aaaaabGG <--- aaaaabGG was formed
table in base <--- aaaaabGG shown in the final output
| a | b | c | d | e | f | g | G |
| 5 | 1 | 0 | 0 | 0 | 0 | 0 | 2 |
02. In for loop where one by one all chars are added. K = 3; I = 2; N = 8 <--- going one character UP, next string after aaaaabGG should be aaaaacaa
table in for loop
| a | b | c | d | e | f | g | G |
| 5 | 0 | 1 | 0 | 0 | 0 | 0 | 2 | <--- but as we can see, during the K = 3 empty loop, the string is aaaaacGG (updates only 3rd char from left)
03. In for loop where one by one all chars are added. K = 2; I = 0; N = 8 <--- second empty loop K = 2
table in for loop
| a | b | c | d | e | f | g | G |
| 6 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | <--- as we can see, during the K = 2 empty loop, the string is updating and is now aaaaacaG (now updates only 2nd char from left, 3rd is OK from previous empty loop)
04. In for loop where one by one all chars are added. K = 1; I = 0; N = 8 <--- Last loop K = 1 (string is updated 1st character in the left only, 2nd and 3rd were updated in previous empty loops respectively)
table in for loop
| a | b | c | d | e | f | g | G |
| 7 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
aaaaacaa <--- we can see that now the string is as it should be aaaaacaa
table in base <--- aaaaacaa shown in the final output
| a | b | c | d | e | f | g | G |
| 7 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
My problem is that I don't understand nested loops well enough to answer this problem. I'm supposed to right-align a stack that I've made on a left alignment using nested for loops, but I can't quite figure out the conditions on the two inner ones.
Correct answer:
Height = 8
.......#
......##
.....###
....####
...#####
..######
.#######
########
My answer:
Height = 8
.......#
.......#......#
.......#......#.....#
.......#......#.....#....#
.......#......#.....#....#...#
.......#......#.....#....#...#..#
.......#......#.....#....#...#..#.#
.......#......#.....#....#...#..#.##
I've played around with it, took it seriously and nothing. I did (k = 7, k > j, k--), (k = 0, k < n-1, k++), k < j+7, I drew tables and i know that the height is pretty much the same as the value of the spaces but inverted on each line. I also know that the value of the hashes and the spaces should be equal to the height input by user.
It's supposed to take in a value from user, but I've worked on it on a separate file with the value n being the height to simplify and work on it without the rest of the program.
#include <stdio.h>
int main(void) {
int n = 8;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
for(int k = 7; k > j; k--) {
printf(".");
}
printf("#");
}
printf("\n");
}
}
It's actually pretty simple. Write a table with each line and how many spaces and '#' you need to print:
n == 8
| output | line | num_spaces | num_signs |
| -------- | ---- | ---------- | --------- |
| .......# | 1 | 7 | 1 |
| ......## | 2 | 6 | 2 |
| .....### | 3 | 5 | 3 |
| ....#### | 4 | 4 | 4 |
| ...##### | 5 | 3 | 5 |
| ..###### | 6 | 2 | 6 |
| .####### | 7 | 1 | 7 |
| ######## | 8 | 0 | 8 |
For line you can start from 0 or from 1 or from n and go backwards. Pick something that is the easiest. You will see that starting from 1 is the simplest in your example.
Now for each line we need to determine how many num_spaces and num_signs we print. They should depend on line and on n.
For num_spaces it's n - line and for num_signs it's line
So the code should look like this:
// for each line
for (int line = 1; line <= n; ++line)
{
// print n - line spaces
// print line # characters
// print \n
}
With loops the code will look like this:
// for each line
for (int line = 1; line <= n; ++line)
{
// print n - line spaces
for (int i = 0; i < n -line; ++i)
std::cout << ' ';
// print line # characters
for (int i = 0; i < line; ++i)
std::cout << '#';
std::cout << '\n';
}
std::cout.flush();
But that's actually not recommended. You can get rid of those inner loops. One good and easy way is to use strings:
// for each line
for (int line = 1; line <= n; ++line)
{
// print n - line spaces
std::cout << std::string(n - line, ' ');
// print line # characters
std::cout << std::string(line, '#');
std::cout << '\n';
}
std::cout.flush();
And you can go even one step further:
// for each line
for (int line = 1; line <= n; ++line)
{
// print n - line spaces and line # characters
std::cout << std::string(n - line, ' ') << std::string(line, '#') << '\n';
}
std::cout.flush();
I've been writing a program conducting some operations on two square matrixes. For the time being I've been thinking of a code which will read a matrix of a fixed (previously known size) and I'm writing these data into a 2-D array. However, I've got a problem, because when I'm debugging my code with addictional output messages everything seems fine, but the final output (the one in the for loop) I'm missing some numbers. It is really strange because when I'm prining all variables used in the process their values look fine.
#include <iostream>
#include <stdio.h>
using namespace std;
int main ()
{
int number = 0;
int index = 0;
int v_ind = 0; // vertical index
int h_ind = 0; // horizontal index
char c;
int size = 3; // temporary fixed size
int searched_number;
int matrix1 [size-1][size-1];
int matrix2 [size-1][size-1];
//scanf("%i %i", &size, &searched_number);
while (index < size)
{
c = getchar_unlocked();
if ( (c >= '0') && (c <= '9') )
{
number = (number * 10) + (c - '0');
continue;
}
if (c == ' ')
{
cout << "number on a space: " << number << endl;
matrix1[h_ind][v_ind] = number;
cout << "1 ) matrix1[" << h_ind << "][" << v_ind << "] : " << matrix1[h_ind][v_ind] << endl << endl;
v_ind ++ ;
number = 0;
continue;
}
if (c == '\n')
{
cout << "num on a newLine: " << number << endl;
matrix1[h_ind][v_ind] = number;
cout << "2) matrix1[" << h_ind << "][" << v_ind << "] : " << matrix1[h_ind][v_ind] << endl << endl;
h_ind ++ ;
v_ind = 0;
number = 0;
index ++ ;
continue;
}
}
for (int i = 0; i < size; i ++) {
for (int j = 0; j < size; j ++) {
int num = matrix1[i][j];
cout << "mat[" <<i <<"][" << j << "] : " << num << " " << endl;
}
}
}
Below I've pasted an exemplary output from Ideone.com of a matrix like this:
| 1 2 3 |
| 4 5 6 |
| 7 8 9 |
Sukces time: 0 memory: 3348 signal:0
number on space: 1
1 ) matrix1[0][0] : 1
number on space: 2
1 ) matrix1[0][1] : 2
num na newLine: 3
2) matrix1[0][2] : 3
number on space: 4
1 ) matrix1[1][0] : 4
number on space: 5
1 ) matrix1[1][1] : 5
num na newLine: 6
2) matrix1[1][2] : 6
number on space: 7
1 ) matrix1[2][0] : 7
number on space: 8
1 ) matrix1[2][1] : 8
num na newLine: 9
2) matrix1[2][2] : 9
mat[0][0] : 1
mat[0][1] : 2
mat[0][2] : 4
mat[1][0] : 4
mat[1][1] : 5
mat[1][2] : 7
mat[2][0] : 7
mat[2][1] : 8
mat[2][2] : 9
The problem looks simple - I'm missing all last numbers from every row, except from the last one. I suspect that somewhere I overwrite proper values but I've got no clue where.
you create the matrix as matrix1[size-1][size-1] which will have indices from 0 to size-2. Then you attempt to print the values from indices o to size-1. Try declaring the matrix as
int matrix1 [size][size]
Let's see the layout of the memory allocated for matrix1 and how you are using it.
You have
int matrix1[size-1][size-1];
Which is equivalent to:
int matrix1[2][2];
For rest of this discussion let me use m instead of matrix1 for illustration.
Memory allocated for m looks like:
m[0][0]
| m[0][1]
| | m[1][0]
| | | m[1][1]
| | | |
v v v v
+----+----+----+----+
| | | | |
+----+----+----+----+
Now let's see where m[0] and m[1] point
m[0]
| m[1]
| |
v v
+----+----+----+----+
| | | | |
+----+----+----+----+
After m[0][0] = 1; and m[0][1] = 2;, the values look like:
+----+----+----+----+
| 1 | 2 | | |
+----+----+----+----+
Things get strange when you set m[0][2] = 3;.
m[0][2] -- this is where the run time thinks m[0][2] points to.
|
v
+----+----+----+----+
| 1 | 2 | | |
+----+----+----+----+
and you get:
+----+----+----+----+
| 1 | 2 | 3 | |
+----+----+----+----+
Now, you execute m[1][0] = 4; If you recall where m[1][0] points to, you will see that the values now become (4 overwrites 3 in the location):
+----+----+----+----+
| 1 | 2 | 4 | |
+----+----+----+----+
After you execute m[1][1] = 5;, you get:
+----+----+----+----+
| 1 | 2 | 4 | 5 |
+----+----+----+----+
When you execute m[1][2] = 6;, you are reaching the memory past what was allocated for m.
m[1][2] -- this is where the run time thinks m[1][2] points to.
|
v
+----+----+----+----+----+
| 1 | 2 | 4 | 5 | |
+----+----+----+----+----+
Normally, you'd enter undefined behavior at this point. However, due to lucky (or unlucky depending your point of view) circumstances, your program does not crash but lets you use that memory. So, you get:
+----+----+----+----+----+
| 1 | 2 | 4 | 5 | 6 |
+----+----+----+----+----+
Now, you try to access memory by using m[2][0], m[2][2], and m[2][2]. Once again, the run time lets you use the memory after m[1][1] without crashing. By following pointer arithmetic, m[2] points to 2 addresses past m[1]
m[2]
|
v
+----+----+----+----+----+
| 1 | 2 | 4 | 5 | 6 |
+----+----+----+----+----+
m[2][0]
| m[2][0]
| | m[2][2]
| | |
v v v
+----+----+----+----+----+----+----+
| 1 | 2 | 4 | 5 | 6 | | |
+----+----+----+----+----+----+----+
After you execute, m[2][0] = 7;, m[2][1] = 8;, and m[2][2] = 9;, the values in memory look like:
+----+----+----+----+----+----+----+
| 1 | 2 | 4 | 5 | 7 | 8 | 9 |
+----+----+----+----+----+----+----+
Now you can see why you are getting the output. m[0][2] and m[1][0] point to the same address that holds the value 4. m[1][2] and m[2][0] point to the same address that holds the value 7.
My guess is that you are using the memory allocated for matrix2 when you are reaching beyond the memory allocated for matrix1 and the program does not crash. In other circumstances, the program might behave in unpredictable ways.
If you're doing anything interesting with your matrices, you should probably consider grabbing an existing library. Many of these will provide a heap of utilities for you, and they will still use either a 2D or 1D array for backing the data. the particular one you should choose will depend on what you're trying to use it for.
If you're determined to roll your own matrices I'd consider using a class with a 1D array. I've used something like this before
class Matrix {
int * values;
unsigned int nx;
unsigned int ny;
unsigned int x_stride;
unsigned int y_stride;
int& operator(int x, int y) {
return values[nx*x_stride+ny*y_stride];
}
... constructors etc...
};
Why use both x_stride and y_stride, when one will be 1 and the other nx? It allows you to do some nice tricks like copyless submatrix and copyless transpose on large matrices.
void transpose(Matrix &m) {
std::swap(m.nx, m.ny);
std::swap(m.x_stride, m.y_stride);
}
Matrix submatrix_slice(const Matrix &m, int start_x, int step_x, int start_y, int step_y) {
Matrix retval(m, Matrix::SharedData());
retval.start_x += start_x;
retval.x_stride *= step_x;
retval.start_y += start_y;
retval.y_stride *= step_y;
}
Why should you care about these? Well maybe you don't, but it can make the implementation of a lot of numerical algorithms neater without compromising speed. (E.g. I've used them to get neat versions of Gaussian elimination, Inverse, Determinant, least squares etc.)
One difference is that you need to use matrix(i,j) rather than matrix[i][j], but if you really care about that (and I've had to care about it before...) you can create a MatrixRow class that backs onto the same data, and is returned by a MatrixRow Matrix::operator[](int), which can also provide a int& MatrixRow::operator[](int), If you do this (and provide the const versions too) you'll be able to do matrix[i][j] as you might expect.
Another advantage of using a class based approach is that it becomes really easy to put debugging assertions into your accessor code, to ensure that you never access outside the matrix bounds.
This is some basic code for an array I'm writing. I need to fill all slots (14) of the array with 4, and then write a loop that will replace the slots 6 and 13 with 0. I am a beginner and have not learned vectors yet, just basic programming material.
const int MAX = 14;
int main ()
{
board ();
cout<<endl;
{
int i;
int beadArray[MAX] = {4};
for (i = 0; i < MAX; i++)
{
beadArray[i] = -1;
}
for (i = 0; i < MAX; i++)
{
cout<<i<<"\t";
}
}
cout<<endl;
system("pause");
return 0;
}
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
//this constant represents the size we want our array to be
//the size of an array must be determined at compile time.
//this means you must specify the size of the array in the code.
const unsigned short MAX(14);
//now we create an array
//we use our MAX value to represent how large we want the array to be
int beadArray[MAX] = {4};
//now our array looks like this:
//+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
//| 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
//+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
//but you want each index to be filled with 4.
//lets loop through each index and set it equal to 4.
for (int i = 0; i < MAX; ++i){
beadArray[i] = 4;
}
//now our array looks like this:
//+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
//| 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 |
//+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
//to set slots 6 and 13 equal to 0, it is as simple as this:
beadArray[6] = 0;
beadArray[13] = 0;
//careful though, is that what you wanted?
//it now looks like this:
//+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
//| 4 | 4 | 4 | 4 | 4 | 4 | 0 | 4 | 4 | 4 | 4 | 4 | 4 | 0 |
//+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
//this is because the [index number] starts at zero.
//index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
// +---+---+---+---+---+---+---+---+---+---+---+---+---+---+
// | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 |
// +---+---+---+---+---+---+---+---+---+---+---+---+---+---+
//print out your array to see it's contents:
for (int i = 0; i < MAX; i++){
cout << beadArray[i] << " ";
}
return EXIT_SUCCESS;
}
you could do something like this
int beadArray[14];
for(int i=0;i<14;i++){
beadArray[i]=4;
}
beadArray[6]=0;
beadArray[13]=0;
or
int beadArray[14];
for(int i=0;i<14;i++){
if(i==6 || i==13)
beadArray[i]=0;
else
beadArray[i]=4;
}