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

\usepackage{tikz}
\usepackage{multicol}

\usetikzlibrary{positioning,shapes,chains}
\usetikzlibrary{shapes.symbols}
\usetikzlibrary{matrix}
\usepackage{dot2texi}
\input{../include/grammar.tex}
\title{Fundamentals of Computing: Lecture 4}
\date{August 3, 2009}
\begin{document}
\begin{frame}
\maketitle
\end{frame}

\begin{frame}
  \frametitle{Summary of previous lecture (Variables)}
  \begin{itemize}
  \item Variables are memory location where values are stored
  \item They have a name, a type associated with them and a value.
  \item The name of a variable can start with a letter and contain
    letter or digit.
  \item The special character \text{\tt \char95{}} (under score) is considered as a letter.
  \item The value associated can be changed using an assignment.
  \end{itemize}
  
\end{frame}

%\begin{frame}
%  \frametitle
%  \input{syntax/variable.tex}
%\end{frame}

\begin{frame}
  \frametitle{Summary Operator precedence}
  
  \begin{itemize}
    \item Arithmetic operators
      \uncover<2->{
        \begin{itemize}
        \item Unary operators (unary -)
        \item \text{\tt \char42{}}, \text{\tt \char47{}}
        \item \text{\tt \char43{}}, \text{\tt \char45{}}
        \end{itemize}
      }
    \item relational operators
    \item boolean operators
  \end{itemize}
  \uncover<3->{
    eg \text{\tt \char45{}~4~\char42{}~3~\char60{}~1~\char38{}\char38{}~2~\char62{}~x~\char43{}~5~} is same as
    \text{\tt \char40{}\char40{}\char45{}4\char41{}~\char42{}~3\char41{}~\char60{}~1\char41{}~\char38{}\char38{}~\char40{}2~\char62{}~\char40{}x~\char43{}~5\char41{}\char41{}}
  }
  
\end{frame}

\begin{frame}
  \frametitle{Integer expressions}
  \begin{itemize}
  \item Variable declaration
    \begin{tabbing}\tt
~~~~~~~int~x\char59{}\\
\tt ~~~~~~~int~foo\char61{}100\char59{}
\end{tabbing}
  \item Printing
    \begin{tabbing}\tt
~~~~~~~printf\char40{}\char34{}The~value~of~integer~variable~\char37{}d\char92{}n\char34{}\char44{}x\char41{}\char59{}
\end{tabbing}
  \item Arithmetic operators
    \text{\tt \char43{}},\text{\tt \char45{}},\text{\tt \char42{}},\text{\tt \char47{}}, \text{\tt \char37{}}, \text{\tt \char45{}} (unary minus) etc
  \item Relational operators.
    \text{\tt \char60{}},\text{\tt \char60{}\char61{}},\text{\tt \char62{}}, \text{\tt \char62{}\char61{}}, \text{\tt \char61{}\char61{}} etc
  \end{itemize}
  \begin{block}{Important}
    The operator for checking for equality is \text{\tt \char61{}\char61{}} and not \text{\tt \char61{}}.
  \end{block}
\end{frame}

\begin{frame}
  \frametitle{Factorial program}  
  \begin{tabbing}\tt
~~~~~\char35{}~include\char60{}stdio\char46{}h\char62{}\\
\tt ~~~~~\\
\tt ~~~~~int~main\char40{}\char41{}\char123{}\\
\tt ~~~~~~~int~n\char59{}\\
\tt ~~~~~~~int~i~\char61{}~1\char59{}\\
\tt ~~~~~~~printf\char40{}\char34{}Enter~the~value\char58{}~\char34{}\char41{}\char59{}\\
\tt ~~~~~~~scanf\char40{}\char34{}\char37{}d\char34{}\char44{}\char38{}n\char41{}\char59{}\\
\tt ~~~~~~~int~fact~\char61{}~1\char59{}\\
\tt ~~~~~~~while\char40{}i~\char60{}\char61{}~n\char41{}\\
\tt ~~~~~~~\char123{}\\
\tt ~~~~~~~~~fact~\char61{}~fact~\char42{}~i\char59{}\\
\tt ~~~~~~~~~i~\char61{}~i~\char43{}~1\char59{}\\
\tt ~~~~~~~\char125{}\\
\tt ~~~~~~~printf\char40{}\char34{}The~factorial~of~\char37{}d~is~\char37{}d\char92{}n\char34{}\char44{}~n\char44{}~fact\char41{}\char59{}\\
\tt ~~~~~\char125{}
\end{tabbing}

\end{frame}

\begin{frame}
  \frametitle{Why did the factorial program go wrong?}
  \pause
  \begin{block}{Answer}
    Integers are of fixed precision typically 32 bits.
  \end{block}
\end{frame}

\begin{frame}
  \frametitle{Real numbers expressions}
  \begin{itemize}
  \item Variable declaration
    \begin{tabbing}\tt
~~~~~~~float~x\char59{}\\
\tt ~~~~~~~float~pi\char61{}3\char46{}141\char59{}\\
\tt ~~~~~~~double~avagadro~\char61{}~6\char46{}023e23\char59{}
\end{tabbing}
  \item Printing
    \begin{tabbing}\tt
~~~~~~~printf\char40{}\char34{}Values~are~\char37{}f\char44{}~\char37{}g\char92{}n\char34{}\char44{}x\char44{}avagadro\char41{}\char59{}
\end{tabbing}
  \item Arithmetic operators and relational operators are similar to integers.
  \end{itemize}
  \begin{block}{Important}
    Use double always. That gives better precission.
  \end{block}
\end{frame}


\begin{frame}
  \frametitle{Integers and Floats}
  C does automatic coversion between integers and floats.
  \begin{itemize}
  \item Integer to  Float/Double extension
  \item Float/Double to integer truncation
  \end{itemize}
  
  Unfortunately this is a very bad design.
  
  \begin{tabbing}\tt
~~~~~int~u~\char61{}~10\char59{}\\
\tt ~~~~~int~v~\char61{}~11\char59{}\\
\tt ~~~~~float~av\char59{}\\
\tt ~~~~~av~\char61{}~\char40{}u~\char43{}~v\char41{}\char47{}2~\\
\tt ~~~~~prinf\char40{}\char34{}\char37{}f\char34{}\char44{}av\char41{}\char59{}
\end{tabbing}

\end{frame}

\begin{frame}
  \frametitle{Assignments}
  
  Assignment is used to modify the value of a variable.
  
  eg.
  \begin{tabbing}\tt
~~~~~x~\char61{}~10\char59{}\\
\tt ~~~~~foo~\char61{}~4\char46{}2\char59{}
\end{tabbing}

  \pause
  \begin{block}{Assignment as expression}
    In C assignment itself is an expression.
    \begin{tabbing}\tt
~~~~~~~x~\char61{}~y~\char61{}~10\char59{}~
\end{tabbing}
  \end{block}
  \pause
  \begin{block}{Special assignment}
  \begin{multicols}{2}
    \begin{tabbing}\tt
~~~~~~~i\char43{}\char43{}\char59{}\\
\tt ~~~~~~~foo~\char42{}\char61{}~10\char59{}
\end{tabbing}
    \newpage
    \begin{tabbing}\tt
~~~~~~~i~\char61{}~i~\char43{}~1\char59{}\\
\tt ~~~~~~~foo~\char61{}~foo~\char42{}~10\char59{}
\end{tabbing}
  \end{multicols}
  \end{block}

\end{frame}

\begin{frame}
  \frametitle{Boolean}
  There are no booleans in C.
  
  Integers, characters etc all play the role of boolean

  value of 0 is false.
  value of nonzero is true.

  \pause
  \begin{block}{WARNING}
    
    \begin{tabbing}\tt
~~~~~~~x~\char61{}~100\char59{}\\
\tt ~~~~~~~if~\char40{}x~\char61{}~0\char41{}\\
\tt ~~~~~~~\char123{}\\
\tt ~~~~~~~~~printf\char40{}\char34{}Null~value~unexpected\char34{}\char41{}\char59{}\\
\tt ~~~~~~~\char125{}else\char123{}\\
\tt ~~~~~~~~~printf\char40{}\char34{}Good~value\char34{}\char41{}\char59{}\\
\tt ~~~~~~~\char125{}
\end{tabbing}
      
  \end{block}
\end{frame}
\end{document}
