-
-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into paras-2407-patch-1
- Loading branch information
Showing
1 changed file
with
19 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
linear_search<-function(vector, search_value){ #made a function named linear_search having two parameters that are an array and a value to be searched | ||
for(i in 1:length(vector)){ | ||
if(vector[i]==search_value){ #comparing each value of array with the value to be searched | ||
return (i) | ||
} | ||
} | ||
return (-1) | ||
} | ||
|
||
user_vec<- c(10,20,30,40,50,60) #input array (hard code) | ||
user_val<-30 #input value to be searched (hard code) | ||
|
||
result<-linear_search(user_vec,user_val) #linear_seach function calling | ||
|
||
if(result!=-1){ | ||
cat("Searched value", user_val, "found at index", result-1) #displaying the index at which value is found (if any) | ||
}else{ | ||
cat("Searched value does not exist in array") | ||
} |