Reputation: 75
I figured the following code would work, Here's an abridged version of my code:
#include <iostream>
#include <vector>
int main()
{
int xCoordMovement, yCoordMovement;
int rows, cols;
char point = '*';
std::cout << "enter number of rows";
std::cin >> rows;
cols = rows;
std::vector<std::vector<char>> grid(rows, std::vector<char>(cols, '0'));
std::vector<char> flattenedGrid;
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
std::cout << grid[x][y];
std::cout << std::endl;
}
std::cout << "input for x coord: ";
std::cin >> xCoordMovement;
std::cout << "input for y coord: ";
std::cin >> yCoordMovement;
for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
flattenedGrid.push_back(grid[x][y]);
flattenedGrid[((cols * yCoordMovement) - (rows - xCoordMovement)) - 1] = point;
std::cout << flattenedGrid.size() << std::endl;
for(int i = 0; i < flattenedGrid.size(); i++)
std::cout << flattenedGrid[i];
std::cout << std::endl;
for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
std::cout << grid[x][y];
std::cout << std::endl;
}
std::cin.ignore();
std::cin.get();
return 0;
}
However it does not seem to change the values of the grid, it should now have a value that is a star at one of it's coordinates, but alas, all grid contains is it's original values.
relevant output:
00000
00000
00000
00000
00000
desired output:
00000
000*0
00000
00000
00000
Here is the bit I was hoping would assign the values of my vector into my vector of vectors:
for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];
Upvotes: 1
Views: 708
Reputation: 26800
There are a couple of changes you have to make to the code:
point
to an element of flattenedGrid
Change:
flattenedGrid[((cols * yCoordMovement) - (rows - xCoordMovement)) - 1] = point;
to:
flattenedGrid[(yCoordMovement) + (rows * xCoordMovement)] = point;
grid
with elements of flattenedGrid
Change:
for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];
to:
for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
grid[x][y] = flattenedGrid[rows * x + y];
Upvotes: 1