C program doubt

"arpita patra" <[email protected]> 26 Mar 2005 13:12:41 -0000
Newsgroups gmane.user-groups.linux.delhi.devel
Message-ID <[email protected]>
  
The problem with this C code below is it doesn't work for array sizes greater than 32767 despite of using far pointer, compiling it in huge compiler model and running the code outside TC editor in the dos shell.

/*PROGRAM TO SORT NUMBERS USING INSERTION SORT*/
#include <stdio.h>
#include<stdlib.h>
#include <alloc.h>
#include<time.h>
#define SIZE 50000

void isort(int far *);

void main()
    {
    int far *fptr;
    FILE *fp,*fpr,*fpw;
    int x;
    long i;
    clock_t start, end;
    clrscr();

    /* allocate memory for the far pointer */
    fptr = (int far *) farmalloc(SIZE*sizeof(int));
    if(fptr==NULL)
      {
      printf("\nnot allocated\n");
      getch();
      exit(1);
      }

    fp=fopen("c:/randfar.txt","w");
    if(fp==NULL)
      {
      printf("\nCOULDN'T OPEN FILE FOR WRITING RANDOM NUMBERS\n");
      getch();
      exit(1);
      }
    randomize();
    for(i=0;i<SIZE;i++)
       {
       x=rand();             //Generate random numbers
       fprintf(fp,"%d\n",x);
       }
    fclose(fp);

    fpr=fopen("c:/randfar.txt","r");
    if(fpr==NULL)
      {
      printf("COULDN'T OPEN FILE FOR READING\n");
      getch();
      exit(1);
      }
    for(i=0;i<SIZE;i++)
       fscanf(fpr,"%d\n",&fptr[i]);
    fclose(fpr);

    start=clock();
    isort(fptr);
    end=clock();
    printf("\n\nTHE TIME TAKEN FOR INSERTION SORT IS: %f\n", (end-start)/CLK_TCK);

    fpw=fopen("c:/inssort.txt","w");
    if(fpw==NULL)
      {
      printf("\nCOULDN'T OPEN FILE FOR WRITING");
      fclose(fpw);
      getch();
      exit(1);
      }
    for(i=0;i<SIZE;i++)
       fprintf(fpw,"%d\n",fptr[i]);
    printf("\nTHE SORTED NUMBERS ARE WRITTEN IN THE FILE 'inssort.txt'\n");
    fclose(fpw);

    /* free the memory */
    farfree(fptr);
    getch();
    }

void isort(int far *fptr)
    {
    long x,i,j;
    int temp;

    for(j=1;j<SIZE;j++)
       {
       temp=fptr[j];
       i=j-1;
       while(i>=0 && fptr[i]>temp)
	    {
	    fptr[i+1]=fptr[i];
	    i--;
	    }
       fptr[i+1]=temp;
       }
    }