How to check if a number is already dispalyed - game - c++

I have vector< vector< set< int > > > for the board where each square is a set and initially has all the possible numbers that can go in {1, 2, 3, 4, 5, 6, 7, 8, 9}. Every time a square is set by a number being passed,;I need to go through all the elements of the box and delete that value from all the squares. I have the code here:
#include <vector>
#include <set>
#include <iostream>
#include <string>
using namespace std;
class Sudoku{
private:
int size;
vector< vector< set< int > > > board;
public:
Sudoku(int sizeIn):
size(sizeIn){}
void setSodoku() { board = vector<vector<set<int>>>(size, vector<set<int>>(size, { 1, 2, 3, 4, 5, 6, 7, 8, 9 })); }
void getSquare(int row, int col){
for(auto i : board[row][col]) std::cout << i << '\n';
}
bool setSquare(int row, int col, int value){
int tempRow = row / size;
int tempColumn = col / size;
for (int rowC=(tempRow*size);rowC<=((tempRow*size)+(size-1));rowC++)
{
for (int columnC=(tempColumn*size);columnC<=((tempColumn*size)+(size-1));columnC++)
{
if(board[rowC][columnC].count(value)==1)
{
board[rowC][columnC].erase(value);
}
if(board[rowC][columnC].empty())
{
cout << "wait 2 " << endl;
return false;
}
}
}
return true;
}
};
int main(){
// | 2 | 3 | | | 9 | 1 | 5 | | | | |
// | | | | | 2 | | | | 5 | 4 | |
// | 6 | | 7 | | | | | | | | |
Sudoku boardTest(9);
boardTest.setSodoku();
boardTest.setSquare(0,0,2);
boardTest.setSquare(0,1,3);
boardTest.setSquare(0,3,9);
boardTest.setSquare(0,4,1);
boardTest.setSquare(0,5,5);
boardTest.setSquare(1,3,2);
boardTest.setSquare(1,6,5);
boardTest.setSquare(1,7,4);
boardTest.setSquare(2,0,6);
boardTest.setSquare(2,2,7);
cout << "--------" << endl;
boardTest.getSquare(1,0);
return 0;
}
When I try to print what elements are in the row 0 and column 1 it displays 8 which should actually be 1,4,5,8,9 as 2,3,6,7 are already in box 0 (first one).
I know I need to have it 9x9 but this is only for test purpose.

I got it sorted. The code had 2 bugs.
1) it should be square root of the size and not size.
2) it should have check if size of the set is not equal to 1 in this if statement if(board[rowC][columnC].count(value)==1)

Related

C++ decrementing the iterator

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

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

Displaying the Boost matrix in form of line. Want to change it to Box

//This simple program initialize a 3x3 matrix and outputs it in the form of line.i want to display this matrix in the form of matrix.
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
int main(){
using namespace boost::numeric::ublas;
matrix<double> m1(3,3);
for(unsigned i=0;i<m1.size1();++i)
for (unsigned j=0;j<m1.size2();++j)
m(i,j)=3*i*j;
std::cout<<m1<<std::endl;
return 0;
}
output
[3,3]((0,0,0),(0,3,6),(0,6,12)
You can do something like this
void printMatrix(const matrix<double> &m)
{
for(unsigned i=0;i<m.size1();++i)
{
cout<<"| ";
for (unsigned j=0;j<m.size2();++j)
{
cout<<m(i,j)<<" | ";
}
cout<<"|"<<endl;
}
}
this will print something like this:
| 0 | 0 | 0 |
| 0 | 3 | 6 |
| 0 | 6 | 9 |

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

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

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