#include \"helpers.h\" /** * Returns true if value is in array of n values, else
ID: 3593878 • Letter: #
Question
#include "helpers.h"
/**
* Returns true if value is in array of n values, else false.
*/
bool binary_search(int value, int values[], int n);
bool search(int value, int values[], int n);
void selection_sort(int values[], int n);
// TODO: implement a searching algorithm
{ if (value < 0)
return false;
else
return binary_search( values, values, n); }
/**
* sorts array of n values
*/
void sort(int values[], int n)
{
// TODO: Implement an 0(n^2) sorting algorithm
selection_sort(values, n);
return;
}
// Custom search functions
bool linear_search(int value, int values[], int n)
{
for (int i = 0; i < n; i++)
if (values[i] == value)
return true;
return false;
}
bool binary_search(int value, int values[], int n)
{
int ending = n-1;
int beggining =0;
while (ending >= beggining)
{
int middle = (beggining + ending) / 2;
if (values[middle] == value)
return true;
else if (values[middle] > value)
ending = middle - 1;
else
beggining = middle + 1;
}
return false;
}
// Custom sort functions
void bubble_sort(int values[], int n)
{
bool iter;
do
{
iter = false;
for (int i = 0; i < n - 1; i++)
{
if (values[i] > values[i+i])
{
int temp = values[i];
values[i] = values[i+i];
values[i+i] = temp;
iter = true;
}
}
} while (iter);
return;
}
void selection_sort(int values[], int n)
{
int temp;
for (int i = 0; i < n; i++)
{
int smallest_index = i;
for (int j = i+1; j < n; j++)
{
if (values[smallest_index] > values[j])
smallest_index = j;
}
//switch values;
temp = values[smallest_index];
values[smallest_index] = values[i];
values[i] = temp;
}
}
helpers.c:21:4: error: expected identifier or '('
{ if (value < 0)
^
1 error generated.
make: *** [helpers] Error 1
How do I fix this bug in my code ?
Explanation / Answer
You are getting error at bold part of the code
void selection_sort(int values[], int n);
// TODO: implement a searching algorithm
{ if (value < 0)
return false;
else
return binary_search( values, values, n); }
See here you have opened the bracket and checking the value whether less than 0. But what is the name of the function. As you did not mention the function name it has to be something like
return_type function_name(int value)
{
if (value < 0)
return false;
else
return binary_search( values, values, n);
}
If you complete the function, the errors will be gone.
**Comment for any further queries.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.