\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{multicol}

\usepackage{algorithm2e}
\usepackage{pgfkeys}
\usepackage{multicol}
\pgfkeys %
    {%
      /tikz/box/.style={ 
        shape=rectangle,%
        minimum size=2em,%
        top color=white,
        bottom color=red!50!black!50,
        inner sep=0pt},
      /tikz/garbage/.style={%
        starburst,%
        fill=yellow,%
        draw=red
      }
    }


\title{Fundamentals of Computing: Lecture 17}
\date{September 7, 2009}
\begin{document}
\begin{frame}
  \maketitle
\end{frame}

\begin{frame}
  \frametitle{Summary of the previous lecture}

  \begin{itemize}
    \item Arrays are passed to functions as pointers.
    \item A function that expects an array of type \text{\tt T} should
      have as argument type \text{\tt T~\char42{}}. 
    \item There is no way a function can know the lenght of the array
      passed unless one of the argument is itself the length.  This is
      unlike arrays in Java.
  \end{itemize} 
    
\end{frame}

\begin{frame}
  \frametitle{Pointers to arrays and array of pointers}
  \begin{tabbing}\tt
~~~~~int~a\char91{}2\char93{}\char91{}3\char93{}\char44{}~\char42{}b\char91{}3\char93{}\char44{}~\char40{}\char42{}c\char41{}\char91{}3\char93{}\char59{}
\end{tabbing}

  \pause
  \begin{itemize}
    \item \text{\tt a} is a 2 dimensional array integer.
    \item \text{\tt b} is an array 3 of pointers to integers.
    \item \text{\tt c} is a pointer to an array of 3 integers.
  \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Two dimensional arrays}
  \begin{itemize}
  \item A two dimensional array is declared as \text{\tt T~a\char91{}2\char93{}\char91{}3\char93{}}.
  \uncover<2->{
  \item The array is row major, i.e. the second index moves faster.
  }
  \uncover<4->{
  \item The expression \text{\tt a} is of type pointer to array of 3 ints and
    therefore \text{\tt a} points to \text{\tt a\char91{}0\char93{}\char91{}0\char93{}} and \text{\tt a\char43{}1} points to a[1][0].
  }
  \end{itemize}

 
  \begin{tikzpicture}
    \begin{scope}
      [every node/.style={box,draw=black}, node distance=0pt];
      \node (a00) {$a_{0,0}$};
      \node [right=of a00] (a01) {$a_{0,1}$};
      \node [right=of a01] (a02) {$a_{0,2}$};
      \node [below=of a00] (a10) {$a_{1,0}$};  
      \node[right=of a10] (a11) {$a_{1,1}$};
      \node[right=of a11] (a12) {$a_{1,2}$};
    \end{scope}
 
    \node [above] (decl) at (a01.north) {$\texttt{int a[2][3]}$};
  
    \uncover<3->{
      \node [right=of a02, draw = black,box] (b00){$a_{0,0}$};
      \begin{scope}
        [every node/.style={box,draw=black}, node distance=0pt];
        \node [right=of b00] (b01) {$a_{0,1}$};
        \node [right=of b01] (b02) {$a_{0,2}$};
        \node [right=of b02] (b10) {$a_{1,0}$};  
        \node[right=of b10] (b11) {$a_{1,1}$};
        \node[right=of b11] (b12) {$a_{1,2}$};
      \end{scope}
      \node [above] (actual) at (b02.north east) {Actual storage in memory};
    }

    \uncover<5->{
      \node [below=of b00] (a) {$\texttt{a}$}; \draw [->,thick, out=180, in = 270] (a) to (b00);
      \node [right=of a] (ap1) {$\texttt{a + 1}$}; \draw [->,thick, out=0, in = 270] (ap1) to (b10);
    }
  \end{tikzpicture}

\end{frame}

  \begin{frame}
    \frametitle{Two dimensional array and pointer to array}
    \begin{tabbing}\tt
~~~~~~~int~a\char91{}2\char93{}\char91{}3\char93{}~\char61{}~\char123{}~\\
\tt ~~~~~~~~~~~~~~~~~~~~~~~00\char44{}01\char44{}02\char44{}\\
\tt ~~~~~~~~~~~~~~~~~~~~~~~10\char44{}11\char44{}12\\
\tt ~~~~~~~~~~~~~~~~~~~~~\char125{}~\\
\tt ~~~~~~~int~\char40{}\char42{}ptr\char41{}\char91{}3\char93{}\char44{}~qtr\char91{}\char93{}\char91{}3\char93{}\char59{}\\
\tt ~~~~~~~ptr~\char61{}~a\char59{}\\
\tt ~~~~~~~qtr~\char61{}~a~\char43{}~1\char59{}
\end{tabbing}

 \begin{tikzpicture}
   
   \begin{scope}
     [every node/.style={box,draw=black}, node distance=0pt];
     \node (a00) {$0$};
     \node [right=of a00] (a01) {$1$};
     \node [right=of a01] (a02) {$2$};
     \node [below=of a00] (a10) {$10$};  
     \node[right=of a10] (a11) {$11$};
     \node[right=of a11] (a12) {$12$};
   \end{scope}
    
   \node [right=of a02, draw = black,box] (b00){$0$};
   
   \begin{scope}
     [every node/.style={box,draw=black}, node distance=0pt];
     \node [right=of b00] (b01) {$1$};
     \node [right=of b01] (b02) {$2$};
     \node [right=of b02] (b10) {$10$};  
     \node[right=of b10] (b11) {$11$};
     \node[right=of b11] (b12) {$12$};
   \end{scope}

    
   % \node [above] (decl) at (a01.north) {$\texttt{int a[2][3]}$};
   % \node [above] (actual) at (b02.north east) {Actual storage in memory};
    
   \node [below=of b00, box, label=left:$\mathtt{ptr}$] (ptr) {$\cdot$}; 
   \draw [->,thick, out=180, in = 270] (ptr.mid) to (b00);
   \node [below=of b11, box, label=right:$\mathtt{qtr}$] (qtr) {$\cdot$}; 
   \draw [->,thick, out=0, in = 270] (qtr.mid) to (b10);
 \end{tikzpicture}


\end{frame}

\begin{frame}
  \frametitle{Passing multi-dimensional array to function}
  \begin{itemize}
  \item For a 2-d array \text{\tt T~a\char91{}m\char93{}\char91{}n\char93{}} remember the
    value of a is pointer to an array of \text{\tt n} T's.
  \item Therefore to pass such an array declare the
    argument as \text{\tt T~\char40{}\char42{}ptr\char41{}\char91{}n\char93{}}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Example code adding matrices}
  
  \begin{tabbing}\tt
~\char47{}\char42{}~adding~5x5~matrix~\char42{}\char47{}\\
\tt ~int~addMatrix\char40{}double~\char40{}\char42{}a\char41{}\char91{}5\char93{}\char44{}~double~\char40{}\char42{}b\char41{}\char91{}5\char93{}\char44{}~double~\char40{}\char42{}c\char41{}\char91{}5\char93{}\char41{}\\
\tt ~\char123{}\\
\tt ~~~for\char40{}int~i~\char61{}~0\char59{}~i~\char60{}~5\char59{}~i\char43{}\char43{}\char41{}\\
\tt ~~~\char123{}\\
\tt ~~~~~for\char40{}int~j~\char61{}~0\char59{}~j~\char60{}~5\char59{}~j\char43{}\char43{}\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~c\char91{}i\char93{}\char91{}j\char93{}~\char61{}~a\char91{}i\char93{}\char91{}j\char93{}~\char43{}~b\char91{}i\char93{}\char91{}j\char93{}\char59{}\\
\tt ~~~~~\char125{}\\
\tt ~~~\char125{}\\
\tt ~\char125{}
\end{tabbing}
      
\end{frame}

\begin{frame}

\end{frame}

\end{document}
