0
enter code here
#include<cs50.h>
#include<stdio.h>
#include<string.h>


int main(int argc, string argv[]);
bool search (int value, int values[], int n);

{
  if(argc!= 2)
   {
     return 1;
   }

int k = atoi (argv[1]);

printf("Enter a number to search:");


// Search each and every element through loop

for(int i=0; i<=value; i++)
    if values[i] == value;
    {
        printf("element found in location");
    }
    else
    {
        print("element not found");
    }
}

I am assure the code is partially correct and logic is correct and I am unsure about the error. Could you please help me on the linear search. Awaiting for response

0

This code is highly messed up. Your code starts with two function declarations. Then you place a code block, which should be part of a function definition.

Function declarations look like

int search_char_in_string(char* haystack, char needle, int offset);

with variable names optional. Those are meant to tell the compiler what the function's signature is, and is needed if you want to call a function before its definition. A definition would look like

int search_char_in_string(char* haystack, char needle, int offset)
{
    // do some fancy stuff
    return something;
}

(important: No semicolon at the end of the first line for a definition)

Now to your linear search: You compare index i with value value, that's like comparing house numbers to an account balance. Both are numbers, but they are not comparable. Compare i to n instead, and use <n, because valid indices range from 0 to n-1.

1
  • Thank you so much for clearing such valuable queries. I will try my best to execute. Oct 16 '17 at 12:52

You must log in to answer this question.

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