Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!spool.mu.edu!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c Subject: Re: Divide and C Message-ID: <639@taumet.com> Date: 28 Mar 91 16:32:02 GMT Article-I.D.: taumet.639 References: <1991Mar27.185804.7221@uunet.uu.net> Organization: Taumetric Corporation, San Diego Lines: 28 karln!karln@uunet.uu.net (Karl Nicholas) writes: > I get a number. I need to use this number to index into a > 2 dimensional array. > EX: int array[10][10]; > val = 24; > ++array[val/10][val%10]; > This is the only way I know how to do this. > My problem with it is that is requires TWO divides. Some compilers are smart enough to recognize a pattern like array[val/10][val%10] and do a single division, using the resulting quotient and remainder. Some are smart enough to recognize this pattern across statement boundaries, e.g., i = a/b; j = a%b; If your compiler is not this smart and you know that the time spent in the extra division is making your program unacceptably slow, you can try the Standard C library function div(), which takes a numerator and denominator and returns a structure containing the quotient and remainder. Use of this function might turn out to be slower than the extra divide. -- Steve Clamage, TauMetric Corp, steve@taumet.com