\documentclass{beamer} %include lhs2TeX.fmt \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 |_| (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 |*|, |/| \item |+|, |-| \end{itemize} } \item relational operators \item boolean operators \end{itemize} \uncover<3->{ eg |- 4 * 3 < 1 && 2 > x + 5 | is same as |((-4) * 3) < 1) && (2 > (x + 5))| } \end{frame} \begin{frame} \frametitle{Integer expressions} \begin{itemize} \item Variable declaration \begin{spec} int x; int foo=100; \end{spec} \item Printing \begin{spec} printf("The value of integer variable %d\n",x); \end{spec} \item Arithmetic operators |+|,|-|,|*|,|/|, |%|, |-| (unary minus) etc \item Relational operators. |<|,|<=|,|>|, |>=|, |==| etc \end{itemize} \begin{block}{Important} The operator for checking for equality is |==| and not |=|. \end{block} \end{frame} \begin{frame} \frametitle{Factorial program} \begin{code} # include int main(){ unsigned int n; int i = 1; printf("Enter the value: "); scanf("%d",&n); unsigned int fact = 1; while(i <= n) { fact = fact * i; i = i + 1; } printf("The factorial of %u is %u\n", n, fact); } \end{code} \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{spec} float x; float pi=3.141; double avagadro = 6.023e23; \end{spec} \item Printing \begin{spec} printf("Values are %f, %g\n",x,avagadro); \end{spec} \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{spec} int u = 10; int v = 11; float av; av = (u + v)/2 prinf("%f",av); \end{spec} \end{frame} \begin{frame} \frametitle{Assignments} Assignment is used to modify the value of a variable. eg. \begin{spec} x = 10; foo = 4.2; \end{spec} \pause \begin{block}{Assignment as expression} In C assignment itself is an expression. \begin{spec} x = y = 10; \end{spec} \end{block} \pause \begin{block}{Special assignment} \begin{multicols}{2} \begin{spec} i++; foo *= 10; \end{spec} \newpage \begin{spec} i = i + 1; foo = foo * 10; \end{spec} \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{spec} x = 100; if (x = 0) { printf("Null value unexpected"); }else{ printf("Good value"); } \end{spec} \end{block} \end{frame} \end{document}