# Searching Array for an Element using Linear Search

• Comp Sci
• iquicck
In summary: The scanf function requires the address of the variable you're going to input to.Try this:scanf("%d", &r);
iquicck
Homework Statement
I write a Linear Search code but ı redesign for ask to user version
Relevant Equations
Linear Search
I write a Linear Search code,then ı decided ask to user "r" and "int arr".I mean,User decide their r and arr numbers.I tried scanf("%d",r); command but doesn't work
This code my first code before the decide Ask user :
C:
#include <stdio.h>

int search(int arr[], int n, int r)
{
int i;
for (i = 0; i < n; i++)
if (arr[ i] == r)
return i;
return -1;
}int main(void)
{
int arr[] = { 7, 9, 12, 28, 17,32 };
int r = 17;
int n = sizeof(arr) / sizeof(arr[0]);

int result = search(arr, n, r);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}

Last edited by a moderator:
I fixed a formatting issue with the code. The forum interprets [i] as command to display following text in italics. To avoid that you can put code in [code] tags or write [plain][i][/plain].
iquicck said:
I tried scanf("%d",r); command but doesn't work
What exactly does not work?
iquicck said:
This code my first code before the decide Ask user :
The code that causes problems would be more useful to find the problem.

mfb said:
I fixed a formatting issue with the code. The forum interprets [i] as command to display following text in italics. To avoid that you can put code in [code] tags or write [plain][i][/plain].What exactly does not work?The code that causes problems would be more useful to find the problem.
Code is work but I want to redesign my code for ask to user for enter number to "r"
I wrote;
printf("Enter r number);
scanf("%d",r);
but it didnt work

iquicck said:
Code is work but I want to redesign my code for ask to user for enter number to "r"
I wrote;
printf("Enter r number);
scanf("%d",r);
but it didnt work
The prompt is not very useful. Instead of printing "Enter r number" it would be better to say "Enter the number to search for".

The scanf function requires the address of the variable you're going to input to.

Try this:
C:
scanf("%d", &r);

mfb said:
I fixed a formatting issue with the code. The forum interprets as command to display following text in italics. To avoid that you can put code in
Code:
 tags or write [plain][i][/plain].
I surrounded the code in post #1 with code tags. I also changed one line in the search function to arr[ i], with a space preceding the index i. This keeps the browser from displaying what follows as italics.

## 1. How does linear search work?

Linear search is a simple search algorithm that sequentially checks each element in an array until a match is found. It starts at the beginning of the array and compares each element with the target element until a match is found or the end of the array is reached.

## 2. What is the time complexity of linear search?

The time complexity of linear search is O(n), where n is the number of elements in the array. This means that the time it takes to search for an element in an array using linear search increases linearly with the size of the array.

## 3. What happens if the element is not found in the array?

If the element being searched for is not found in the array, linear search will continue until it reaches the end of the array. At this point, it will return a "not found" or "false" result, indicating that the element does not exist in the array.

## 4. Is linear search an efficient algorithm for searching arrays?

No, linear search is not considered an efficient algorithm for searching arrays. As the size of the array increases, the time it takes to search for an element also increases. This makes it a better option for smaller arrays or when the target element is more likely to be at the beginning of the array.

## 5. Are there any advantages to using linear search?

Linear search is a simple and easy-to-implement algorithm, making it a good choice for beginners or for situations where the array is small. It also does not require the array to be sorted, unlike other search algorithms. Additionally, linear search can be used on arrays of any data type, making it a versatile option.

• Engineering and Comp Sci Homework Help
Replies
3
Views
861
• Engineering and Comp Sci Homework Help
Replies
18
Views
1K
• Engineering and Comp Sci Homework Help
Replies
2
Views
2K
• Engineering and Comp Sci Homework Help
Replies
17
Views
1K
• Engineering and Comp Sci Homework Help
Replies
21
Views
2K
• Engineering and Comp Sci Homework Help
Replies
1
Views
2K
• Engineering and Comp Sci Homework Help
Replies
4
Views
2K
• Engineering and Comp Sci Homework Help
Replies
3
Views
1K
• Engineering and Comp Sci Homework Help
Replies
12
Views
1K
• Engineering and Comp Sci Homework Help
Replies
3
Views
782