Help with hw

Hello smart people!
So this is my first c++ class a=in college and i like c++ the most out of all the languages but professor put up extra credit which is really tough because its on arrays which is absolutely the toughest for me. Can i get some feedback to where I can start?


this is the what the program has to be:

1. Write a program that simulates a n x n treasure map. a) Request a map size between 5 and 9. Verify size with a validation loop. If size is incorrect, output an error and request new values. b) Request a starting row/column for the treasure location on the map. Validate row and column to insure that they are within the borders of the map. If values are incorrect, output an error and request new values. c) Use a nested loop to display the map (see output example). - X and Y coordinate labels should be displayed - Treasure location should be marked with an ‘X’. - Blank coordinates should be marked with a ‘-‘. d) Use a switch statement within a while loop to display menu options: 1) Update treasure coordinates (row/column). 2) Shift X left or right. 3) Shift X up or down. 4) Exit Program. d) If 1 is selected: - Request row and column coordinate information. - Validate input with an error loop (similar to step b) - Display the updated map and menu options. e) If 2 is selected: - Request a (-/+) horizontal offset. Negative right, positive left. - Validate proposed location with an error loop. If location cannot exist on the current map, output an error and repeat request. - Display the updated map and menu options. f) If 3 is selected: - Request a (-/+) vertical offset. Negative up, positive down. - Validate proposed location with an error loop. If location cannot exist on the current map, report an error and repeat request. - Display the updated map and menu options. g) If 4 is selected: - Exit the menu while loop and output ‘Exiting program.’ Hints: (1) create an integer variable dimension. (2) create a global while loop with ‘4’ as the sentinel value (3) create a switch statement for the menu within the while loop (4) create the map with a nested loop using the dimension variable (5) use branching mechanisms within the loop to set and update X. (6) use do/while loop structures to validate input values
2
Introduction to C++ Programming Design and Implementation S. Trowbridge 2017
Output: Treasure Map ------------ Enter a map size between 5 and 9 inclusive: 10 Map size incorrect.
Enter a map size between 5 and 9 inclusive: 9 Initial location of X (row column): 10 1 Starting point must be a valid position on the map. Initial location of X (row column): 4 3
0 - - - - - - - - -
1 - - - - - - - - -
2 - - - - - - - - -
3 - - - - - - - - -
4 - - - X - - - - -
5 - - - - - - - - -
6 - - - - - - - - -
7 - - - - - - - - -
8 - - - - - - - - -
0 1 2 3 4 5 6 7 8 1)

Update treasure coordinates (row/column). 2) Shift X left or right. 3) Shift X up or down. 4) Exit program. Select: 2
Shift X left or right (negative values left, positive values right): -4 Treasure location must be within the confines of the map.
Shift X left or right (negative values left, positive values right): -1
0 - - - - - - - - -
1 - - - - - - - - -
2 - - - - - - - - -
3 - - - - - - - - -
4 - - X - - - - - -
5 - - - - - - - - -
6 - - - - - - - - -
7 - - - - - - - - -
8 - - - - - - - - -
0 1 2 3 4 5 6 7 8
Sure thing. I'll get you started with the very first part, (1a) and how to get the X where you want it. Since this is your very first class, you most likely have been introduced to arrays. You want your array to be a 2D array to hold the information you want. Let's use an array of type char so we can use the "-" and "X" characters. We set an initial size to it too. Remember, an array needs a constant size. Let's try 5 by 5 for simplicity.
 
char treasureMap[5][5];

So, there's our treasure map array that is made up of 5 rows and 5 columns.
Let's initialize all of them to some initial value, the "-". This can be done with a nested for loop.
1
2
3
4
5
6
7
8
for (int i = 0; i < 4; i++)
{
  for (int j = 0; j < 4; j++)
  {
    treasureMap[i][j] = '-'; //treasureMap[0][0] is now a "-", treasureMap[0][1], etc.
  }
}

So, we have given each element in the array an initial value of "-".
Now, at some point we can ask the user for the row and column where they want to place the "X". Once they input that information, we can over right the spot that needs it to make it a X.
1
2
//Say they chose, row 1, column 2
treasureMap[1][2] = 'X';

If you need the row and column number displayed as you have in your question, it is probably best to make those numbers display with the printing function, which would print your map with the row and column numbers as you have displayed.
Last edited on
Needs to use vector of vectors since both input parts dynamic. int* blah = new int[SOME_DYNAMIC]; is not legal in C++.

Working example for first input parts and drawing; can test at https://repl.it/repls/HealthyHoneydewTags

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
#include <iostream>
#include <vector>

using namespace std;

bool OutOfBounds(int row, int col, int size)
{
    return row<0 || col<0 || row>=size || col>=size;
}

void Show(const vector<vector<char>>& board)
{
    cout << endl;
    for (int r=0; r<board.size(); ++r)
    {
        cout << r << " ";
        for (int c=0; c<board[r].size(); ++c)
        {
            cout << board[r][c] << " ";
        }
        cout << endl;
    }
    cout << "  ";
    for (int i=0; i<board.size(); ++i)
        cout << i << " ";
    cout << endl;
}

int main() 
{
    int min_size = 5;
    int max_size = 9;
    cout << "Enter a map size from " << min_size << " to " << 
            max_size << "." << endl;
    cout << "? ";
    
    int size = -1;
    while ( cin >> size && (size<min_size || size>max_size) )
    {
        cout << "? ";
    }
    
    cout << "Enter two space-separated numbers to mark the treasure " <<
            "row and column, from 0 to " << size-1 << "." << endl;
    cout << "? ";
    int tr = -1, tc = -1;
    while (cin >> tr >> tc && OutOfBounds(tr,tc,size))
    {
        cout << "? ";
    }
    
    vector<vector<char>> board(size, vector<char>(size, '-'));
    board[tr][tc] = 'X';
    Show(board);
    
    return 0;
}


Possible output:
Enter a map size from 5 to 9.
?  -7
?  19
?  9
Enter two space-separated numbers to mark the treasure row and column, from 0 to 8.
?  12 24
?  -1 8
?  8 99
?  3 6

0 - - - - - - - - - 
1 - - - - - - - - - 
2 - - - - - - - - - 
3 - - - - - - X - - 
4 - - - - - - - - - 
5 - - - - - - - - - 
6 - - - - - - - - - 
7 - - - - - - - - - 
8 - - - - - - - - - 
  0 1 2 3 4 5 6 7 8 
I agree with above. Vectors would be a lot more suitable for this situation.
Topic archived. No new replies allowed.