Re: C program doubt
"Kapil Sethi" <[email protected]> Mon, 28 Mar 2005 15:31:49 +0530
| Newsgroups | gmane.user-groups.linux.delhi.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi Arpita, I guess it is because of the very simple reason. Under DOS the long datatype uses the MSB as sign bit so 32768 actually becomes -1. I suggest u try using unsigned long i for array size greater than 32767. Kapil ----- Original Message ----- From: "arpita patra" <[email protected]> To: <Ilugd-dev-cunTk1MwBs8/[email protected]> Sent: Saturday, March 26, 2005 6:42 PM Subject: [ILUGD-Dev] C program doubt 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; } } _______________________________________________ The ILUGD-Dev mailing list Ilugd-dev-cunTk1MwBs8/[email protected] Subscribe/Unsubscribe/Suspend your list membership at: http://frodo.hserus.net/mailman/listinfo/ilugd-dev