delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/08/09:09:18

Xref: news2.mv.net comp.lang.c:66865 comp.os.msdos.djgpp:3561
From: Ronald Fischer <ronald DOT fischer AT uebemc DOT siemens DOT de>
Newsgroups: comp.lang.c,comp.os.msdos.djgpp
Subject: Re: Use of random
Date: 8 May 1996 09:44:23 GMT
Organization: Siemens AG,(Hofmannstr) Munich-Germany-Europe.
Lines: 94
Message-ID: <4mpqdn$9li@moody.mchh.siemens.de>
References: <4mikhp$pa5 AT frodo DOT smartlink DOT net>
NNTP-Posting-Host: themse.mchh.siemens.de
Mime-Version: 1.0
To: nmarrone AT smartlink DOT net
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

This is a multi-part message in MIME format.

---------------------------------7102734724240
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=us-ascii

The only problem I see with your program is in the malloc:

onesp = (int *)malloc(NUM * 2)

Unless it happens that sizeof(int)==2, this won't allocate enough memory and
strange results will occur. 

>but it gives me #s far larger than integers 

This is not possible with your program. Even if rand would return a long
instead of an int (which it is not allowed to), you are storing it into an int,
and an
int can't hold numbers larger than an int (did you invent a bit-squeeze
algorithm which stores two bits into one?). Also, your printf statement 
prints the ints correctly as int, so I wonder how you came to your strange
conclusion.

Ronald

---------------------------------7102734724240
Content-Transfer-Encoding: 7bit
Content-Type: text/plain

From: nmarrone AT smartlink DOT net (Nicholas Marrone)
Newsgroups: comp.lang.c,comp.os.msdos.djgpp
Subject: Use of random
Date: Sun, 05 May 1996 15:51:12 GMT
Organization: SmartLink.net Premier ISP 805-294-1273
Message-ID: <4mikhp$pa5 AT frodo DOT smartlink DOT net>

Hullo,
	To test out my radix_bit_sort function, I've been trying to generate a
list of random numbers to sort, and then sort them. The list is rather
long, 4000, and I'd like it to be ints, but I can' seem to get it! The
program compiles and runs without error, but it gives me #s far larger
than integers when I print out some results. I'm using DJGPP v2.
Here's some of the code...


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUM 2000

int main()
{

   int ctr;
   int *origp;
   int *onesp, *zerop;
   int ticks;       /* for use with the clock in randomizing shtuff */

   if ( !(origp = (int *)malloc(NUM * 2)) ||
	!(onesp = (int *)malloc(NUM * 2)) ||
	!(zerop = (int *)malloc(NUM * 2)) )
      {
	 printf("Not enough memory to run this program!!!");
      }

   clock();             /* Initialize the clock */
   (int *)ticks = clock();
   srand(ticks);

   for (ctr = 0; ctr < NUM; ctr++) {
      origp[ctr] = rand();
   }

   radix_bit_sort(origp, onesp, zerop, NUM);


   for (ctr = 0; ctr < 20; ctr++)
      printf("%d\n", origp[ctr]);

   free(origp);
   free(onesp);
   free(zerop);

   return 0;
}

I really need some serious help. Thanks.

	nicholas



---------------------------------7102734724240--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019