C++ code for uniform cost search algorithm

This code shows a function uniformSearch that will search an array and will take a uniform amount of time, except if the array is really, really big. If that happens, you can increase the amount of time the search should take until it takes a uniform amount of time again.

The uniform amount of time it will takes (in seconds) is passed into the function as a parameter.

#include <algorithm>
#include <ctime>
#include <iostream>

int* uniformSearch(int* arrayToSearch, int sizeofArray, int valueToFind, int timeToTake);

int main()
{

  const int sizeOfArray = 5;
  int arrayToSearch[sizeOfArray] = {10,20,30,40,50};
  int valueToFind = 40;
  int secondsToTake = 5;

  int* pointerToFoundValue = uniformSearch(arrayToSearch, sizeOfArray, valueToFind, secondsToTake);
  if (pointerToFoundValue < arrayToSearch+sizeOfArray)
    {
      std::cout << "Found value in element " << pointerToFoundValue - arrayToSearch << ". Remember that the first element is element 0." << std::endl;

    }
  else
    {
      std::cout << "Did not find value" << std::endl;
    }

  valueToFind = 38;
  pointerToFoundValue = uniformSearch(arrayToSearch, sizeOfArray, valueToFind, secondsToTake);
  if (pointerToFoundValue < arrayToSearch+sizeOfArray)
    {
      std::cout << "Found value" << std::endl;
    }
  else
    {
      std::cout << "Did not find value" << std::endl;
    }



}

int* uniformSearch(int* arrayToSearch, int sizeofArray, int valueToFind, int timeToTake)
{

  time_t startTime;
  time(&startTime);

  int* foundPointer = std::find(arrayToSearch, arrayToSearch + sizeofArray, valueToFind);

  time_t checkTime;
  while(time(&checkTime) - startTime < timeToTake)
    {}

  return foundPointer;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.