Linear search in c , get array size


linearSearch.c


# include  <stdio.h>
# define NELEMS(arr) (sizeof(arr) / sizeof(arr[0])) // macro to calculate array size

int main(int argc, char *argv[]){
 int key, linear_arr[100], arr_size, i;

 //key = get_random(100);
 key = 16;
 printf("key===>%d\n",key );
 arr_size = NELEMS(linear_arr);

 //loop to generate array of randome numbers
 for(i=0; i< arr_size; i++ ){
  linear_arr[i] = get_random(100);
  printf("%d, ",linear_arr[i] );
 }

 //loop to linear search array for key
 for(i=0; i< arr_size; i++ ){
    if (key == linear_arr[i]){
      printf("\nkey found----%d\n", key);
      break;
    }
  }
}

//to generate random number 
int get_random(int limit){
  return rand() % limit ;
}
Advertisements

I am Senior Software Engineer. I love reading, writing, sharing,developing, hiking, movies, trips, mountains, brooks, hills etc.

Tagged with: ,
Posted in c language, Parallel Programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

StackOverFlow
Categories
Archives
%d bloggers like this: