Sorting and Searching Arrays

#include <iostream> // need for, cout, cin
using namespace std;

// Function prototypes
void getIntegers ();
void sortIntegers (int [], int);
int binaryIntegers (const int [], int);

// Global variables
const int TEN_INTEGERS = 10; // number of integers
int integers [TEN_INTEGERS]; // integers

int main ()
{
// Get the ten integers from the keyboard

getIntegers ();

// sort the ten integers that the user input

sortIntegers (integers, TEN_INTEGERS);

// use a binary search to determine if the value inputted
// by the user is in the array

int results; // hold the search results

results = binaryIntegers (integers, TEN_INTEGERS);

// if results contains -1 the ID was not found

if (results == -1)
cout << "The integer that you have entered does not exist within the array.\n";

else
{
// otherwise results contain the subscript of the specified
// integer within the array

cout << "That integer is found at element " << results;
cout << " in the array.\n";
}
return 0;
}

//**************************************************************
//
// Function Name: getIntegers
//
// Purpose: menu prompt for user to input values into the array
//
// Input parameters: int
//
// Output parameters: request user for ten integers
//
// Return Value: 0
//
//**************************************************************

void getIntegers ()
{

cout << "Please enter ten integers (whole numbers)\n";

for (int count = 0; count < TEN_INTEGERS; count++)
{
cout << "\nInteger " << (count +1) <<": ";

cin >> integers [count];
}
}

//**************************************************************
//
// Function Name: sortIntegers
//
// Purpose: sort integers within the array
//
// Input parameters: integers [TEN_INTEGERS]
//
// Output parameters: sorted integers [TEN_INTEGERS]
//
// Return Value: 0
//
//**************************************************************

void sortIntegers (int array[], int size)
{
bool swap;
int temp;

do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count+1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}

//**************************************************************
//
// Function Name: binaryIntegers
//
// Purpose: use a binary search to figure out whether the
// integer entered is within the array
//
// Input parameters: integers [TEN_INTEGERS]
//
// Output parameters: an integer of integers [TEN_INTEGERS]
//
// Return Value: 0
//
//**************************************************************

int binaryIntegers (const int array [], int size, int value)
{
int first = 0, // first array element
last = size - 1, // last array element
middle, // midpoint of search
position = - 1; // position of search value
bool found = false; // Flag

while (!found && first <= last)
{
middle = (first + last) / 2; // calculate midpoint
if (array[middle] == value) // If value found at mid
{
found = true;
position = middle;
}
else if (array[middle] > value) // If value is in lower half
last = middle -1;
else
first = middle + 1; // If value is in upper half
}
return position;
}



For some reason Microsoft visual c++ 2010 express keeps saying error LNK2019: unresolved external symbol "int __cdec1 binaryIntegers(int * const, int)" (?binaryIntegers@@YAHQAHH@Z) referenced in function _main

Can someone please help me
The function declaration

int binaryIntegers (const int [], int);

does not correspond to its definition

int binaryIntegers (const int array [], int size, int value)
Last edited on
Your forward declaration for binaryIntegers states that it takes two arguments:
 
int binaryIntegers (const int [], int);


Your implementation of binaryIntegers takes three arguements:
 
int binaryIntegers (const int array [], int size, int value)

That function has a different signature and does not satisfy the reference where you call it with two arguments:
 
results = binaryIntegers (integers, TEN_INTEGERS);


The forward declaration and implementation must match EXACTLY.

PLEASE USE CODE TAGS (the <> formatting button) when posting code. It makes it easier to help you.
Thanks...but now I have no idea what my third argument is for results = binaryIntegers (integers, TEN_INTEGERS);...
You must have thought it was useful, because you reference the third argument in two if statements inside binaryIntegers:
 
if (array[middle] == value) // If value found at mid 

Topic archived. No new replies allowed.