C++ filling arrays with constant values, looping and changing values - c++

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;
}

Related

Permutations with output limitation in C++

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 |

Modifying iterator inside for loop

I have a problem like this:
I got size of array and the array itself (all the elements) and i need to sort the array in the way that first item in array (lets call it key item) is placed so all items smaller then key item go left from key item and all items larger then key item go right form key item, but the items need to be i a same order as they was in declaration like so:
array input:
4 7 3 5 6 2 9 1 10 8
array output:
3 2 1 4 7 5 6 9 10 8
so i thought i can just go one time through array and if value is smaller then key item swap them and if value is larger then key item, place that value (item ) at the end (if there is n items in array place it on n+1 place) and then to shift all items from current index one place to the left like:
| 4 | 7 | 3 | 5 | 6 | 2 | 9 | 1 | 10 | 8 |
| 4 | | 3 | 5 | 6 | 2 | 9 | 1 | 10 | 8 | 7 |
| 4 | 3 | 5 | 6 | 2 | 9 | 1 | 10 | 8 | 7 |
but like that i would need to after every shifting make iterator stay same (so it can check that place again.
I have a code like this :
int u=1;
for (int i=1;i<n;i++){
if (x[i]<x[u]){
swap(x[i],x[u]);
u+=1;
}else {
x[i]=x[n];
for (int k=i;k<n-1;k++){x[k]=x[k+1];}
i--;
}
}
i think i made a mistake in the swapping part but i cant figure it out.
What you are looking for can be done using the STL's std::stable_partition() algorithm:
divides elements into two groups while preserving their relative order
Let it do the hard work for you, eg:
#include <iostream>
#include <algorithm>
struct doPartitionOn
{
int m_key;
doPartitionOn(int key) : m_key(key) {}
bool operator()(int a)
{
return (a < m_key);
}
};
int main()
{
int arr[] = {4, 7, 3, 5, 6, 2, 9, 1, 10, 8};
for(int i = 0; i < 10; ++i)
std::cout << arr[i] << ' ';
std::cout << std::endl;
std::stable_partition(arr, arr+10, doPartitionOn(arr[0]));
for(int i = 0; i < 10; ++i)
std::cout << arr[i] << ' ';
std::cout << std::endl;
std::cin.get();
return 0;
}
4 7 3 5 6 2 9 1 10 8
3 2 1 4 7 5 6 9 10 8
Live demo
Or, if using C++11 or later, you can use a lambda instead of a manual functor:
#include <iostream>
#include <algorithm>
int main()
{
int arr[] = {4, 7, 3, 5, 6, 2, 9, 1, 10, 8};
for(int value: arr)
std::cout << value << ' ';
std::cout << std::endl;
std::stable_partition(arr, arr+10, [&arr](int a){ return (a < arr[0]); });
for(int value: arr)
std::cout << value << ' ';
std::cout << std::endl;
std::cin.get();
return 0;
}
4 7 3 5 6 2 9 1 10 8
3 2 1 4 7 5 6 9 10 8
Live demo

Creating a new object seems change another object previously created (???)

I have a probem with a class I'm writing.
It consists in a Matrix represented as an std::array of std::array. So the first array is a sort of container of columns or rows (this can be decided by the user).
The class keeps trave of this by using a boolean.
When I create a single instance of that all seems ok, the problem appears when i create an another instance: the second instance is ok, while in the first the boolean value changes.
I can't figure out the reason of this behaviour.
The problem is present only if i put the initializeToZero() method inside the default constructor, or also with the copy constructor. Moreover the 2 instance must have the same numberOfColumn, otherwise the problem does not appear.
I link you the most relevant part of the code:
#include <stdio.h>
#include <array>
using namespace std;
template <typename scalar,int numberOfRows,int numberOfColumns> class FullArrayMatrix {
bool onlyColumnBased; //if true the first array contains the columns
array<array<scalar, numberOfRows>, numberOfColumns> verticalMatrixArray;
array<array<scalar, numberOfColumns>, numberOfRows> horizontalMatrixArray;
// if onlyColumnBased is true tre programm considers only verticalMatrixArry, if false only horizontalMatrixArray
public:
///CONSTRUCTOR
FullArrayMatrix (bool forceColumnBased=true):onlyColumnBased(forceColumnBased){
initializeToZero();
} //default initializer
bool getOnlyColumnBased() const;
void print();
///OPERATOR
scalar & operator ()(int riga, int colonna);
};
//initializeToZero
template <typename scalar,int numberOfRows,int numberOfColumns>
void FullArrayMatrix<scalar,numberOfRows,numberOfColumns>::initializeToZero() {
for (int i=1; i<=numberOfRows; i++) {
for (int j=1; j<=numberOfColumns; j++) {
if ((*this)(i,j)) {
(*this)(i,j)=0;
}
}
}
}
//print
template<typename scalar,int numberOfRows,int numberOfColumns> void FullArrayMatrix<scalar, numberOfRows, numberOfColumns >::print(){
cout<<endl<<"--"<<endl;
if (getOnlyColumnBased()) {
for (int i=1; i< numberOfRows+1; i++) {
cout<<"|\t";
for (int j=1; j< numberOfColumns+1; j++) {
cout<<(*this)(i,j)<<"\t";
}
cout<<"|"<<endl;
}
}
else {
for (int i=1; i< numberOfColumns+1; i++) {
cout<<"|\t";
for (int j=1; j< numberOfRows+1; j++) {
cout<<(*this)(i,j)<<"\t";
}
cout<<"|"<<endl;
}
}
cout<<"--"<<endl;
}
template <typename scalar,int numberOfRows, int numberOfColumns> scalar & FullArrayMatrix<scalar,numeroRighe,numeroColonne>::operator ()(int row, int column){
if (getOnlyColumnBased()) {
return verticalMatrixArray[column][row];
} else {
return horizontalMatrixArray[row][column];
}
}
This is the main:
FullArrayMatrix<double, 10, 1> full1(true);
full1.print();
cout<<full1.getOnlyColumnBased()<<endl;
FullArrayMatrix<double, 10, 1> full2(true);
cout<<endl<<endl<<full1.getOnlyColumnBased()<<full2.getOnlyColumnBased()<<endl;
full1.print();
full1.initializeToZero();
full1.print();
And this is the output:
--
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
--
1
01
--
| 0 0 0 0 0 0 0 0 0 3.49057e-320 |
--
--
| 0 0 0 0 0 0 0 0 0 0 |
--
Thank you very much for any help!
It seems you have an off-by-one error ("there are two hard problems in computer science: naming things, cache coherency, and off-by-one errors"): C++ uses zero based arrays. You are accessing indices 1...numberOf... in your initializeToZero() method. As a result you'll have a buffer overrun which, apparently, results in some of your variables being overwritten.
After the first comment I discovered a stupid error, now the only problem is in the copy constructor. As described before it seems to change onlyColumnBased in the input matrix.
How can it change this value? inputMatrix is a const reference, isn't it?
The code is the following:
In the declaration:
FullArrayMatrix (const FullArrayMatrix<scalar,numeroRighe,numeroColonne> &inputMatrix,bool forceColumnBased=true);
In the implementation:
template <typename scalar,int numberOfRows, int numberOfColumns> FullArrayMatrix<scalar, numberOfRows, numberOfColumns >::FullArrayMatrix (const FullArrayMatrix<scalar, numberOfRows, numberOfColumns > &matriceInput,bool forceColumnBased):onlyColumnBased(forceColumnBased){
cout<<endl<<endl<<matriceInput.getOnlyColumnBased()<<onlyColumnBased;
if (!onlyColumnBased) {
for (int i=1; i< numberOfRows +1; i++) {
for (int j=1; j< numberOfColumns +1; j++) {
(*this)(i,j)=inputMatrix.horizontalMatrixArray[j-1][i-1];
}
}
} else {
for (int i=1; i< numberOfColumns +1; i++) {
cout<<endl<<"column "<<i<<" "<<inputMatrix.getOnlyColumnBased();
for (int j=1; j< numberOfRows +1; j++) {
cout<<endl<<"row "<<j<<" "<<inputMatrix.getOnlyColumnBased();
(*this)(i,j)=inputMatrix.verticalMatrixArray[i-1][j-1];
}
}
}
cout<<inputMatrix.getOnlyColumnBased();
}
I have put some cout to notice when onlyColumnBased changes and it seems to change in the 1st column, 3rd row.
This is the main:
FullArrayMatrix<double, 10, 1> full1(true);
full1.print();
FullArrayMatrix<double, 10, 1> full2(full1,true);
full1.print();
This is the output:
--
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
| 0 |
--
11
column 1 1
row 1 1
row 2 1
row 3 1
row 4 0
row 5 0
row 6 0
row 7 0
row 8 0
row 9 0
row 10 0
0
--
| 0 0 0 0 0 0 0 0 0 0 |
--
I really dont't have idea of the problem!

Creating matrix using 2-D array

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.

A Program for Simulating the Rolling of 2 Dice

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]
);