<?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>Omnia sunt communia &#187; programming</title>
	<atom:link href="http://www.blog.amrlima.info/archives/tag/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.blog.amrlima.info</link>
	<description>gnu/linux, cultura livre e outras divagações</description>
	<lastBuildDate>Fri, 20 Jan 2012 20:18:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Another C mathematical program</title>
		<link>http://www.blog.amrlima.info/archives/869</link>
		<comments>http://www.blog.amrlima.info/archives/869#comments</comments>
		<pubDate>Fri, 15 Jan 2010 19:28:16 +0000</pubDate>
		<dc:creator>amrlima</dc:creator>
				<category><![CDATA[Programação]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[mode]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.blog.amrlima.info/?p=869</guid>
		<description><![CDATA[Disclaimer: For the non geeky programming lovers out there, sorry for spamming you with this bunch of incomprehensible lines of text!
The first time I wrote some kind of program was back in high school with a Ti80 calculator which had some sort of BASIC programming language. After reading the manual I spent hours making small [...]]]></description>
			<content:encoded><![CDATA[<p>Disclaimer: For the non geeky programming lovers out there, sorry for spamming you with this bunch of incomprehensible lines of text!</p>
<p>The first time I wrote some kind of program was back in high school with a <a href="http://www.albion.edu/math/MBollman/TI80.jpg" target="_blank">Ti80 calculator</a> which had some sort of BASIC programming language. After reading the manual I spent hours making small mathematical programs to speedup my problem solving study. I must say the programs were not that useful to math because in the tests I had of course to show all the calculation I did&#8230; Then I forgot most things about programming&#8230; Until some years ago <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Anyways, what does this have to do with the code below? Almost nothing! The code bellow calculates the <a href="http://en.wikipedia.org/wiki/Mode_(statistics)" target="_blank">mode</a> of n numbers provided by a user. It dynamically creates an n size array and checks with value is seen more often. I still haven&#8217;t done a python version this time, if someone wan&#8217;ts to do it in python or any other language, please do! <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Here it goes.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*Create a n sized array*/</span>
<span style="color: #993333;">int</span><span style="color: #339933;">*</span> makearray<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span><span style="color: #339933;">*</span> v<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
&nbsp;
    v <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> malloc<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">*</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/*Alocates n int numbers*/</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Array elements:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #808080; font-style: italic;">/*Fill the array with user input*/</span>
        scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>v<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> v<span style="color: #339933;">;</span>    
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*Get the size of an array*/</span>
<span style="color: #993333;">int</span> arraylen<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> v<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> i<span style="color: #339933;">++;</span>
    <span style="color: #b1b100;">return</span> i<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*Count how many times a given element is repeated in an array*/</span>
<span style="color: #993333;">int</span> countelem<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> v<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> num<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">,</span> n<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> arraylen<span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span> num<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            n<span style="color: #339933;">++;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> n<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> n<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> count <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> mode<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span><span style="color: #339933;">*</span> v<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Size of the array?<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #808080; font-style: italic;">/*We don't wan't anyone creating a oversized array: DANGER'*/</span>
    <span style="color: #808080; font-style: italic;">/*So we limit it's' size. Here I randomly defined 100*/</span>
    <span style="color: #b1b100;">do</span><span style="color: #009900;">&#123;</span>
        scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>n <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    v <span style="color: #339933;">=</span> makearray<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #808080; font-style: italic;">/*Update the wich value is repeated more ofted -&gt; Mode*/</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>countelem<span style="color: #009900;">&#40;</span>v<span style="color: #339933;">,</span> v<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> count<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            count <span style="color: #339933;">=</span> countelem<span style="color: #009900;">&#40;</span>v<span style="color: #339933;">,</span> v<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/*Update the value of the maximum number of repeats*/</span>
            <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> count<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            mode <span style="color: #339933;">=</span> v<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/*Update mode*/</span>
        <span style="color: #009900;">&#125;</span> 
&nbsp;
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Mode: %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> mode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>EDIT: There was a little bug with my code, it&#8217;s fixed now <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p><a href="http://maracuja.homeip.net/">One of my colegues</a> made a python version <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> . Hope he doesn&#8217;t mind if I publish it <img src='http://www.blog.amrlima.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/bin/sh python</span>
<span style="color: #808080; font-style: italic;">#-*- coding: utf-8 -*-</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> makearray<span style="color: black;">&#40;</span>v<span style="color: black;">&#41;</span>:
  <span style="color: #483d8b;">&quot;&quot;&quot;Creates the dictionary with the elements input by the user
  &quot;&quot;&quot;</span>
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Array elements (end with empty line):&quot;</span>
  <span style="color: #ff7700;font-weight:bold;">while</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    s = <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-&gt; &quot;</span><span style="color: black;">&#41;</span>    <span style="color: #808080; font-style: italic;"># get an element</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> s:
      <span style="color: #ff7700;font-weight:bold;">break</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
      i = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> v.<span style="color: black;">has_key</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>:      <span style="color: #808080; font-style: italic;"># if already exists, increment frequency</span>
        v<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> += <span style="color: #ff4500;">1</span>
      <span style="color: #ff7700;font-weight:bold;">else</span>:                 <span style="color: #808080; font-style: italic;"># if not, create it</span>
        v<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> = <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">except</span>:                 <span style="color: #808080; font-style: italic;"># error handling</span>
       <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;not an integer&quot;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">#-----------------------------------------------------------------------</span>
<span style="color: #808080; font-style: italic;"># program</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># instead of a vector or array, we do this with a dictionary</span>
<span style="color: #808080; font-style: italic;"># that way we can keep the frequency count with each entry</span>
v = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
makearray<span style="color: black;">&#40;</span>v<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># get the mode</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>v<span style="color: black;">&#41;</span>:
  mode = <span style="color: #ff4500;">0</span>
  maxfreq = <span style="color: #ff4500;">0</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> v.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> v<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: #66cc66;">&gt;</span> maxfreq:
      mode = i
      maxfreq = v<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Mode %d, with frequency %d&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>mode, maxfreq<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">else</span>:
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Empty array&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.blog.amrlima.info/archives/869/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

