\documentclass{beamer} %include lhs2TeX.fmt \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 |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{code} #include void hanoi(int, char, char,char); int main() { int n; printf("enter the number of disks: "); scanf("%d",&n); if( n < 0 ) return 1; hanoi(n,'A','B','C'); return 0; } void hanoi(int n, char a, char b, char c) { if ( n <= 0 ) return; hanoi(n-1 , a, c, b); printf("(%d) %c -> %c\n", n, a, c); hanoi( n-1, b, a, c); } \end{code} \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{spec} int sum = 0; int i = 0; while( i < n) { i++; sum += i; } \end{spec} A nice invarient is $sum = \sum_{j=0}^i i$. \end{frame}