change x,y of board by user input - c++

i have a board of 10x10 and here his code:
for(int x = 0; x < 10; x++) // X
{
cout << 0;
for(int y = 0; y < 10; y++) // Y
{
cout << " " << 0;
}
cout << endl;
}
now I want to change 0 to 1 in the x,y location by user input.
how can I do that?
here is simply what I want to do ( in pictures ):
the user input is x = 2, y = 2 and the table changing from Table 1 Example to Table 2 Example ( as a new table ):
TABLE 1 Example | TABLE 2 Example
its just a curiosity question that I've been trying to make.

Get x0 and y0 before the loop using cin>> x0 and cin>> y0.
Inside the loop
if(x == x0 && y==y0)
cout << " " << 1;
else
cout << " " << 0;

The best way would be to keep all values in a two dimensional array. Then, play around with the
values of that array. After than, write the whole array once to the screen.
See the example below :
int matrix[10][10];
for(int i = 0; i < 10; i++)
for(int j = 0; j < 10; j++) matrix[i][j]=0;
matrix[2][3]=1;
matrix[3][4]=1;
matrix[4][5]=1;
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
cout<<matrix[i][j];
}
cout << endl;
}

you can print again the whole matrix and check the x,y pair before writing the 0 or the 1
for(int x = 0; x < 10; x++) // X
{
cout << 0;
for(int y = 0; y < 10; y++) // Y
{
//here verify the x,y against the user input
if(x == xUser && y == yUser)
{
cout << " " << 1;
} else{
cout << " " << 0;
}
}
cout << endl;
}

Related

How do I set all the diagonals in a matrix equal to zero?

I have a problem with my homework that asks me to have the compiler print out a matrix in which all the diagonals are outputted as zero. I also have to pass it to a function. However, I have no idea how to do this..
Here is my code:
#include <iostream>
using namespace std;
int diagonals();
int main()
{
//problem 1
int matrix[3][3];
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3 ; j++)
{
cout << "Row " << i << " column " << j<< ": ";
cin >> matrix[i][j];
}
}
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
cout << "\nReverse of the matrix:" << endl;
for (int j = 1; j <= 3; j++)
{
for (int i = 1; i <= 3; i++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}//end of problem 1
//problem 2
cout << "Diagonals changed to 0:\n" << endl;
}
your matrix declaration says int matrix[3][3]; that it has three 1-D array & in each 1-D array you can store three elements. And in C/C++ array index starts from zero.
Problematic statement is for (int i = 1; i <= 3; i++) as you are skipping matrix[0][0] and trying to store into matrix[3][3] which doesn't exist which in turn causes undefined behavior.
So firstly start iterating loop from 0 to number of rows & column respectively.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3 ; j++) {
cout << "Row " << i << " column " << j<< ": ";
cin >> matrix[i][j];
}
}
Coming to task you mentioned, print out a matrix in which all the diagonals are outputted as zero. ? write one condition so that if row value & col value are equal then assign it to zero otherwise scan from user. Here is the sample code
int main(void) {
int matrix[3][3] = { 0 }; /* initialize it */
int row = sizeof(matrix)/sizeof(matrix[0]); /* find no of rows */
int col = sizeof(matrix[0])/sizeof(matrix[0][0]);/* find no of columns */
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if( i == j)
matrix[i][j] = 0;/* when i and j are equal means thats diagonal and assign it to zero */
else /* if its not diagonal then scan from user */
std::cin>>matrix[i][j];
}
}
return 0;
}
Secondly, I also have to pass it to a function. for this learn how to pass 2d array to a function. Here is the sample example.
void diagonal(int (*mat)[3],int row, int col) { /* mat is pointer to an array */
std::cout<<"printing matrix "<<std::endl;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
std::cout<<mat[i][j]<<"\t";
}
std::cout<<std::endl;
}
}
And call diagonal() like below from main() function as
diagonal(matrix,row,col); /* pass matrix, no of rows, no of columns */

How to print diamond shape w/ c++

I need help, I created a short little program a while ago where it would print a simple pyramid with "*" like this:
*
***
*****
but I decided to challenge myself and see if I could create a simple diamond shape like this:
*
***
*****
***
*
Here is my code so far.
I should also add that the value you input, for example 5, determines how big the diamond is.
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;
for (int i = 0; i < value; i++) {
//print spaces v v v
for (int x = 0; x < (value - i - 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2 * i + 1); y++) {
cout << "*";
}
cout << endl;
}
for (int i = 0; i < value; i++) {
int number = 0;
number+= 2;
//print spaces v v v
for (int x = 0; x < (value - value + i + 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (/*ATTENTION: What do I do here? Plz help*/); y++) {
cout << "*";
}
cout << endl;
}
return 0;
}
What I've been trying to do is figure out what to put inside the parenthesis where it says (//ATTENTION). I've been working for at least an hour trying to do random things, and one time it worked when I input 4, but not for 5, and it's just been very hard. This is key to building the diamond, try putting in just value and compile to see what happens. I need it to be symmetrical.
I need to know what to put inside the parenthesis please. I'm sorry this is very long but the help would be appreciated thanks.
I also apologize if my code is messy and hard to read.
int number = 0; and number+= 2;
value - value inside for (int x = 0; x < (value - value + i + 1); x++) {
are not required.
Inside the parenthesis, you can use
2*(value-i-1)-1
However, I would suggest you to first analyze the problem and then try to solve it instead of trying random things. For instance, let's consider the cases of even and odd inputs i.e., 2 and 3.
Even Case (2)
*
***
***
*
The Analysis
Row Index Number of Spaces Number of Stars
0 1 1
1 0 3
2 0 3
3 1 1
For row index < value
Number of Spaces = value - row index - 1
Number of Stars = 2 * row index + 1
For row index >=value
The number of spaces and stars are simply reversed. In the odd cases, the situation is similar too with a small exception.
Odd Case (3)
*
***
*****
***
*
The Analysis
Row Index Number of Spaces Number of Stars
0 2 1
1 1 3
2 0 5
3 1 3
4 2 1
The small exception is that while reversing, we have to ignore the row index = value.
Now, if we put the above analysis in code we get the solution
//Define the Print Function
void PrintDiamond(int rowIndex, int value)
{
//print spaces v v v
for (int x = 0; x < value - rowIndex -1; x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < 2 * rowIndex + 1; y++) {
cout << "*";
}
cout << endl;
}
And then inside main
//Row index < value
for (int i = 0; i < value; i++) {
PrintDiamond(i,value);
}
//For row index >= value reversing the above case
//value-(value%2)-1 subtracts 1 for even and 2 for odd cases
//ignore the row index = value in odd cases
for (int i = value-(value%2)-1; i >=0; i--) {
PrintDiamond(i,value);
}
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;
for (int i = 0; i < value; i++) {
//print spaces v v v
for (int x = 0; x < (value - i - 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2 * i + 1); y++) {
cout << "*";
}
cout << endl;
}
for (int i = 0; i < value-1; i++) {
// int number = 0;
// number+= 2;
// //print spaces v v v
for (int x = 0; x < i+1; x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2*(value-1-i)-1); y++) {
cout << "*";
}
cout << endl;
}
return 0;
}
I hope that you will get this .Also in the second for loop you were iterating it one extra time by iterating the loop upto value. But since the pyramid is symmetric so the no of rows in the pyramid will be 2*value-1.So I in the second loop i should vary upto value -1.
This code should resolve the problem:
#include <sstream>
using namespace std;
void printSpaces(int howMany) {
for(int i = 0; i < howMany; i++) cout << " ";
}
void figure(int size) {
bool oddSize = size % 2 == 1;
int center = size / 2;
int spaces = size / 2;
// If figure is of an odd size adjust center
if (oddSize) {
center++;
} else { // Else if figure is of even size adjust spaces
spaces--;
}
for (int i = 1; i <= center; i++) {
printSpaces(spaces);
for(int j = 0; j < 1 + (i - 1) * 2; j++) cout << "*";
cout << endl;
spaces--;
}
spaces = oddSize ? 1 : 0; // If the figure's size is odd number adjust spaces to 1
center -= oddSize ? 1 : 0; // Adjust center if it's an odd size figure
for(int i = center; i >= 1; i--) {
printSpaces(spaces);
for(int j = 0; j < 1 + (i - 1) * 2; j++)
cout << "*";
cout << endl;
spaces++;
}
}
int main() {
int value = 0;
while(value < 3) {
cout << "Please enter in a value (>= 3): ";
cin >> value;
cout << endl;
}
figure(value);
return 0;
}

why doesn't this replace the 'Xs' with space's

int mapSizeX = 30;
int mapSizeY = 10;
string map[10][30];
char playerMovement;
for (int i = 0; i < mapSizeY; i++)
{
for (int j = 0; j < mapSizeX; j++)
{
map[i][j]= "X";
cout << map[i][j];
}
cout << endl;
}
for (int i = 1; i < mapSizeY - 1; i++)
{
for (int j = 1; j < mapSizeX - 1; j++)
{
map[i][j] = " ";
cout << map[i][j];
}
cout << endl;
}
this for some reason doesn't replace the X's with spaces and adds it after the X's and doesn't make a "arena", this is for a snake game by the way
You're outputting to cout twice. Remove both cout << map[i][j]; from the loops and output only when you are done editing the string:
... // Previous code without printing
for (int i = 0; i < mapSizeY; i++)
{
for (int j = 0; j < mapSizeX; j++)
{
cout << map[i][j];
}
cout << endl;
}
Try adding the following to see your arena:
cout << endl << endl;
cout << "The arena: " << endl << endl;
for (int i = 0; i < mapSizeY; i++)
{
for (int j = 0; j < mapSizeX; j++)
{
cout << map[i][j];
}
cout << endl;
}
After you printed the entire arena with X's, you then print the entire arena with white spaces just one tile shorter on each side. So you're printing them after eachother. What you wanna do instead is to print them in the same nested for loop, like this:
for (int i = 0; i < mapSizeY; i++)
{
for (int j = 0; j < mapSizeX; j++)
{
if (i == 0 || j == 0 || i == mapSizeY - 1 || j == mapSizeX - 1) {
map[i][j] = "X";
cout << map[i][j];
}
else {
map[i][j] = " ";
cout << map[i][j];
}
}
cout << endl;
}
The if-clause checks if the current iteration is an edge (if its 0 on X or Y axis or if its the last element in the array on X or Y axis) and prints the X, if its not (its in the field) it prints a blank space.
Little tip I wanna give you: I would initialize the field straight away with the characters without printing it.
So initialize the field like this:
for (int i = 0; i < mapSizeY; i++)
{
for (int j = 0; j < mapSizeX; j++)
{
if (i == 0 || j == 0 || i == mapSizeY - 1 || j == mapSizeX - 1) {
map[i][j] = "X";
}
else {
map[i][j] = " ";
}
}
}
and then after you've done the equations on the snake, food and stuff and written that all into you ´map´ vairable, go and print the whole map like this:
for (int i = 0; i < mapSizeY; i++)
{
for (int j = 0; j < mapSizeX; j++)
{
cout << map[i][j];
}
cout << endl;
}
Hope I could help!

Convert string to char 2d array

I was wondering does anyone know how to convert a string into a 2d array? This was my attempt:
string w;
char s[9][9];
int p=0;
getline(cin, w);
while(p != w.size())
{
for (int k = 0; k < 9; k++)
{
for(int j = 0; j < 9; j++)
{
s[k][j] = w[p];
p++;
}
}
}
cout << "nums are: " << endl;
for(int k = 0; k < 9; k++)
{
for(int j = 0; j <9; j++)
{
cout << s[k][j];
}
}
But the numbers don't print out correctly. I want s[k][j] to print out everything in w but it simply prints out gibberish. I also noticed if i do string[81] then I get a whole bunch of errors. Could anyone help me? Thanks.
Try this:
const int NUM_ROWS = 9;
const int NUM_COLS = 9;
string w;
char s[NUM_ROWS][NUM_COLS];
getline(cin, w);
if (w.size() != (NUM_ROWS * NUM_COLS))
{
cerr << "Error! Size is " << w.size() << " rather than " << (NUM_ROWS * NUM_COLS) << endl;
exit(1);
}
for (int count = 0; count < w.size(); count++)
{
if (!isdigit(w[count]) && w[count] != '.')
{
cerr << "The character at " << count << " is not a number!" << endl;
}
}
for (int row = 0; row < NUM_ROWS; row++)
{
for(int col = 0; col < NUM_COLS; col++)
{
s[row][col] = w[col + (row * NUM_COLS)];
}
}
cout << "Nums are: " << endl;
for(int row = 0; row < NUM_ROWS; row++)
{
for(int col = 0; col < NUM_COLS; col++)
{
cout << s[row][col] << " ";
}
cout << endl;
}
Based on our chat, you might want this:
const int NUM_ROWS = 9;
const int NUM_COLS = 9;
string w;
char s[NUM_ROWS][NUM_COLS];
while (!cin.eof())
{
bool bad_input = false;
getline(cin, w);
if (w.size() != (NUM_ROWS * NUM_COLS))
{
cerr << "Error! Size is " << w.size() << " rather than " << (NUM_ROWS * NUM_COLS) << endl;
continue;
}
for (int count = 0; count < w.size(); count++)
{
if (!isdigit(w[count]) && w[count] != '.')
{
cerr << "The character at " << count << " is not a number!" << endl;
bad_input = true;
break;
}
}
if (bad_input)
continue;
for (int row = 0; row < NUM_ROWS; row++)
{
for(int col = 0; col < NUM_COLS; col++)
{
s[row][col] = w[col + (row * NUM_COLS)];
}
}
cout << "Nums are: " << endl;
for(int row = 0; row < NUM_ROWS; row++)
{
for(int col = 0; col < NUM_COLS; col++)
{
cout << s[row][col] << " ";
}
cout << endl;
}
}
You haven't described what you're trying to do very well and you haven't described the problem you're encountering, so the following is just based on guesses.
So it looks what you're trying to do is take a string like:
The quick brown fox jumped over the lazy dogs.
And put that into a 2D array like:
0 1 2 3 4 5 6 7 8
0 T h e q u i c k
1 b r o w n f o
2 x j u m p e d
3 o v e r t h e
4 l a z y d o g s
5 . x x x x x x x x
6 x x x x x x x x x
7 x x x x x x x x x
8 x x x x x x x x x
One thing wrong with your code is that as you copy values from w into s you don't ensure that the index p is actually within the bounds. You seem to have attempted to deal with this in the line that says while(p != w.size()); but that's an outer loop that does not protect p from being incremented out of bounds and used in the inner loops. Instead you'd have to put something like p++; if (p==w.size()) break; inside the inner most loop where you increment p. Or better yet, you should iterate over the string instead of over the array. Something like the following pseudo-code would replace your entire while(p){for(k){for(j){}}} set of loops.:
for(size_t i=0; i<w.size(); ++i) {
int k = compute target row from i
int j = compute target column from i
s[k][j] = w[i]
}
Also, here's some code to better visualize the array as you're debugging.
#include <iostream>
int main() {
char s[9][9] = {"The","quick","brown","fox","jumped","over","the","lazy","dogs."};
// your code to get input and copy it into the array goes here
//
// for(size_t i=0; i<w.size(); ++i) {
// int k = compute target row from i
// int j = compute target column from i
// s[k][j] = w[i]
// }
std::cout << " 0 1 2 3 4 5 6 7 8\n";
for (int i=0; i<9; ++i) {
std::cout << i;
for (int j=0; j<9; ++j)
std::cout << ' ' << s[i][j];
std::cout << '\n';
}
}
If you run this program without any changes the output should look like this:
0 1 2 3 4 5 6 7 8
0 T h e
1 q u i c k
2 b r o w n
3 f o x
4 j u m p e d
5 o v e r
6 t h e
7 l a z y
8 d o g s .

allocating a multi-dimensional array VSC++

I am working with a 2D array in a VS2010 console application. I am allocating the array as such:
Thing::Thing(int _n, bool _control){
m = _n;
control = _control;
thisArray = new int*[m];
for(int ii = 0; ii < m; ii++){
thisArray[ii] = new int[m];
for (int jj = 0; jj < m; jj++){
thisArray[ii][jj] = 0;
}
}
if (control == true){
int num = 1;
for (int jj = 0; jj < m; jj++){
for ( int ii = 0; ii<m; ii++){
if ((jj == (m-1)) && (ii == (m-1))){
std::cout << "inserting " << 0 <<
"at[" << ii << "][" << jj << "]" << std::endl;
thisArray[ii][jj] = 0;
std::cout << thisArray[ii][jj] << std::endl;
} else{
std::cout << "inserting " << num <<
"at[" << ii << "][" << jj << "]" << std::endl;
thisArray[ii][jj] = num++;
std::cout << thisArray[ii][jj] << std::endl;
}
}
}
pi.x = m-1;
pi.y = m-1;
}
}
then I attempt to display it through
void Thing::display(){
int x = 0;
int y = 0;
for( ; y < m; y++){
for( x = 0; x < m; x++){
if (Point(x, y) == pi){
std::cout << "[ ]";
}
std::cout << "[" << thisArray[x][y] << "]";
if ( x == m ){
std::cout << std::endl;
}
}
}
}
but it seems like it is only displaying the first dimension as output when I pass in 4, and true looks like this:
inserting 1 at[0][0]1
inserting 2 at[1][0]2
inserting 3 at[2][0]3
inserting 4 at[3][0]4
inserting 5 at[0][1]5
inserting 6 at[1][1]6
inserting 7 at[2][1]7
inserting 8 at[3][1]8
inserting 9 at[0][2]9
inserting 10 at[1][2]10
inserting 11 at[2][2]11
inserting 12 at[3][2]12
inserting 13 at[0][3]13
inserting 14 at[1][3]14
inserting 15 at[2][3]15
inserting 0 at[3][3] 0
[1][2][3][4]
, and when I add the array to my watch list the system shows the pointer to pointer to a single value, and then will only show me the first element, and not all of the elements.
it throws no run time access errors, or anything like that, but when I try to display the array it only does the first row. Note: I have been asked to use an actual 2D-array of ints, and am not allowed to use a pre-written library, or a single dimensional lie.
edit: added additional output info.
edit2 (resolved): in display changed the second for loop to be
for (x = 0; x < m; x++)
when holding values external to a for loop insure that they are reset for circular iteration.
In your display function, you have to reset x at each iteration of the outer y loop.
It would even be clearer, if you did the initialization within the for statement:
for(int y = 0; ; y < m; y++){
for(int x = 0; ; x < m; x++){
And to view a pointer as an array in Visual C++ debugger, you can look at that other question.