\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{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 |T| should have as argument type |T *|. \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{spec} int a[2][3], *b[3], (*c)[3]; \end{spec} \pause \begin{itemize} \item |a| is a 2 dimensional array integer. \item |b| is an array 3 of pointers to integers. \item |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 |T a[2][3]|. \uncover<2->{ \item The array is row major, i.e. the second index moves faster. } \uncover<4->{ \item The expression |a| is of type pointer to array of 3 ints and therefore |a| points to |a[0][0]| and |a+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{code} int a[2][3] = { 00,01,02, 10,11,12 } int (*ptr)[3], qtr[][3]; ptr = a; qtr = a + 1; \end{code} \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 |T a[m][n]| remember the value of a is pointer to an array of |n| T's. \item Therefore to pass such an array declare the argument as |T (*ptr)[n]| \end{itemize} \end{frame} \begin{frame} \frametitle{Example code adding matrices} \begin{code} /* adding 5x5 matrix */ int addMatrix(double (*a)[5], double (*b)[5], double (*c)[5]) { for(int i = 0; i < 5; i++) { for(int j = 0; j < 5; j++) { c[i][j] = a[i][j] + b[i][j]; } } } \end{code} \end{frame} \begin{frame} \end{frame} \end{document}