This algorithm is a linear search algorithm that finds the desired value. But when I compile this code, gives the segmentation fault in if(dizi[i]==aranan) this line. How can I solve this ?
#include <stdio.h>
int N,i,aranan;
int ArrayBastir(int *dizi,int N)
{
printf("My numbers\n");
for(int i =0; i<N;i )
{
printf("%d\n", dizi[i]);
}
}
int findValue()
{
printf("The value you want to search");
scanf("%d",&aranan);
}
int Output(int *dizi,int N,int aranan)
{
for(int i =0; i<N;i )
{
if(dizi[i]==aranan)
{
printf("%d number %d. found in queue \n", aranan,i 1);
}
}
}
int main() {
int N;
int aranan;
printf("Please enter how many numbers you want to enter");
scanf("%d", &N);
int dizi[N];
for(int i =0; i<N;i )
{
scanf("%d", &dizi[i]);
}
ArrayBastir( dizi, N);
findValue(aranan);
Output(*dizi,N,aranan);
return 1;
}
Linear Search algorithm
CodePudding user response:
You have two objects defined as:
int aranan;
Once is defined at file scope (a global), and one is defined within the scope of main
.
When findValue(aranan)
is called, a copy of the uninitialized value of aranan
within the scope of main
is passed to findValue
. findValue
is lacking a prototype, having not declared its arguments, so this is ignored.
findValue
scans a value into the file scope aranan
, but when Output(*dizi, N, aranan)
is called it uses the value of aranan
defined within main
. aranan
within main
was never initialized, and thus this causes Output
to search for an indeterminate value.
Additionally, *dizi
is an int
, when Output
expects an int *
as its first argument.
ArrayBastir
and Output
are also defined as each returning an int
, which they do not do.
You should not ignore the return value of scanf
, as it indicates the number of successful conversions, or a negative value on failure (EOF
). In a program this small, you can get away with writing a simple wrapper function for reading integers, that just exits the program if the user enters something invalid.
main
returning nonzero generally indicates your program failed. main
is special - it is the only non-void function where you can omit a return statement. If main
reaches the end of execution without an explicit return
, it is treated as though it returned 0
.
The issues above can be mitigated by avoiding the use of global variables, and by turning up your compilers warning level to catch obvious type mismatches.
For GCC or Clang, use -Wall -Wextra
, and possibly -Werror
.
For MSVC, use /Wall
, and possibly /Wx
.
Minimally refactored:
#include <stdio.h>
#include <stdlib.h>
int get_int(void)
{
int x;
if (1 != scanf("%d", &x)) {
fprintf(stderr, "Invalid input.\n");
exit(EXIT_FAILURE);
}
return x;
}
void ArrayBastir(int *dizi, int N)
{
printf("My numbers\n");
for (int i = 0; i < N; i ) {
printf("%d\n", dizi[i]);
}
}
void Output(int *dizi, int N, int aranan)
{
for (int i = 0; i < N; i ) {
if (dizi[i] == aranan) {
printf("Found <%d> at position %d.\n", aranan, i);
}
}
}
int main(void)
{
printf("Please enter how many numbers you want to enter: ");
int N = get_int();
int dizi[N];
for (int i = 0; i < N; i ) {
dizi[i] = get_int();
}
ArrayBastir(dizi, N);
printf("Enter the value you want to search for: ");
int aranan = get_int();
Output(dizi, N, aranan);
}