How to access elements in array within an array?

Okay, so the idea is this:

Lets say I create an array of strings like below:

string word[4]

This creates a string array consisting of 5 strings. Now lets say I let the user input (using cin) what characters these 5 strings will be made of, like below:

1
2
3
4
5
6
    for(int i=0; i<5; i++)
    {
        cout << "sequence " << i << ": " << flush;
        cin >> word[i];
        cout << endl;
    }


Since a string itself is an array (of characters), now we have an array within an array.

Now how do I access a single character within this array of 5 strings?

Hope this is clear.
Last edited on
word[0][0] accesses the first character of the first string.
word[0][1] accesses the second character of the first string.
word[2][1] accesses the second character of the third string.
Don't forget to check the length of the string first.
Oh alright, so its a 2D Array.

Anyway, the problem I was attempting was EXERCISE 26 here: https://en.wikibooks.org/wiki/C%2B%2B_Programming/Exercises/Iterations

Here is my full program, but for some reason it doesn't work. It works SOMeTIMES, but most of the time it gets the number wrong. Try it out for yourself:

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
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main()
{
    string sequence[5]; // declare array of string
    char character;
    int cnt = 0, maximum = 0;

    cout << "Type in 5 sequences of characters: " << endl << endl;

    // lets user input string of characters and stores them in string array
    for(int i=0; i<5; i++)
    {
        cout << "sequence " << i+1 << ": " << flush;
        cin >> sequence[i];
        cin.ignore(1000, '\n');
        cin.clear();
        cout << endl;
    }

    cout << "\n";
    cout << "Which character would you like to search for? " << flush;
    cin >> character;

    // loops through each character of each sequence to find a match.
    for(int i=0; i<5; i++)
    {
        for(int k=0; k<sequence[i].length(); k++)
        {
            if(sequence[i][k]==character)
                cnt++;
        }

            if(cnt>maximum)
            {
                maximum=cnt;
                cnt=0;
            }
    }

    cout << "\n";
    cout << character << " has been used a maximum of " << maximum << " times" << endl;

   cin.get();

}



If I enter entire phrases (with spaces between words), it gets the number completely wrong. If I enter single words it gets it right, but only sometimes.

I have no idea what im doing wrong. Help would eb much appreciated.

[For the record, I know the solution is available in the link. Im purposely not looking at it as I want to try and figure this out myself and with some hints from you guys]
Last edited on
Arslan7041 wrote:
Oh alright, so its a 2D Array.
There is no such thing as a 2D array.

You may be having problems because of your use of the formatted extraction operator:

cin >> sequence[i];

This only reads a single word at a time. If you want to read the entire line, use:

std::getline(std::cin, sequence[i]);
There is no such thing as a 2D array.


Doesn't seem like something that should be mentioned without at least a little bit of elaboration. Can you explain what you mean by this? Why is there no such thing as a 2-Dimensional array?


This only reads a single word at a time. If you want to read the entire line, use:


Good idea. Just tried it, but still it doesn't work.
Computer memory is one dimensional - you only need a single number to represent a memory address. Using math, you can simulate a 2D array such as an image, but it's still 1D in memory. You can also have an array of arrays, but everything is still 1D. Personally, I find it more confusing to think about something as 2D when it isn't in reality - maybe you find it easier.

Please post your current code and I'll try running it.
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
#include <iostream>
#include <string>

int main()
{
    const std::size_t NUM_STRINGS = 5 ;
    std::string sequence[NUM_STRINGS] ;

    std::cout << "Type in 5 sequences of characters:\n\n" ;
    for( std::size_t i = 0 ; i < NUM_STRINGS ; ++i )
    {
        // by default, std::cout is tied to std::cin.
        // Unless we break the tie, std::cout is automatically flushed
        // before each input operation on std::cin
        std::cout << "sequence " << i+1 << ": " ;
        std::getline( std::cin, sequence[i] ) ;
    }

    std::cout << "\nWhich character would you like to search for? " ;
    char character ;
    std::cin >> character;

    int max_occurrences = 0 ;

    // favour range-based loops over classical loops
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( std::string str : sequence ) // for each string in the array
    {
        int num_occurrences_in_str = 0 ;
        for( char c : str ) // for each character in the string
            if( c == character ) ++num_occurrences_in_str ;

        if( num_occurrences_in_str > max_occurrences )
            max_occurrences = num_occurrences_in_str ;
    }

    std::cout << "\ncharacter '" << character << "' has been used a maximum of "
              << max_occurrences << " times.\n" ;
}
Here is my program:

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

using namespace std;

int main()
{
    string sequence[5]; // declare array of string
    char character;
    int cnt = 0, maximum = 0;

    cout << "Type in 5 sequences of characters: " << endl << endl;

    // lets user input string of characters and stores them in string array
    for(int i=0; i<5; i++)
    {
        cout << "sequence " << i+1 << ": " << flush;
        getline(cin, sequence[i]);
        cout << endl;
    }

    cout << "\n";
    cout << "Which character would you like to search for? " << flush;
    cin >> character;

    // loops through each character of each sequence to find a match.
    for(int i=0; i<5; i++)
    {
        for(int k=0; k<sequence[i].length(); k++)
        {
            if(sequence[i][k]==character)
                cnt++;
        }

            if(cnt>maximum)
            {
                maximum=cnt;
                cnt=0;
            }
    }

    cout << "\n";
    cout << character << " has been used a maximum of " << maximum << " times" << endl;

   cin.get();

}



@JLBorges: Sorry, I dont understand what you are doing with the loops and the link does not help. I'd love to learn it later, but I'd rather not bother with that right now.

I'd like to write this program using normal loops, thanks.
Arslan7041 wrote:
Here is my program:
Do you have a question?
Yes, of course. Like I said, its still not working. Try running it. Its been giving me wrong answers.

Can you look through it and see what I did wrong. Im at a loss.

I really appreciate your help by the way.
Last edited on
Aha! I figured out what the problem was. The cnt=0 statement should be OUTSIDE the if-statement.

Here is my finalized (and working) code:
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string sequence[5]; // declare array of string
    char character;
    int cnt = 0, maximum = 0;

    cout << "Type in 5 sequences of characters: " << endl << endl;

    // lets user input string of characters and stores them in string array
    for(int i=0; i<5; i++)
    {
        cout << "sequence " << i+1 << ": " << flush;
        getline(cin, sequence[i]);
        cout << endl;
    }

    cout << "\n";
    cout << "Which character would you like to search for? " << flush;
    cin >> character;

    // loops through each character of each sequence to find a match.
    for(int i=0; i<5; i++)
    {
        for(int k=0; k<=sequence[i].length(); k++)
        {
            if(sequence[i][k]==character)
                cnt++;
        }

            if(cnt>maximum)
                maximum=cnt;

            cnt=0;

    }

    cout << "\n";
    cout << character << " has been used a maximum of " << maximum << " times" << endl;

   cin.get();

}


Thank you everyone for you help.
if(sequence[i][k]==character)

you really ought to get into the habit of using sequence[i].at(k)

the difference being that if(sequence[i][k]==character) will not complain if k = 1000000 and if(sequence[i].at(k)==character) will stop your program and you may not understand exceptions now, but they will be very helpful later in debugging your code.
Topic archived. No new replies allowed.