Hi, I've written a code which let's the user input the dimensions of a grid which they will then put in numbers in the grid (0,1 and 2). 0 represents and empty space, 1 an unlighted bomb and 2 a lighted bomb.
The final output would be what would happen to the grid when the lighted bombs (2) explodes (like an unlighted bomb on top of 2 would fall and replace the position of 2). I'm assuming the input would be correct and that no one would violate the rules.
I've made the code and it works, but I want the values printed out to be in a string for each row instead of an int for each grid.
for example a row would be 1 0 0 which I would print out number by number through the 'for' loop, but how do I convert that 3 values into 1 string?
I hope I'm making myself clear. Really new at programming in general so not clear on what info is needed, don't hesitate to ask.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include <iostream>
using namespace std;
int main(){
int sizes[2];
string values;
cout<<"Insert two numbers separated by spaces here. Do not violate this rule."<<endl;
for (int i=0; i<2; i++){
cin>>sizes[i];
}
int *x, *y;
x=&sizes[0];
y=&sizes[1];
int grid[*x][*y];
cout<<"Grid is "<<*y<<" number of rows and "<<*x<<" number of columns."<<endl;
for (int i=0; i<*y; i++){
cout<<"Type in "<<*x<<" numbers. Separated by spaces. use only 0, 1 or 2"<<'\n'<<endl;
for (int j=0; j<*x; j++){
cin>>grid[i][j];
cout<<"Your "<<*x<<" numbers are: "<<grid[i][j]<<'\n'<<endl;
}
}
cout<<"Your grid number is:"<<'\n'<<endl;
for (int i=0; i<*y; i++){
for (int j=0; j<*x; j++){
cout<<grid[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<"Now, assuming all those numbers in the grid are empty spaces and bombs."<<'\n'
<<"0 is an empty space, 1 is an unlighted bomb and 2 is a lighted bomb,"<<'\n'
<<"How would the grid look like after all the bombs have 'Kaboom'-ed?"<<'\n'<<endl;
for (int i=0; i<*y; i++){
for (int j=0; j<*x; j++){
if (grid[i][j]!=1){
grid[i][j]=0;}
}
}
for (int i=0; i<*x; i++){
int up = 0, down = *y-1;
while (up < down)
{
while (grid[up][i] == 0 && up < down)
up++;
while (grid[down][i] == 1 && up < down)
down--;
if (up < down)
{
grid[up][i] = 0;
grid[down][i] = 1;
up++;
down--;
}
}
}
cout<<"Your new grid number is:"<<'\n'<<endl;
for (int i=0; i<*y; i++){
for (int j=0; j<*x; j++){
cout<<grid[i][j]<<" "; //this is what I want to change
}
cout<<endl;
}
}
|
been looking around, I've tried using ostream or stringstream but maybe due to my inexperience, I couldn't convert each row into a string. Another thing is that the dimension is determined only on input so I can't use a fixed number of strings. Hope it's doable.
Also, if there are changes that can be made to improve the overall code, suggestions would be good.
Thanks a lot!