0

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

Paroz
  • 39
  • 4
  • 1
    Compilation cannot end with a segmentation fault (unless there's a bug in the compiler itself). –  Nov 20 '22 at 15:17
  • 1
    And you should call `Output(dizi,N,aranan)`, not `Output(*dizi,N,aranan)`. –  Nov 20 '22 at 15:18
  • Why ? It doesn't give any output – Paroz Nov 20 '22 at 15:19
  • 1
    Turn up your compiler warnings. You pass incorrect types to your function. The compiler should tell you. For GCC ot clang use `-Wall -Wextra -pedantic` – Gerhardh Nov 20 '22 at 15:19
  • Thanks ı changed to Output(dizi,N,aranan) but the same – Paroz Nov 20 '22 at 15:20
  • Remove your 3 global variables. You are not using two of them and the thirs is used incorrectly. – Gerhardh Nov 20 '22 at 15:22
  • General note: You should always check return value of `scanf` to see if input was valid. Otherwise you might work with uninitialized variables. – Gerhardh Nov 20 '22 at 15:24

1 Answers1

1

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);
}
Oka
  • 23,367
  • 6
  • 42
  • 53
  • I appreciate for your effort and taking time. I really like the control expression when the value is retrieved from the user with scanf. I will try to use it from now on. I don't know how to use stderr I'll take a look at it. Thank you very much again for your answer, full of good tips.I want to analyze this code with Big -O notation. Do you know if there are any ready-made programs for this? – Paroz Nov 20 '22 at 16:11
  • `stderr` is the appropriate stream to use for errors and diagnostics. I do not have any recommendations for programs that can analyze this code. This program is *O(n)*, however, as linear search and individually printing or populating an array are all *O(n)*. – Oka Nov 20 '22 at 16:21