Efficient way to fill a 2D Array?

The function "arrayFill" fills a [10][10] 2D array with a sentence the user enters, and it works. I was wondering if their is a more efficient way of doing this?

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
86
87
88
89
90
91
92
93
94
95
96
97
  #include <iostream>

using namespace std;

class Functions
{
    private:
        char cipherArray[10][10];
        int input{};
        int counter{};
        string sentance;

    public:
        void arrayInitialize (char (&tempArray)[10][10])
        {
            for (int j=0;j<10;j++)
            {
                for (int i=0;i<10;i++)
                {
                    tempArray[j][i]='#';
                }
            }
        }

        void userInput (string &tempString)
        {
            cout << "Enter a sentance: ";
            getline (cin, tempString);
            cout << endl;
        }

        void arrayFill (char (&tempArray)[10][10])
        {
            for (int j=0;j<10;j++)
            {
                for (int i=0;i<10;i++)
                {
                    tempArray[j][i]=sentance[counter];
                    counter++;

                    if (counter==sentance.length())
                    {
                        break;
                    }
                }

                if (counter==sentance.length())
                {
                    break;
                }
            }

            for (int j=0;j<10;j++)
            {
                for (int i=0;i<10;i++)
                {
                   cout << tempArray[j][i] << "    ";
                }
                cout << endl;
            }
            cout << endl;
        }

        void menu ()
        {
            while (input!=3)
            {
                cout << "1. Transposition cipher" << endl;
                cout << "2. Code breaker" << endl << endl;
                cout << "Enter the number of the option you want to proceed with: ";
                cin >> input;
                cout << endl;
                cin.ignore();

                switch (input)
                {
                    case 1:
                        arrayInitialize(cipherArray);
                        userInput(sentance);
                        arrayFill(cipherArray);
                        break;

                }
            }
        }

};

int main()
{
    Functions program;
    program.menu();


    return 0;
}
Last edited on
I was wondering if their is a more efficient way of doing this?

Well, class properties are visible inside the methods of the same class, so you don’t need to pass them round.
Efficiency is important, but in OOP making a code easy to read and to maintain is pretty beneficial too, I think.
Here’s your code in ‘different clothing’:
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
#include <iostream>
#include <string>

constexpr unsigned ROWS { 10 };
constexpr unsigned COLS { 10 };

class Functions {
private:
    char cipher[ROWS * COLS];
    std::string sentence;

public:
    Functions();
    void menu ();
    void userInput();
    void arrayFill();
};

Functions::Functions()
{
    for (unsigned j = 0; j < ROWS; ++j) {
        for (unsigned i = 0; i < COLS; ++i) { cipher[j * COLS + i] = '#'; }
    }
}

void Functions::userInput()
{
    std::cout << "Enter a sentence: ";
    getline (std::cin, sentence);
}

void Functions::arrayFill()
{
    for (unsigned j = 0, counter {}; j < ROWS; ++j) {
        for (unsigned i = 0; i < COLS && counter < sentence.length(); ++i) {
            cipher[j * COLS + i] = sentence.at(counter++);
        }
    }

    for (unsigned j = 0; j < ROWS; ++j) {
        for (unsigned i = 0; i < COLS; ++i) {
           std::cout << cipher[j * COLS + i] << "    ";
        }
        std::cout << '\n';
    }
    std::cout << '\n';
}

void Functions::menu()
{
    int input {};
    while (input != 3) {
        std::cout << "1. Transposition cipher\n"
                     "2. Code breaker\n\n"
                     "Enter the number of the option you want to proceed with: ";
        std::cin >> input;
        std::cin.ignore();

        switch (input) {
        case 1:
            userInput();
            arrayFill();
            break;
        case 2: std::cout << "Not implemented yet!\n"; break;
        default: break;
        }
    }
}

int main()
{
    Functions program;
    program.menu();
    return 0;
}

Topic archived. No new replies allowed.