home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk1.iso
/
altsrc
/
articles
/
11361
< prev
next >
Wrap
Text File
|
1994-10-18
|
2KB
|
77 lines
Path: wupost!spool.mu.edu!howland.reston.ans.net!pipex!sunic!erinews.ericsson.se!etxmesa
From: etxmesa@eos.ericsson.se (Michael Salmon)
Newsgroups: alt.sources
Subject: Re: Ugly code.
Date: 18 Oct 1994 12:01:30 GMT
Organization: Ericsson Telecom AB
Lines: 63
Distribution: world
Message-ID: <380deq$bdr@erinews.ericsson.se>
References: <CxtEys.DKr@cs.dal.ca>
Reply-To: etxmesa@eos.ericsson.se (Michael Salmon)
NNTP-Posting-Host: eos6c02.ericsson.se
In article <CxtEys.DKr@cs.dal.ca>
franklin@ug.cs.dal.ca (Steve Franklin) writes:
|>
|> This is some code for a solid-state assignment which computes the number
|> of neighbors from a central atom that are sqrt(1), sqrt(2), etc... away
|> from the central atom. Anyone able to make itany more compact or ugly?
|> It needs the math library, and it's ansi C.
|>
|> Steve Franklin
|>
|> #include<stdio.h>
|> #include<math.h>
|> typedef float f;typedef int Y;f p(f a,f b,f c){return sqrt(a*a+b*b+c*c);}
|> f a,b,c;main(){Y i,j,k,X[9],n,V[9]={1,2,3,4,5,6,8,9,12};for(n=0;n<9;n++)
|> {X[n]=0;}for(i=-2;i<3;i++)for(j=-2;j<3;j++)for(k=-2;k<3;k++){{{a=(f)i;b=(f)j;
|> c=(f)k;for(n=0;n<9;n++){if(p(a,b,c)==(f)sqrt(V[n])){X[n]++;}}}}}for(n=0;n<9;
|> n++){printf("N=%-2d #=%d\n",V[n],X[n]);}}
This is my suggestion:
#include<stdio.h>
int sqrP(int a,int b,int c)
{
return (a*a+b*b+c*c);
}
main()
{
int i,j,k,l,X[9],n,V[9]={1,2,3,4,5,6,8,9,12};
for(n=0;n<9;n++) {
X[n]=0;
}
for(i=-2;i<3;i++)
for(j=-2;j<3;j++)
for(k=-2;k<3;k++){
for(n=0;n<9;n++){
l=sqrP(i,j,k);
if(l==V[n]){
X[n]++;
break;
}
}
}
for(n=0;n<9; n++){
printf("N=%-2d #=%d\n",V[n],X[n]);
}
}
It should be faster with a lot of points but for only a few it isn't
that noticeable.
--
Michael Salmon
#include <standard.disclaimer>
#include <witty.saying>
#include <fancy.pseudo.graphics>
Ericsson Telecom AB
Stockholm