Arrays

If you have and array, for example char potatoes[100], and you know that in the n number of the array and only that number has the element G, can you know n?
Last edited on
You could use a loop that go through all array elements until you find the value you are looking for.
Last edited on
Is that the best way? Something like this?
1
2
3
4
5
6
7
8
9
10
int counting = -1;
for(int x = 0;x<100;x++)
    {
        counting++;
        if(potatoes[x]=='G')
        {
            break;
        }
}
cout<<potatoes[counting]<<endl;
If I am understanding your question correctly, I've wondered the same thing myself and I'm pretty certain there is no better way besides using a loop. Someone can correct me if I'm wrong, but to my understanding you are able to get the value of an array at a particular index with one operation (i.e. potatoes[5]), but not the other way around: using only one operation to get the particular index of an array when given the value.

And a few tips that I think would clean up your code a bit: you output the value of potatoes[counting] when you mentioned that you were looking for the index, which is just the value of counting. Based on your code, the value you output will always be 'G', or the last element in the array if there is no G.
You also don't need the counting variable. You can loop by incrementing x and then once you find the value you're looking for, you can output x right in the conditional statement, call the break, and then you're done.
There's no need to increment a variable until you find the one you want. Just remember where it was found, if it was found. This allows you to use -1 to mean not found, which is a common convention.

(With your approach you should check that counting is less than the array size before using it to access the array element.)

Andy

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
#include <iostream>
using namespace std;

int main()
{
    char potatoes[100] = {0}; // zero all elements

    const int count = sizeof(potatoes) / sizeof(potatoes[0]);

    potatoes[42] = 'G';

    int found_at = -1;
    for(int index = 0; index < count; ++index)
    {
        if(potatoes[index] == 'G')
        {
            found_at = index;
            break;
        }
    }

    if(-1 == found_at)
        cout << "not found" << endl;
    else
        cout << "found at " << found_at << endl;

    return 0;
}

Last edited on
there is no better way besides using a loop.

While you can't really avoid using a loop, you don't need to write the loop yourself. You can use one the C++ standard library's algorithms.

Also, potatoes would be better off it was a std::array<char> (if a fixed size) or std::vector<char>.

Andy

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
// this code needs a C++11+ compiler
#include <iostream>
#include <algorithm>
#include <iterator>
// not using using namespace std; here

int main()
{
    char potatoes[100] = {0}; // zero all elements

    potatoes[42] = 'G';

    auto iter_beg = std::begin(potatoes);
    auto iter_end = std::end(potatoes);
    
    // find uses a loop for you
    auto iter = std::find(iter_beg, iter_end, 'G');
    
    if(iter_end == iter)
        std::cout << "not found" << std::endl;
    else
        std::cout << "found at " << std::distance(iter_beg, iter) << std::endl;

    return 0;
}


or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// this code needs a C++11+ compiler
#include <iostream>
#include <array>
#include <algorithm>
#include <iterator>

int main()
{
    std::array<char, 100> spuds{}; // zero all elements

    spuds[42] = 'G';
    
    auto iter = std::find(std::begin(spuds), std::end(spuds), 'G');
    
    if(std::end(spuds) == iter)
        std::cout << "not found" << std::endl;
    else
        std::cout << "found at " << std::distance(std::begin(spuds), iter) << std::endl;

    return 0;
}
Last edited on
Topic archived. No new replies allowed.