I'm trying to create a tic tac toe board but I'm having two problems. (Sorry if the format of this question is bad, this is my first time asking a question here)
#include <iostream>
#include <iomanip>
#include <cctype>
#include <fstream>
#include <cstdlib>
using namespace std;
One problem (the array declaration below) is that when the program runs (as shown at the very bottom), the last number (9) doesn't print it's just blank. If I replace the 9 with another number (for example, 5) it prints it out just like the other numbers. I can't figure out why it wont print for the number 9
char board[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int row;
int column;
void display_board();
int main()
{
display_board();
return 0;
}
void display_board()
{
for (row = 0; row < 3; row++)
{
for (column = 0; column < 3; column++)
{
cout << board[row][column];
}
cout << "\n";
}
}
The other problem is that when the program runs (as shown below), it prints three zeros before every number. I just want the 1-9 not the three zeros. I can't figure out why those three zeros are even there, let alone getting rid of them. Any and all help is appreciated thank you.
try
char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
have fun coding, and try read some books on the way
Thank you for the help everyone.
It's true, the problem could be fixed by either putting the numbers in the array declaration in single quotes or by changing the array itself to an int. Silly mistake on my part. Thanks again.
Change
char board[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
to
int board[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
(Make your char array an integer array).
There is nothing else that seems to be wrong in your program. I just quickly ran it in an IDE and I am getting the desired output after changing the array to int.
Related
Can anyone explain to me what is the meaning of this code :
byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3
How do they get the number of {9, 8, 7, 6} and {5, 4, 3, 2}. Here is the full code:
/*4x4 Matrix Keypad connected to Arduino
This code prints the key pressed on the keypad to the serial port*/
#include <Keypad.h>
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]= {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3
//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
void setup() {
Serial.begin(9600);
}
//If key is pressed, this key is stored in 'keypressed' variable
//If key is not equal to 'NO_KEY', then this key is printed out
//if count=17, then count is reset back to 0 (this means no key is pressed during the whole keypad scan process
void loop() {
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY) {
Serial.print(keypressed);
}
}
Everything in the code seems easy to understand. Along with the comment it is clear as a crystal. But as you said, you need an explanation, I will provide an answer :
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
In the above given piece of code, two byte vaiables would be declared named numRows and numCols and initialized each with the value 4.
byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3
So here is the code that you are stuck at. Two byte array would be declared named rowPins and colPins each of size 4 (since value of numRows and numCols are 4). Which would range from 0 to 3 (like array in c or java). In this numbers 9,8,7,6 would be assigned to array rowPins and 5,4,3,2 will be assigned to array colPins. Now how or where will these values be. They will be stored in a sequential way from index 0 to index 3. i.e
rowPins[0]=9
rowPins[1]=8
rowPins[2]=7
rowPins[3]=6
colPins[0]=5
colPins[1]=4
colPins[2]=3
colPins[3]=2
This is how they get those numbers.
They are not getting the numbers they are assigning it to the arduino pins 2,3,4,5,6,7,8,9
byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3
you can also use 4,5,6,7,8,9,10,11 pins too. Just check what pins you are assigning for the row and column inputs and then write the code according to it.
I have a vector that allows for duplicates, I want to randomly chose an element with the probability that represents how many times an element was repeated.
For example - for the vector below, 6 should have the highest probability of being chosen. I thought about using rand(), but I am not quiet sure how to incorporate the probability.
vector A = [ 0, 0, 2, 2, 4, 5, 1, 6, 6, 6]
thanks
I think you are on the right way for getting a custom distribution of values. See the following code which demonstrates the access to the vector. Hope it helps.
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <vector>
int main()
{
std::vector<int> A { 0, 0, 2, 2, 4, 5, 1, 6, 6, 6 };
std::srand(std::time(0)); // use current time as seed for random generator
int random_pos = std::rand() % A.size(); // Modulo to restrict the number of random values to be at most A.size()-1
int random_val = A[random_pos];
}
Maybe something like this (untested!):
#include <vector>
#include <random>
#include <iostream>
int main()
{
std::vector<size_t> A{0, 0, 2, 2, 4, 5, 1, 6, 6, 6};
static thread_local std::mt19937 g{std::random_device{}()};
static thread_local std::uniform_int_distribution<size_t> d{0,A.size()};
std::cout << A[d(g)] << std::endl;
}
vector<int> data = {3, 1, 5, 3, 3, 8, 7, 3, 2};
std::nth_element(data.begin(), data.begin() + median, data.end());
Will this always result in:
data = {less, less, 3, 3, 3, 3, larger, larger, larger} ?
Or would a other possible outcome be:
data = {3, less, less, 3, 3, 3, larger, larger, larger} ?
I've tried it multiple times on my machine wich resulted in the nth values always being contiguous. But that's not proof ;).
What it's for:
I want to building a unique Kdtree but I have duplicates in my vector. Currently I'm using nth_element to find the median value. The issue is to select a unique/reconstructible median, without having to traverse the vector again. If the median values were contiguous I could choose a unique median, without much traversing.
No. The documentation does not specify such behavior, and with a few minutes of experimentation, it was pretty easy to find a test case where the dupes weren't contiguous on ideone:
#include <iostream>
#include <algorithm>
int main() {
int a[] = {2, 1, 2, 3, 4};
std::nth_element(a, a+2, a+5);
std::cout << a[1];
return 0;
}
Output:
1
If the dupes were contiguous, that output would have been 2.
I have just tried several not-so-simple examples, and on the third got non-contiguous output.
Program
#include <vector>
#include <iostream>
#include <algorithm>
int main() {
std::vector<int> a = {1, 3, 3, 2, 1, 3, 5, 5, 5, 5};
std::nth_element(a.begin(), a.begin() + 5, a.end());
for(auto v: a) std::cout << v << " ";
std::cout << std::endl;
}
with gcc 4.8.1 under Linux, with std=c++11, gives me output
3 1 1 2 3 3 5 5 5 5
while the n-th element is 3.
So no, the elements are not always contiguous.
I also think that even a simpler way, with no thinking of a good test case, was just generating long random arrays with many duplicate elements and checking whether it holds. I think it will break on the first or second attempt.
I just want to hard code in a matrix using C++ (g++ 4.1.2), by default I went with a std::vector of std::vectors.
My guess is this can be done in one line I just don't know the correct syntax.
For example:
(1,2,5)
(9,3,6)
(7,8,4)
I thought it might be something like this -
vector<int> v1(1,2,3);
vector<int> v2(4,5,6);
vector<int> v3(7,8,9);
vector<vector<int>> vA(v1,v2,v3);
Normally, I wold read this info out of a text file, but I need to manually put in the numbers by hand and I have to use g++ 4.1.2
If you're not going to change the size or shape of this matrix and since you're hard-coding the values anyway, you may be better with a plain old array:
int matrix[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Otherwise, Fred Nurk's answer is what you are looking for.
The simplest way is the easiest (without C++0x):
vector<vector<int> > v (3);
for (int a = 0; a != 3; ++a) {
v[a].resize(3);
for (int b = 0; b != 3; ++b) {
v[a][b] = a * 3 + b + 1;
}
}
With 0x initializers, which I doubt that version of gcc supports:
vector<vector<int>> v = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
My question is as follows:
Refer to the following array declaration in the main():
const int size = 4;
int x[size][size] = {{1, 2, 3, 4}, {5, 6, 7, 8},
{9, 8, 7, 3}, {2, 1, 7, 1}};
Write a function SwapRows() to swap two rows of the above 2D array. For
example, if the function has been called to swap the first and the second rows of the
above 2D array then the result would be that the first row now becomes {5, 6, 7, 8}
and the second row now becomes {1, 2, 3, 4}. The function receives as parameter the
2D array, the size of the array, and two integers to indicate the rows to swap.
Help,,how can i go about this?????
Note: Using C++ Language
Pseudo code:
SwapRows(x[size][size], row0, row1, size)
for col = 0 to size - 1 do
temp = x[row0][col]
x[row0][col] = x[row1][col]
x[row1][col] = temp
Now all you need to do is convert the pseudo code into C++, then test, debug and document it.
#include <algorithm>
void SwapRows(int arr[][4], int r1, int r2)
{
std::swap(arr[r1],arr[r2]);
}