-1

I am writing a program that's supposed to search an array that is filled by user input and return different output depending on whether or not another integer given by the user is in that array. the output is the index of the element.

e.g., suppose my array is {1, 2, 3}. using search(), if I enter 2, it should tell me that 2 is in the array and that its index value is 1.

But for some reason, this function only works correctly if I enter the very first element. This means that if I search for 1 in the array above, it will tell me that the index value is 0 like it's supposed to, but it won't do that for other elements.

My code is below. What am I doing wrong here?

#include <iostream>

using namespace std;

const int DECLARED_SIZE = 20;

void fillArray(int a[], int size, int& numberUsed);
int search(const int a[], int numberUsed, int target);
int search2(const int a[], int numberUsed, int target);

int main() {
  int size;

  cout << "Enter the array size: ";
  cin >> size;

  int arr[size], listSize, target;

  fillArray(arr, size, listSize);

  char ans;
  int result;

  do {
    cout << "Enter a number to search for: ";
    cin >> target;
    cout << endl << endl;

    result = search(arr, size, target);

    if (result == -1) {

      cout << target << " is not on the list." << endl << endl;
      cout << "Search again? (y/n): ";
      cin >> ans;
      cout << endl << endl;
    }
    else {
      cout << target << " is stored in array position " << result << "." << endl << endl;
      cout << "Search again? (y/n): ";
      cin >> ans;
      cout << endl << endl;
    }

  } while ((ans != 'n') && (ans != 'N'));

  cout << "End of program." << endl;
  return 0;


}

void fillArray(int a[], int size, int& numberUsed) {

  cout << "Enter up to " << size << " non-negative whole numbers." << endl;
  cout << "Mark the end of the list with a negative number." << endl;

  int next, index = 0;

  cin >> next;

  while ((next >= 0) && (index < size)) {

    a[index] = next;
    index++;
    cin >> next;
  }

  numberUsed = index;
}


//searches an array that is filled by the user

//this is where i think i am struggling

int search(const int a[], int numberUsed, int target) {

  int index = 0;
  bool found = false;

  while ((!found) && (index < numberUsed)) {

    if (target == a[index]) {
      found = true;
    }
    else {
      index++;
    }

    if (found) {
      return index;
    }
    else {
      return -1;
    }
  }

  return 0;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
qwerty
  • 213
  • 1
  • 7
  • 1
    You are checking to see if the element is found *inside* the loop, and if it isn't, then you are returning -1. You loop will only iterate once because of this – GBlodgett Mar 18 '20 at 15:42
  • 1
    [`int arr[size]` is ill-formed](https://stackoverflow.com/questions/57367473/create-an-array-when-the-size-is-a-variable-not-a-constant/57367539#57367539). –  Mar 18 '20 at 15:42
  • 2
    @Chipster no it's not UB, it's simply non standard and some compilers support it as an extension. – Jabberwocky Mar 18 '20 at 15:44
  • 1
    @Chipster It's "ill-formed". – HolyBlackCat Mar 18 '20 at 15:45
  • @Chipster No it is not undefined behavior. It's just not part of the c++ standard. Some compilers support this, others will throw a compile time error. – Eric Mar 18 '20 at 15:45
  • Sorry, everyone. I edited my comment. –  Mar 18 '20 at 15:46
  • @Chipster it's not ill formed on the OP's platform. His compiler has that non standard extension. – Jabberwocky Mar 18 '20 at 15:51
  • Better would be to use a vector and avoid having to manage size yourself. – stark Mar 18 '20 at 15:56
  • 1
    You should pass `listSize` to `search`, not `size`. – molbdnilo Mar 18 '20 at 15:58

2 Answers2

3

If you look at your search function you will see that it always returns at the bottom of the while loop. That's why you only find the first number. What you should do is return if you find the number but carry on if you don't. Like this (with some other simplifications of your code)

int search(const int a[], int numberUsed, int target) {
  for (int index = 0; index < numberUsed; index++) {
    if (target == a[index]) {
      return index;
    }
  }
  return -1;
}
john
  • 85,011
  • 4
  • 57
  • 81
1

In your while loop in your search function, you're doing:

if (found) {
    return index;
} else {
    return -1;
}

Which means if you didn't find your input, it immediately returns -1 instead of trying the next index. You should only return when you've visited all other indexes.

Arno Deceuninck
  • 320
  • 3
  • 10