1

I am trying to test my search function by creating a separate function that takes an input [some integer] and then checks whether that integer fits within the given array [numbers 10-19].

I made the array within a main function

int main(void) 

 {

   int values[ 10 ]; /* n is an array of 10 integers */
   int g;

   /* initialize elements of array n to 0 */         
   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 have the Bool linear search function after:

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; }

However, I have struggled to find a way to actually find a way to give an input for value.

I wanted to do something like:

int main(int argc, string argv[])   {

     string num = argv[1];
     int value = atoi(num);

However, this returns unused parameter and unused variable errors. I can't put the Bool function within main either. What can I do?

1

Your "small" program is a little complex, you can try the one I coded, based on your code to see if it serves you. The program accepts the entry of the number you want to search for the command line. The program can be improved a bit.

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <stdbool.h>

bool search(int value, int values[]);

int main(int argc, string argv[]) 
{
     if(argc != 2)
     {
         printf("usage: ./search <number>");
         return 1;
     }

     int value = atoi(argv[1]);
     //int value = atoi(num);


     int values[ 10 ] = {0}; /* n is an array of 10 integers */
     int g = 0;

   /* initialize elements of array n to 0 */         
       for (g = 0; g < 10; g++ ) 
       {
          values[ g ] = g + 10; /* set element at location i to i + 10 */
          printf("%i ", values[g]);
       }
     printf("\n");
     bool k = search(value, values);
     if(k)
     {
         printf("found\n");
     }
     else
     {
         printf("no found\n");  
     }
}   


bool search(int value, int values[]) 
{
    // TODO: implement a searching algorithm
    for (int i = 0; i < 10; i++)
    {
        if ( value == values[i])
        return true;
    }
    return false; 
}
| improve this answer | |
  • Thanks MARS, that is informative. I was wondering why your removed the third parameter of bool search(value, int values[],int m) ? – Haim Apr 28 '17 at 22:23
  • In the case of find the size is variable, you are proposing a fixed size, there is no need for a variable that is constant, for simplicity I delete it, but surely you can incorporate a variable size of the array, well we leave you some fun to you – MARS Apr 28 '17 at 22:41
  • Thanks MARS. Can you explain this part of your code: int values[ 10 ] = {0}; Why did you need the {0} ? – Haim Apr 30 '17 at 1:35
  • Is not really necessary, it's just a habit to initialize the variables at the time of the declaration, but you can remove it. – MARS Apr 30 '17 at 7:29

You must log in to answer this question.

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