Home > OS >  Lack of searched element in array returns wrong value (pointers searching) in C
Lack of searched element in array returns wrong value (pointers searching) in C

Time:07-21

It's probably a stupid problem, but I can't get it right. It's supposed to look like that:

Input:
2            // amount of data sets
5            // amount of numbers in array
1 2 3 1 5    //array elements
3            //searched element index  1
4            //and so on
4 3 2 1
5

Output:
3
None

But if there is no searched value program returns "98779" instead of "NONE" I have no idea whats wrong. Here is the code

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
  int tab[100000];
  int x,y,z,elem;
  cin >> x;
  
    for(int i=0;i<x;i  )
    {
      cin >>y;
      for(int j=0;j<y;j  )
        {
          cin >> z;
            tab[j]=z;
        }
      cin >> elem;
      int n = sizeof(tab)/sizeof(tab[0]);
      auto itr = find(tab, tab   n, elem);
      
      if (itr != end(tab))
      {
        cout << distance(tab,itr) 1;
      }
      else 
      {
          cout << "NONE";
      }
    }
  return 0;
}

CodePudding user response:

For starters you need to include header <iterator>

#include <iterator>

These statements

  int n = sizeof(tab)/sizeof(tab[0]);
  auto itr = find(tab, tab   n, elem);

are incorrect.

You entered y elements in the loop

  cin >>y;
  for(int j=0;j<y;j  )
    {
      cin >> z;
        tab[j]=z;
    }

So the variable n must be equal to y.

So you have to write

  int n = y;

  auto itr = find(tab, tab   n, elem);
  
  if (itr != tab   n )
  {
    cout << distance(tab,itr) 1;
  }
  else 
  {
      cout << "NONE";
  }

Pay attention to that instead of the array it would be better to use the standard container std::vector<int> as shown in the demonstration program below.

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    unsigned int count = 0;

    if ( std::cin >> count )
    {
        while ( count-- )
        {
            unsigned int n = 0;

            if ( std::cin >> n )
            {
                std::vector<int> v;
                v.reserve( n );

                std::copy_n( std::istream_iterator<int>( std::cin ), n,
                             std::back_inserter( v ) );

                int elem = 0;
                std::cin >> elem;

                auto it = std::find( std::begin( v ), std::end( v ), elem );

                if ( it != std::end( v ) )
                {
                    std::cout << std::distance( std::begin( v ), it )   1 << '\n';
                }
                else
                {
                    std::cout << "NONE\n";
                }
            }
        }
    }
}

CodePudding user response:

The lines

int n = sizeof(tab)/sizeof(tab[0]);

and

if (itr != end(tab))

are bad.

The lines mean to search from the whole array despite of only the first few elements are initialized.

The lines should be

int n = y;

and

if (itr != tab   n)
  • Related