0

I am trying to get started with Problem Set 3. I thought first to make a linear search program that would take an integer as an input and then search an array to see if the integer is within the array. I could make the array of numbers 10:19 with this code:

int main(void) 
{

int values[ 10 ]; 
int g;

for (g = 0; g < 10; g++ ) {
  values[ g ] = g + 10; /* set element at location i to i + 10 */
  printf("%i", values[g]);
}
printf("\n");
}

I then hoped to test the search function from helpers.c, by pasting the code below my array above.:

bool search(value, int values[], int m)
{
   // TODO: implement a searching algorithm
for (int i = 0; i < m; i++)
{
    if ( value == values[i])
    return true;
}
  return false;
} 

I originally thought to test it by placing the code within the main function, but I received "error: function definition is not allowed here." Now, I place it outside of the main function, though I am not sure how to give this function an input.

I received four errors:

 error: expected identifier
bool search(value, int values[], int m)
               ^
 error: use of undeclared identifier 'm'
for (int i = 0; i < m; i++)
                    ^
  error: use of undeclared identifier 'value'
    if ( value == values[i])
         ^
   error: use of undeclared identifier 'values'
    if ( value == values[i])

Can I get some help with how to proceed?

1

Function should be written as bool search(int value, int values[], int m)

Note the int before the variable name value.

2
  • Thanks, that removed the errors. I still wonder how to actually give the input value to the search function, since I cannot put this Boolean function within the main function. I thought to simply do value = get_int(); however that leads to more errors
    – Haim
    Apr 27 '17 at 19:45
  • 1
    You can call the function in main and store it's return value in a bool variable if you define it outside main. Also if the answer answered your question, mark it as answered by clicking on the tick mark beside the answer. Apr 28 '17 at 17:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .