<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Shape of Code &#187; distance</title>
	<atom:link href="http://shape-of-code.coding-guidelines.com/tag/distance/feed/" rel="self" type="application/rss+xml" />
	<link>http://shape-of-code.coding-guidelines.com</link>
	<description></description>
	<lastBuildDate>Sun, 12 Feb 2012 20:42:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Average distance between two fields</title>
		<link>http://shape-of-code.coding-guidelines.com/2008/12/02/average-distance-between-two-fields/</link>
		<comments>http://shape-of-code.coding-guidelines.com/2008/12/02/average-distance-between-two-fields/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 00:39:47 +0000</pubDate>
		<dc:creator>Derek-Jones</dc:creator>
				<category><![CDATA[Datatypes]]></category>
		<category><![CDATA[average]]></category>
		<category><![CDATA[datatype]]></category>
		<category><![CDATA[distance]]></category>

		<guid isPermaLink="false">http://shape-of-code.coding-guidelines.com/?p=12</guid>
		<description><![CDATA[If I randomly pick two fields from an aggregate type definition containing N fields what will be the average distance between them (adjacent fields have distance 1, if separated by one field they have distance 2, separated by two fields they have distance 3 and so on)? For example, a struct containing five fields has [...]]]></description>
			<content:encoded><![CDATA[<p>If I randomly pick two fields  from an aggregate type definition containing N fields what will be the average distance between them (adjacent fields have distance 1, if separated by one field they have distance 2, separated by two fields they have distance 3 and so on)?</p>
<p>For example, a <code>struct</code> containing five fields has four field pairs having distance 1 from each other, three distance 2, two distance 2, and one field pair having distance 4; the average is 2.</p>
<p>The surprising answer, to me at least, is (N+1)/3.</p>
<p><strong>Proof</strong>: The average distance can be obtained by summing the distances between all possible field pairs and dividing this value by the number of possible different pairs.</p>
<pre>                  Distance 1  2  3  4  5  6
Number of fields
            4              3  2  1
            5              4  3  2  1
            6              5  4  3  2  1
            7              6  5  4  3  2  1</pre>
<p>The above table shows the pattern that occurs as the number of fields in a definition increases.</p>
<p>In the case of a definition containing five fields the sum of the distances of all field pairs is: (4*1 + 3*2 + 2*3 + 1*4) and the number of different pairs is: (4+3+2+1). Dividing these two values gives the average distance between two randomly chosen fields, e.g., 2.</p>
<p>Summing the distance over every field pair for a definition containing 3, 4, 5, 6, 7, 8, &#8230; fields gives the sequence: 1, 4, 10, 20, 35, 56, &#8230; This is sequence <a href="http://www.research.att.com/~njas/sequences/A000292">A000292</a> in the On-Line Encyclopedia of Integer sequences and is given by the formula n*(n+1)*(n+2)/6 (where n = N − 1, i.e., the number of fields minus 1).</p>
<p>Summing the number of different field pairs for definitions containing increasing numbers of fields gives the sequence: 1, 3, 6, 10, 15, 21, 28, &#8230; This is sequence <a href="http://www.research.att.com/~njas/sequences/A000217">A000217</a> and is given by the formula n*(n + 1)/2.</p>
<p>Dividing these two formula and simplifying yields (N + 1)/3.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fshape-of-code.coding-guidelines.com%2F2008%2F12%2F02%2Faverage-distance-between-two-fields%2F&amp;title=Average%20distance%20between%20two%20fields" id="wpa2a_2"><img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://shape-of-code.coding-guidelines.com/2008/12/02/average-distance-between-two-fields/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

