using modulus to correct row and column numbers?

closed account (1Ck93TCk)
Hello,
I'm working on this program for class. It fills an array with blank spaces and then accepts a row/column from a user and places a star in that location.

Part of the instructions are really unclear to me, and it's now the weekend so I can't ask my teacher. I'm hoping someone on here can help.

The instructions say "if the user requested row or column are not within the bounds of the array, then use the modulus operator to correct the row and column."

I've never used modulus this way and can't find any examples in my text book or online. Can anyone clear this up for me? I really appreciate it!

Here's my program so far:
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
73
74
75
76
77
78
79
80
81
82
83
84
85
 #include <iostream>
#include <string>

using namespace std;

const int MAX_ROWS = 80;
const int MAX_COLUMNS = 80;

void placeAStar(string starryNight[MAX_ROWS][MAX_COLUMNS], int &rowSelection, int &columnSelection, const string star);
void printSky(string starryNight[MAX_ROWS][MAX_COLUMNS], int &userRowMax, int &userColumnMax);

int main()
{
    int userRowMax;
    int userColumnMax;
    string starryNight[MAX_ROWS][MAX_COLUMNS];
    int j;
    int i;
    int rowSelection = 1;
    int columnSelection = 1;
    const string star = "*";

    cout << "Enter the number of rows you want (greater than 1 and up to 80): ";
    cin >> userRowMax;

    cout << "Enter the number of columns you want(greater than 1 and up to 80): ";
    cin >> userColumnMax;



        if (userRowMax < 2 || userRowMax > MAX_ROWS || userColumnMax < 2 || userColumnMax > MAX_COLUMNS)
        {
            cout << "That number is out of range.";
            exit(1);
        }

        else
            for (int i = 0; i < userRowMax; i++)
        {
                for (int j = 0; j < userColumnMax; j++)
                {
                    starryNight[i][j] = " ";
                }
        }

        cout << "The range for the rows starts at zero and ends at " << userRowMax - 1 << endl;
        cout << "The range for the columns starts at zero and ends at " << userColumnMax - 1 << endl;


    while(rowSelection != 0 || columnSelection != 0)
    {
        cout << "Enter a row and column number to place a star. Press 0 to quit: ";
        cin >> rowSelection >> columnSelection;

        if(rowSelection <= MAX_ROWS && columnSelection <= MAX_COLUMNS)
        {
            placeAStar(starryNight, rowSelection, columnSelection, star);
            printSky(starryNight, userRowMax, userColumnMax);
        }
    }
    return 0;
}

void placeAStar(string starryNight[MAX_ROWS][MAX_COLUMNS], int &rowSelection, int &columnSelection, string star)
{
    starryNight[rowSelection][columnSelection] = star;
}

void printSky(string starryNight[MAX_ROWS][MAX_COLUMNS], int &userRowMax, int &userColumnMax)
{
    int k;
    int l;

    for (int k = 0; k < userRowMax; k++)
        {
            for (int l = 0; l < userColumnMax; l++)
            {
                cout << starryNight[k][l];
            }
            cout << endl;
        }

}



Thank you in advance!
jmb
%n gives a number from 0 to n-1
so for an array

char x[100];
unsigned int index; //I think you want unsigned to handle (correct for) negative user input. Negatives and modulo can give unexpected results until you look at how it is implemented and why it does what it does.
cin >> index; //say its 1234;
x[index%100] = '*'; //location 34 is set, 1234%100 is 34.

you are going about learning in a way that will make life hard for you.
if you understood what % did, you wouldn't need my code or an example, you would have seen it easily. If you don't understand % or any other fundamental tool, stop and play with it a bit, get a really firm grasp of what it is, what it does, how it works. Like xor.. xor has a bunch of cool properties, and if you understand even half of them you can do a ton of things with it.
Last edited on
closed account (1Ck93TCk)
Thank you! I didn't even know that I didn't understand %. I'm struggling and my class is entirely online, so I don't have easy access to anyone that can help (except here, of course). I have my textbook which I study like crazy, but I'm constantly running into things that are not covered by the book (using % in this way is not in the book, for example), and so it becomes trial and error. I agree, not the best way to go about learning. I am going to educate myself on %. Anyway, thanks again!
Topic archived. No new replies allowed.