1

I am supposed to be creating a program that asks a user to populate an array of size 10. There are three functions which by their name are self-explanatory; one fills up the array with elements, the second one displays the array horizontally, and the third function checks to see if a number entered by the user is an element in the array.

#include<iostream>
#include<iomanip>
void fillUpArray(int array[], int size);
void displayArray(int array[], int size);
bool isNumberPresent(int array[], int size, int SearchNum);
    
int main(){
  int s = 10; //size of array
  int A[s]; //array A with size s
  int num; //search number
      
  fillUpArray(A, s);
    
  std::cout <<"\n";
    
  displayArray(A, s);
    
  std::cout << "\n";
    
  std::cout << "Enter a number to check if it is in the array:\n";
  std::cin >> num;
    
  std::cout << std::boolalpha << isNumberPresent(A, s, num) << std::endl;
    
  return 0;
      
}
    
void fillUpArray(int array[], int size)
{
  std::cout << "Enter 10 integers to fill up an array, press enter after every number:\n";
  for(int i = 0; i < size; i++){
        
    std::cin >> array[i];
  
  }
    
}

void displayArray(int array[], int size)
{
  for(int j = 0; j < size; j++){
    std::cout << array[j] << "\t";
  }
}

bool isNumberPresent(int array[], int size, int SearchNum)
{
  bool isPresent;
  for(int k = 0; k < size; k++){
    if(array[k] == SearchNum)
      isPresent = true;
    else
      isPresent = false;
  }
  return isPresent;
}

That last function, which is a bool function, is not performing the way I thought it would. I thought by doing array[k] whatever index k is then it should spit out the element in the array and then with the expression if(array[k] == SearchNum) it should then work as if(element == SearchNum) but that doesn't seem to be the case and the output is always false.

5
  • Step through your program in your debugger and note where the flow of execution differs from what you expect. Commented Mar 31, 2021 at 23:13
  • @chris Yeah sorry, I should have stated I don't program in your standard IDE. I use replit.com. I am in the process (as I finish typing this) of downloading visual studios since I can't take doing my CS homework without a proper debugger takes more time. Commented Mar 31, 2021 at 23:17
  • 1
    In that case, another tactic some people like to use is adding print statements to check execution flow and values until the problem is narrowed down to a specific line or otherwise small unit of code. Commented Mar 31, 2021 at 23:19
  • 1
    some questions that might help: what happens if the size of the array is 0? What if the search matches the very last item in array? What if it matches the first one but not the last one? Can it match more than one entry in the array? Commented Mar 31, 2021 at 23:24
  • @chris Thank you for the help. I did try doing those same tactics, I think I am getting somewhere! Sorry for the trouble! Commented Apr 1, 2021 at 2:50

1 Answer 1

2

The for loop in your isNumberPresent function will run to the end of the array (until k equals size) unconditionally; in each run of that loop, you set the value of the isPresent variable according to whether or not the current element is a match for searchNum, overwriting the previous value. So, the function, as it stands, will simply return whether or not the last element in the array is the same as the given test number.

You can simplify that function and remove the need for the local variable: if you find a match, then return true immediately; if the loop ends without finding a match, then return false:

bool isNumberPresent(int array[], int size, int SearchNum)
{
  for(int k = 0; k < size; k++){
    if(array[k] == SearchNum) return true; // Found a match - we can return immediately
  }
  return false; // We didn't find a match
}

Note, also, that Variable Length Arrays (VLAs) are not part of Standard C++, though some compilers (like GNU g++) support them (they are part of the C language according to the C99 Standard). In your program, as you only use one (fixed) value for the array size, you can conform to Standard C++ simply by qualifying that s is a const:

int main()
{
    const int s = 10; //size of array - make this a "const" be 'proper' C++
    int A[s]; //array A with size s
//...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.