-3

I am getting an error Abnormal program termination when I execute this code.

The objective the program is read array of numbers from file list.txt and perform recursive binary and recursive linear search on that loaded array.

Here is my code:

#include <stdio.h>
#include <conio.h>

void menu();

int a[30000],n;

void main()
{
    FILE *fp;
    int i, ch;

    fp = fopen("list.txt", "r");
    if(fp == NULL)
    {
        printf("\nCant read\n");
        exit(0);
    }

    for(i = 0; i < n; i++)
        fscanf(fp, "%d", &a[i]);

    fclose(fp);

    for(i = 0; i < n; i++)
        printf(" %d ", a[i]);

    menu();

    scanf("%d", &ch);
    if(ch == 1)
    {
        printf("ch1\n");
    }
    else if(ch == 2)
    {
        printf("ch2\n");
    }
    else
    {
        exit(1);
    }
}//end main



void menu()
{
    printf("\nEnter the number of elements in array\n");
    scanf("%d", &n);
    printf("\n1.Linear Search\n2.Binary Search\n3.Exit\nEnter your choice\n");
}

I have the logic for my choice 1 and 2. I need to know whats wrong with my above code. Please help me out in this

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • 2
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. – too honest for this site Sep 11 '15 at 14:02
  • And format/indent your code properly. This is a mess. – too honest for this site Sep 11 '15 at 14:02
  • 2
    You don't set `n` until you call `menu`, so before that call `n` will be zero, and nothing will be read into the array. – Some programmer dude Sep 11 '15 at 14:05
  • And as I remember, Turbo C do have a debugger, that you can use to, for example, step through the code line by line, and that will halt on crashes. – Some programmer dude Sep 11 '15 at 14:07
  • For starters call `menu` before the loop . – ameyCU Sep 11 '15 at 14:12
  • 2
    Are you sure this code produces an "Abnormal program termination" error? I don't see anything that would cause that. Please post an [mcve]. BTW, those loops won't execute as @JoachimPileborg has pointed out because `n` is 0 at those points. It seems that you need to move `menu()` *before* everything, not *after* everything. – Spikatrix Sep 11 '15 at 14:12
  • Turbo C 3.0 was released in 1992. Can we be sure `n` is initialised to 0? – JeremyP Sep 11 '15 at 15:18
  • Also, `int a[30000]` is 120,000 bytes if `int` is 32 bit. This is somewhat larger than a segment in the Intel 8086 architecture. – JeremyP Sep 11 '15 at 15:21
  • dear @JoachimPileborg i tried what you said but no use. Please try to help me. Or do I need to reduce the array size? – Deenath Kumar Sep 11 '15 at 19:23
  • and CoolGuy I prototyped menu function before main() so i guess that would not be an issue.. – Deenath Kumar Sep 11 '15 at 19:28
  • @JeremyP Remember that DOS is a 16-bit environment, where "words" (including the `int` type IIRC) are 16 bits, But still you have hit the nail on the head probably, the OP is using 60000 bytes of only 65536 available just for that array. – Some programmer dude Sep 12 '15 at 04:34

1 Answers1

1

Turbo C! That brings back some memories, but I don’t have my copy any more. Anyway, you’re using n before you initialize it in menu(). (As the commenters pointed out.) You might also consider allocating your array with malloc() instead of a fixed size.

Borland Turbo C came with Turbo Debugger, and in fact I think Borland gave it away for free for a while, so running in that can give you an idea where the abnormal termination happened and a stack trace you can use to check the variables that caused it. Another good practice is to put in assert() calls to make sure your assumptions about what’s in your variables are true.

Davislor
  • 14,674
  • 2
  • 34
  • 49