Comp Sci Searching Array for an Element using Linear Search

AI Thread Summary
The discussion focuses on issues with implementing a linear search in C, specifically regarding user input for the search element. The user attempted to use `scanf("%d", r);` but encountered problems because the address of the variable was not provided. The correct usage is `scanf("%d", &r);`, which allows the program to store the input correctly. Additionally, suggestions were made to improve the prompt for user input to clarify its purpose. Overall, the code functions but needs adjustments for user interaction and formatting.
iquicck
Messages
3
Reaction score
0
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:
Physics news on Phys.org
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.
 

Similar threads

Replies
3
Views
1K
Replies
2
Views
2K
Replies
1
Views
3K
Replies
21
Views
3K
Replies
3
Views
2K
Replies
12
Views
2K
Replies
14
Views
4K
Back
Top