The objective is to use the X/Y arrays to pinpoint the coordinates on the map with `X`. The grid map display perfectly when I omit the `for loop` in the `else` block. However once I include it , the map will not display properly because there is no empty spaces. I have attempted to add another `if` in the `else` block but this did not produce the expected result (see commented portion in the code).
I have also tried to replace the `for loop` and putting the conditions in the `else if` but the `X` will only print once (the last x,y coordinate from the array).
Does anyone has any suggestion on how to get the expected results?
#include <iostream>
#include <vector>
#include <string>
usingnamespace std;
int main()
{
constint N = 7;
vector<string> grid( N + 3, string( 3 * N + 7, ' ' ) );
auto ival = [N]( int y ){ return N - y; };
auto jval = [N]( int x ){ return 3 * ( x + 2 ); };
int XPT[] = { 1, 1, 1, 2};
int YPT[] = { 1, 2, 3, 1};
int NPTS = sizeof XPT / sizeof XPT[0];
// Border (effectively, top corresponds to y=N, bottom to y=-1 etc)
for ( int x = -1; x <= N; x++ ) grid[ival(-1)][jval( x)] = grid[ival(N)][jval(x)] = '#';
for ( int y = 0; y < N; y++ ) grid[ival( y)][jval(-1)] = grid[ival(y)][jval(N)] = '#';
// Numbers (effectively placed at y=-2 or x=-2)
for ( int x = 0; x < N; x++ ) grid[ival(-2)][jval( x)] = '0' + x;
for ( int y = 0; y < N; y++ ) grid[ival( y)][jval(-2)] = '0' + y;
// Initial points
for ( int n = 0; n < NPTS; n++ ) grid[ival(YPT[n])][jval(XPT[n])] = 'X';
// Output
for ( string s : grid ) cout << s << '\n';
}
Is there no other ways to solve my issue other than to rewrite the code?
you can debug and fix it, most likely. I mean, it looks to me like your only issue is not padding spaces internally?
Fixing it may take some doing, but that is the alternative.
Everyone has a throw out and redo moment sometimes. You get a block the size of a page, maybe 2 pages, that does not work and is difficult to debug and fix, and you throw it out, using what you learned from your mistakes and what went well, and build it again doing better the second time. Happens to everyone, and some coders expect to rewrite complex things a time or 2 before calling it done.
Indeed, I am more keen to work on the set of my code and trying to rectify it.
You are also right, the issue lies with padding the spaces internally after 'X' is place inside the map. I do not know how to manipulate the map to tell it to look at (if its empty, put space, if condition is met (x and y), put X).
I have added comments to my if else if it helps someone to understand what the code is. The idea is to understand how to effectively use for loop and if else to do this.
I am more keen to work on the set of my code and trying to rectify it.
Here is a start to you fixing your abominable code. All you need is another couple of convoluted if's and judicious use of left and right - if you must
#include <stdio.h>
#include <iomanip>
#include <string>
#include <iostream>
usingnamespace std;
int main()
{
int grid_x_length = 6;
int grid_y_length = 6;
int y_counter = 6;
int x_counter = 0;
int xArray[4];
xArray[0] = 1;
xArray[1] = 1;
xArray[2] = 1;
xArray[3] = 2;
int yArray[4];
yArray[0] = 1;
yArray[1] = 2;
yArray[2] = 3;
yArray[3] = 1;
for(int row = grid_y_length+2; row >= -1; row--)
{
for(int col = 0; col <= grid_x_length+3; col++)
{
// print empty space at the start and end
if(((col == 0) && (row == 0)) || ((col == 0) && (row == grid_y_length+2)))
cout << setw(5) << right << " ";
// print y axis scale
elseif ((col == 0) && (row >= 1) && (row < grid_y_length+3))
cout << setw(3) << right << y_counter--;
// print empty space at the start
elseif (((col == 1) && (row == -1)) || ((col == grid_x_length+3) && (row == -1)))
cout << setw(8) << right << " ";
// print x axis scale
elseif ((col >= 1) && (row == -1))
cout << setw(3) << left << x_counter++;
elseif ((row == grid_y_length+2) && (col > 0) && (col <= grid_x_length+3)) // top border
cout << setw(3) << left << "#";
elseif ((row == 0) && (col > 0) && (col <= grid_x_length+3)) // bottom border
cout << setw(3) << left << "#";
elseif ((col == 1) && (row > 0) && (row < grid_y_length+2)) // left border
cout << setw(3) << right << "#";
elseif ((col == grid_x_length+3) && (row > 0) && (row < grid_y_length+2)) // right border
cout << setw(24) << right << "#";
else
// print X location
for(int i = 0; i < 4; i++)
{
if (row == yArray[i]+1 && col == xArray[i]+2)
{
cout << setw(6) << right << "X";
}
}
}
cout << endl;
}
return 0;
}
# # # # # # # # #
6 # #
5 # #
4 # #
3 # X #
2 # X #
1 # X X #
0 # #
# # # # # # # # #
0 1 2 3 4 5 6
Program ended with exit code: 0
The only sense I can make out of your 'graphic' is you are plotting bar charts.
If that's the case and the data is changing, rather than being a one-off, then like @jonnin indicates you need to consider stop digging because it's the very hard way to go about it. Your 'hours to solve it' are better spent on setting up an array of characters and then after that's done displaying the complete array line by line. That way you can throw out most, if not all, of your if's, set's, left's and right's because you won't need them.
It's your decision needless to say, so happy excavating if you like.
:)
Newbie812, your logic is inside out. You have generic loops and the within the loop, a ton of if statements to decide where you are the diagram. Here's a version that prints the pieces one at a time: