\documentclass{beamer}
%
%
\makeatletter
\@ifundefined{lhs2tex.lhs2tex.sty.read}%
  {\@namedef{lhs2tex.lhs2tex.sty.read}{}%
   \newcommand\SkipToFmtEnd{}%
   \newcommand\EndFmtInput{}%
   \long\def\SkipToFmtEnd#1\EndFmtInput{}%
  }\SkipToFmtEnd

\newcommand\ReadOnlyOnce[1]{\@ifundefined{#1}{\@namedef{#1}{}}\SkipToFmtEnd}
\usepackage{amstext}
\usepackage{amssymb}
\usepackage{stmaryrd}
\DeclareFontFamily{OT1}{cmtex}{}
\DeclareFontShape{OT1}{cmtex}{m}{n}
  {<5><6><7><8>cmtex8
   <9>cmtex9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmtex10}{}
\DeclareFontShape{OT1}{cmtex}{m}{it}
  {<-> ssub * cmtt/m/it}{}
\newcommand{\texfamily}{\fontfamily{cmtex}\selectfont}
\DeclareFontShape{OT1}{cmtt}{bx}{n}
  {<5><6><7><8>cmtt8
   <9>cmbtt9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmbtt10}{}
\DeclareFontShape{OT1}{cmtex}{bx}{n}
  {<-> ssub * cmtt/bx/n}{}
\newcommand{\tex}[1]{\text{\texfamily#1}}	% NEU

\newcommand{\Sp}{\hskip.33334em\relax}
\newcommand{\NB}{\textbf{NB}}
\newcommand{\Todo}[1]{$\langle$\textbf{To do:}~#1$\rangle$}

\EndFmtInput
\makeatother
%
\author{Piyush P Kurur\\
  Office no: 224\\
  Dept. of Comp. Sci. and Engg.\\
  IIT Kanpur}


\newcommand{\Token}[2][]{\node(#2#1)[token]{#2};}
\newcommand{\Symbol}[2][]{\node(#2#1)[symbol]{#2};}
\newcommand{\Point}[1]{\node(#1)[point]{#1};}

\usepackage{tikz}
\usetikzlibrary{positioning,shapes,chains,fit}
\usetikzlibrary{shapes.symbols}
\usetikzlibrary{matrix}
\usetikzlibrary{backgrounds}
\usetikzlibrary{shapes.geometric}

\usepackage{pgfkeys}
\usepackage{multicol}
\pgfkeys %
    {%
      /tikz/coin/.style={
        rectangle,
        minimum height=2.5mm,
        minimum width=1cm,
        draw=black,
        fill=black!20,
        rounded corners
      },
      /tikz/towercolor/.style={
        fill=black!90
      },
      /tikz/towerbase/.style={
        trapezium,
        trapezium angle=45,
        trapezium stretches=true,
        towercolor,
        minimum width=7.5mm,
        minimum height=2.5mm,
      },
      /tikz/tower/.style={
        rectangle,
        rounded corners,
        towercolor,
        minimum width=2.5mm,
        minimum height=17.5mm,
      }
    }

\title{Fundamentals of Computing: Lecture 8}
\date{August 12, 2009}
\begin{document}
\begin{frame}
\maketitle
\end{frame}

\begin{frame}
  \frametitle{Summary of last class}
  \pause
  \begin{itemize}
  \item We looked at functions.
    \pause
  \item For C the argument passing scheme is call by value
    \pause
  \item Functions can be recursive
    \pause
  \item The \text{\tt main} function and its return type
    \pause
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Recursion: Tower of Hanoi}
  \begin{multicols}{2}
    \begin{tikzpicture}
      \pgfdeclarelayer{background}
      \pgfsetlayers{background,main}
      \node[towerbase] (baseA) at (0,0){};
      \node[towerbase] (baseB) at (1.5cm,-1.5cm){};
      \node[towerbase] (baseC) at (3cm,0){};
      \node[tower,above] (towerA) at (baseA.south){};
      \node[tower,above] (towerB) at (baseB.south){};
      \node[tower,above] (towerC) at (baseC.south){};
      \node[above] at (towerA.north) {$A$}; 
      \node[above] at (towerB.north) {$B$}; 
      \node[above] at (towerC.north) {$C$}; 
      
      \begin{scope}[every node/.style={coin,above}]
        \node[minimum width=1.75cm] (d) at (baseA.north){};
        \node[coin,minimum width=1.5cm, above] (c) at (d.north) {};
        \node[coin,minimum width=1.25cm] (b) at (c.north) {};
        \node[coin,minimum width=1cm] (a) at (b.north) {};
      \end{scope}
      \node[above] at (towerA.north) {$A$}; 
      \begin{pgfonlayer}{background}
        \node[rectangle, fill=yellow!40, fit=(towerA) (baseB) (baseC) (d)] {};
      \end{pgfonlayer}
    \end{tikzpicture}
    \newpage
    \begin{tikzpicture}
      \pgfdeclarelayer{background}
      \pgfsetlayers{background,main}
      \node[towerbase] (baseA) at (0,0){};
      \node[towerbase] (baseB) at (1.5cm,-1.5cm){};
      \node[towerbase] (baseC) at (3cm,0){};
      \node[tower,above] (towerA) at (baseA.south){};
      \node[tower,above] (towerB) at (baseB.south){};
      \node[tower,above] (towerC) at (baseC.south){};
      \node[above] at (towerA.north) {$A$}; 
      \node[above] at (towerB.north) {$B$}; 
      \node[above] at (towerC.north) {$C$}; 
      \begin{scope}[every node/.style={coin,above}]
        \node[minimum width=1.75cm] (d) at (baseC.north){};
        \node[coin,minimum width=1.5cm, above] (c) at (d.north) {};
        \node[coin,minimum width=1.25cm] (b) at (c.north) {};
        \node[coin,minimum width=1cm] (a) at (b.north) {};
      \end{scope}
      \begin{pgfonlayer}{background}
        \node[rectangle, fill=blue!40, fit=(baseA) (baseB) (towerC) (d)] {};
      \end{pgfonlayer}
    \end{tikzpicture}
  \end{multicols}


  %% \begin{multicols}{2}
  %%   \begin{tikzpicture}
  %%     \pgfdeclarelayer{background}
  %%     \pgfsetlayers{background,main}
  %%     \node[towerbase] (baseA) at (0,0){};
  %%     \node[towerbase] (baseB) at (1.5cm,-1.5cm){};
  %%     \node[towerbase] (baseC) at (3cm,0){};
  %%     \node[tower,above] (towerA) at (baseA.south){};
  %%     \node[tower,above] (towerB) at (baseB.south){};
  %%     \node[tower,above] (towerC) at (baseC.south){};
  %%     \node[above] at (towerA.north) {$A$}; 
  %%     \node[above] at (towerB.north) {$B$}; 
  %%     \node[above] at (towerC.north) {$C$}; 
      
  %%     \begin{scope}[every node/.style={coin,above}]
  %%       \node[minimum width=1.75cm] (d) at (a.north){};
  %%       \node[coin,minimum width=1cm] (a1) at (baseA.north) {};
  %%     \end{scope}
  %%     \begin{pgfonlayer}{background}
  %%       \node[rectangle, fill=green!40, fit=(towerA) (baseB) (baseC) (d)] {};
  %%     \end{pgfonlayer}
  %%   \end{tikzpicture}

  %%   \newpage
  %%   \begin{tikzpicture}
  %%     \pgfdeclarelayer{background}
  %%     \pgfsetlayers{background,main}
  %%     \node[towerbase] (baseA) at (0,0){};
  %%     \node[towerbase] (baseB) at (1.5cm,-1.5cm){};
  %%     \node[towerbase] (baseC) at (3cm,0){};
  %%     \node[tower,above] (towerA) at (baseA.south){};
  %%     \node[tower,above] (towerB) at (baseB.south){};
  %%     \node[tower,above] (towerC) at (baseC.south){};
  %%     \node[above] at (towerA.north) {$A$}; 
  %%     \node[above] at (towerB.north) {$B$}; 
  %%     \node[above] at (towerC.north) {$C$}; 
  %%     \begin{scope}[every node/.style={coin,above}]
  %%       \node[minimum width=1cm] (d) at (a1.north){};
  %%       \node[coin,minimum width=1.75cm] (a1) at (baseA.north) {};
  %%     \end{scope}
  %%     \begin{pgfonlayer}{background}
  %%       \node[rectangle, fill=red!40, fit=(baseA) (baseB) (towerC) (d)] {};
  %%     \end{pgfonlayer}
  %%   \end{tikzpicture}
  %% \end{multicols}

\end{frame}




\begin{frame}
  \begin{multicols}{2}
  \begin{tikzpicture}
    \pgfdeclarelayer{background}
    \pgfsetlayers{background,main}
    \node[towerbase] (baseA) at (0,0){};
    \node[towerbase] (baseB) at (1.5cm,-1.5cm){};
    \node[towerbase] (baseC) at (3cm,0){};
    \node[tower,above] (towerA) at (baseA.south){};
    \node[tower,above] (towerB) at (baseB.south){};
    \node[tower,above] (towerC) at (baseC.south){};
    \node[above] (A) at (towerA.north) {$A$}; 
    \node[above] (B) at (towerB.north) {$B$}; 
    \node[above] (C) at (towerC.north) {$C$}; 
    
    \begin{scope}[every node/.style={coin,above}]
      \node[minimum width=1.75cm] (d) at (baseA.north){};
      \node[coin,minimum width=1.5cm, above] (c) at (d.north) {};
      \node[coin,minimum width=1.25cm] (b) at (c.north) {};
      \node[coin,minimum width=1cm] (a) at (b.north) {};
    \end{scope}
    \node[above] at (towerA.north) {$A$}; 
    \begin{pgfonlayer}{background}
      \node[rectangle, fill=yellow!30, fit=(towerA) (baseB) (baseC) (d)] {};
      \uncover<2->{
        \node[rectangle, rounded corners, fill=red!50, fit=(a)(b)(c)](abc){};
        \path (towerA.north) edge[->,bend left] (towerB.north);
      }
    \end{pgfonlayer}
  \end{tikzpicture}
  \newpage

  \uncover<3->{
    \begin{tikzpicture}
      \pgfdeclarelayer{background}
      \pgfsetlayers{background,main}
      \node[towerbase] (baseA) at (0,0){};
      \node[towerbase] (baseB) at (1.5cm,-1.5cm){};
      \node[towerbase] (baseC) at (3cm,0){};
      \node[tower,above] (towerA) at (baseA.south){};
      \node[tower,above] (towerB) at (baseB.south){};
      \node[tower,above] (towerC) at (baseC.south){};
      \node[above] at (towerA.north) {$A$}; 
      \node[above] at (towerB.north) {$B$}; 
      \node[above] at (towerC.north) {$C$}; 
      \begin{scope}[every node/.style={coin,above}]
        \node[minimum width=1.75cm] (d) at (baseA.north){};
        \node[coin,minimum width=1.5cm, above] (c) at (baseB.north) {};
        \node[coin,minimum width=1.25cm] (b) at (c.north) {};
        \node[coin,minimum width=1cm] (a) at (b.north) {};
      \end{scope}
      \begin{pgfonlayer}{background}
        \node[rectangle, fill=yellow!30, fit=(baseA) (baseB) (towerC) (d)] {};
        \uncover<4->{
          \node[rectangle, rounded corners, fill=red!50, fit=(d)](abc){};
          \path (towerA.north) edge[->,bend left] (towerC.north);
        }   
      \end{pgfonlayer}
    \end{tikzpicture}
  }
  \end{multicols}
  \begin{multicols}{2}
    \uncover<5->{
      \begin{tikzpicture}
        \pgfdeclarelayer{background}
        \pgfsetlayers{background,main}
        \node[towerbase] (baseA) at (0,0){};
        \node[towerbase] (baseB) at (1.5cm,-1.5cm){};
        \node[towerbase] (baseC) at (3cm,0){};
        \node[tower,above] (towerA) at (baseA.south){};
        \node[tower,above] (towerB) at (baseB.south){};
        \node[tower,above] (towerC) at (baseC.south){};
        \node[above] at (towerA.north) {$A$}; 
        \node[above] at (towerB.north) {$B$}; 
        \node[above] at (towerC.north) {$C$}; 
        \begin{scope}[every node/.style={coin,above}]
          \node[minimum width=1.75cm] (d) at (baseC.north){};
          \node[coin,minimum width=1.5cm, above] (c) at (baseB.north) {};
          \node[coin,minimum width=1.25cm] (b) at (c.north) {};
          \node[coin,minimum width=1cm] (a) at (b.north) {};
        \end{scope}
        \begin{pgfonlayer}{background}
          \node[rectangle, fill=yellow!30, fit=(baseA) (baseB) (towerC) (d)] {};
          \uncover<6->{
            \node[rectangle, rounded corners, fill=red!50, fit=(a)(b)(c)](abc){};
            \draw[->,out=90,in=180] (B.north) to (towerC.north);
          }   
        \end{pgfonlayer}
      \end{tikzpicture}
    }
    \newpage
    \vspace{1cm}
    \uncover<7->{
      \begin{tikzpicture}
        \pgfdeclarelayer{background}
        \pgfsetlayers{background,main}
        \node[towerbase] (baseA) at (0,0){};
        \node[towerbase] (baseB) at (1.5cm,-1.5cm){};
        \node[towerbase] (baseC) at (3cm,0){};
        \node[tower,above] (towerA) at (baseA.south){};
        \node[tower,above] (towerB) at (baseB.south){};
        \node[tower,above] (towerC) at (baseC.south){};
        \node[above] at (towerA.north) {$A$}; 
        \node[above] at (towerB.north) {$B$}; 
        \node[above] at (towerC.north) {$C$}; 
        \begin{scope}[every node/.style={coin,above}]
          \node[minimum width=1.75cm] (d) at (baseC.north){};
          \node[coin,minimum width=1.5cm, above] (c) at (d.north) {};
          \node[coin,minimum width=1.25cm] (b) at (c.north) {};
          \node[coin,minimum width=1cm] (a) at (b.north) {};
        \end{scope}
        \begin{pgfonlayer}{background}
          \node[rectangle, fill=blue!40, fit=(baseA) (baseB) (towerC) (d)] {};
        \end{pgfonlayer}
      \end{tikzpicture}
    }
  \end{multicols}
\end{frame}

\begin{frame}
  \begin{tabbing}\tt
~\char35{}include~\char60{}stdio\char46{}h\char62{}\\
\tt ~~~~~\\
\tt ~void~hanoi\char40{}int\char44{}~char\char44{}~char\char44{}char\char41{}\char59{}\\
\tt ~int~main\char40{}\char41{}\\
\tt ~\char123{}\\
\tt ~~~~~~~~~int~n\char59{}\\
\tt ~~~~~~~~~printf\char40{}\char34{}enter~the~number~of~disks\char58{}~\char34{}\char41{}\char59{}~\\
\tt ~~~~~~~~~scanf\char40{}\char34{}\char37{}d\char34{}\char44{}\char38{}n\char41{}\char59{}\\
\tt ~~~~~~~~~if\char40{}~n~\char60{}~0~\char41{}~return~1\char59{}\\
\tt ~~~~~~~~~hanoi\char40{}n\char44{}\char39{}A\char39{}\char44{}\char39{}B\char39{}\char44{}\char39{}C\char39{}\char41{}\char59{}\\
\tt ~~~~~~~~~return~0\char59{}\\
\tt ~\char125{}\\
\tt ~\\
\tt ~void~hanoi\char40{}int~n\char44{}~char~a\char44{}~char~b\char44{}~char~c\char41{}\\
\tt ~\char123{}\\
\tt ~~~~~~~~~if~\char40{}~n~\char60{}\char61{}~0~\char41{}~return\char59{}\\
\tt ~~~~~~~~~hanoi\char40{}n\char45{}1~\char44{}~a\char44{}~c\char44{}~b\char41{}\char59{}\\
\tt ~~~~~~~~~printf\char40{}\char34{}\char40{}\char37{}d\char41{}~\char37{}c~\char45{}\char62{}~\char37{}c\char92{}n\char34{}\char44{}~n\char44{}~a\char44{}~c\char41{}\char59{}\\
\tt ~~~~~~~~~hanoi\char40{}~n\char45{}1\char44{}~b\char44{}~a\char44{}~c\char41{}\char59{}\\
\tt ~\char125{}
\end{tabbing}
\end{frame}     

\end{document}





\begin{frame}
  \frametitle{How to design loops?}
  
  \begin{definition}
    An invarient of a loop is a condition that is
    true always during the execution of the loop.
  \end{definition}
  
  Eg.
  
  \begin{tabbing}\tt
~~~~~int~sum~\char61{}~0\char59{}\\
\tt ~~~~~int~i~~~\char61{}~0\char59{}\\
\tt ~~~~~while\char40{}~i~\char60{}~n\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~i\char43{}\char43{}\char59{}\\
\tt ~~~~~~~sum~\char43{}\char61{}~i\char59{}\\
\tt ~~~~~\char125{}
\end{tabbing}

  A nice invarient is $sum = \sum_{j=0}^i i$.
  
\end{frame}



